• Lua"invalid pattern capture"实现防注入的string.replace函数


    起因

    上线的游戏,有个俄语玩家提bug说聊天界面崩溃了,登上去看了下并没有出现问题,于是直接去捞日志,发现了invalid pattern capture的错误日志,于是请教度娘,找到了答案,可能是匹配参数中的pattern包含了括号
    游戏聊天系统里面有@功能,处理过程需要用到string.gsub匹配替换玩家名字。昵称规则中是不允许带括号的,怎么会这样呢?原来是聊天里面还有个功能,自动翻译,丫把外文昵称的某个符号翻译成了括号,然后匹配的时候就出错了。

    解决

    实现自己的替换函数,采用string.find,指定不执行模式匹配。

    -- 字符串替换【不执行模式匹配】
    -- s       源字符串
    -- pattern 匹配字符串
    -- repl    替换字符串
    --
    -- 成功返回替换后的字符串,失败返回源字符串
    string.replace = function(s, pattern, repl)
        local i,j = string.find(s, pattern, 1, true)
        if i and j then
            local ret = {}
            local start = 1
            while i and j do
                table.insert(ret, string.sub(s, start, i - 1))
                table.insert(ret, repl)
                start = j + 1
                i,j = string.find(s, pattern, start, true)
            end
            table.insert(ret, string.sub(s, start))
            return table.concat(ret)
        end
        return s
    end
    

    参考

    lua下实现防注入的string.replace函数(这位博主的内容不错,推荐)

  • 相关阅读:
    pip python代码
    Saltstack module http 详解
    Saltstack module hosts 详解
    Saltstack module highstate_doc 详解
    Saltstack module hashutil 详解
    Saltstack module group 详解
    Saltstack module grains 详解
    Saltstack module grafana4 详解
    Saltstack module google_chat 详解
    Saltstack module gnome 详解
  • 原文地址:https://www.cnblogs.com/nickcan/p/16126415.html
Copyright © 2020-2023  润新知