注:文章转载需注明出处,违规必究。
原文地址:点击打开链接
最近在Xlua中做一个聊天功能,其中提供了敏感词汇表,不过我发现其中114项(共16000+项)在require时会失败导致界面加载不出来,如下:
--"<\haschisch\b",
--"\<an[oi]\>",
--"\<anal",
--"\<bagasc",
--"\<baldracc",
--"\<balle\>",
--"\<bastard",
--"\<batton[ae]\>",
--"\<bocchin",
--"\<bong\>",
--"\<bukk?ak+e\>",
--"\<caga",
--"\<cappell",
--"\<checc",
--"\<chiava",
--"\<cocaina\>",
--"\<coddio\>",
--"\<cogl",
--"\<cul[aoi]\>",
--"\<dild[oi]\>",
--"\<dio [ck]ane?\>",
--"\<dio boia\>",
--"\<dio porco\>",
--"\<dio[ck]ane?\>",
--"\<dioboia\>",
--"\<dioporco\>",
--"\<ditalin",
--"\<eiacul",
--"\<fanc",
--"\<fellatio\>",
--"\<fi[cg]a",
--"\<figli[oa] di puttana\>",
--"\<figli[oa] di troia\>",
--"\<figli[oa]diputtana\>",
--"\<figli[oa]ditroia\>",
--"\<fott",
--"\<fregn",
--"\<froci",
--"\<gangbang\>",
--"\<ganja\>",
--"\<gay\>",
--"\<hashish\>",
--"\<ingoi",
--"\<lesb",
--"\<limona",
--"\<lsd\>",
--"\<mari?juana\>",
--"\<merd",
--"\<mescalina\>",
--"\<metanfetamina\>",
--"\<meth\>",
--"\<mignott+",
--"\<milf\>",
--"\<minchi",
--"\<nazi",
--"\<necrofil",
--"\<negr",
--"\<nerchi",
--"\<oppio\>",
--"\<orgasm",
--"\<pedofil",
--"\<pen[ei]\>",
--"\<pipp[ae]\>",
--"\<pirl",
--"\<pompin",
--"\<porc",
--"\<porca madonna\>",
--"\<porca puttana\>",
--"\<porcamadonna\>",
--"\<porcaputtana\>",
--"\<porco dio\>",
--"\<porcod+io+\>",
--"\<porn",
--"\<puttan",
--"\<r[ie]cchion",
--"\<roipnol\>",
--"\<sborr?",
--"\<scopa",
--"\<sega",
--"\<seghe\>",
--"\<sorca\>",
--"\<sorche\>",
--"\<sperma\>",
--"\<stronz",
--"\<stupr",
--"\<succhiacazz[oi]\>",
--"\<succhiaminchi[ae]\>",
--"\<succhiapen[ei]\>",
--"\<terron",
--"\<testa di cazzo\>",
--"\<testa di minchia\>",
--"\<testadicazzo\>",
--"\<testadiminchia\>",
--"\<tett[ae]\>",
--"\<tetton[ae]\>",
--"\<trans\>",
--"\<transessual[ei]\>",
--"\<travell[io]\>",
--"\<travon[ei]\>",
--"\<troi",
--"\<tromba",
--"\<vacca\>",
--"\<vacch",
--"\<vaffancul",
--"\<vagin",
--"\<viagra\>",
--"\<vibrator[ei]\>",
--"\<violentare\>",
--"\<zinn[ae]\>",
--"\<zoccol[ae]\>",
--"\<zoofil",
--"[\S]*\.com\b",
--"[\S]*\.cn\b",
以上部分取消注释则无法加载
具体屏蔽方法如下:
--检查违规字符 并返回替换后的字符
function UIUtils.CheckString(acStr,gsubChar)
local tempAcStr = acStr
acStr = string.gsub(acStr, " ", "")
acStr = string.gsub(acStr, "\r", "")
acStr = string.gsub(acStr, "\n", "")
local bHasForbidden = false
for k,v in pairs(BannedWordsTable) do
local startIndex,endIndex = string.find(acStr,v)
local nextChar = ""
local localHasForbidden = false
local checkRet = nil
if startIndex ~= nil and endIndex ~=nil then
local bHas = UIUtils.HasChinese(v)
checkRet = v
if bHas == false then -- 不含中文
local iLen = #acStr
if startIndex == 1 and string.find(acStr,v.."%W") then --- %W子母和数字
bHasForbidden = true
localHasForbidden = true
if not gsubChar then
break
end
elseif string.find(acStr,"%W"..v.."%W") then
bHasForbidden = true
localHasForbidden = true
if not gsubChar then
break
end
elseif endIndex == #acStr and string.find(acStr,"%W"..v) then
bHasForbidden = true
localHasForbidden = true
if not gsubChar then
break
end
end
else
bHasForbidden = true
localHasForbidden = true
if not gsubChar then
break
end
end
end
if gsubChar and localHasForbidden then
local startIndex, endIndex = string.find(acStr, checkRet)
while startIndex do
-- get utf8 string
local subString = string.sub(acStr, startIndex, endIndex)
local replaceStr = string.rep(gsubChar, UIUtils.GetUTF8Length(subString))
acStr = string.gsub(acStr, checkRet, replaceStr, 1)
startIndex, endIndex = string.find(acStr, checkRet, endIndex + 1)
end
end
end
if bHasForbidden == true then
return bHasForbidden, acStr
else
return bHasForbidden, tempAcStr
end
end
function UIUtils.HasChinese(str)
local ptr = 1
repeat
local char = string.byte(str, ptr)
local char_len = UIUtils.GetUTF8CharLength(char)
if char > 127 then
return true
end
ptr = ptr + char_len
until(ptr>#str)
return false
end
--根据UTF8流获取字符串长度
--GetUTF8Length("一二三四五六七") 返回7
function UIUtils.GetUTF8Length(str)
local len = 0
local ptr = 1
repeat
local char = string.byte(str, ptr)
local char_len = UIUtils.GetUTF8CharLength(char)
len = len + 1
ptr = ptr + char_len
until(ptr>#str)
return len
end
--根据首字节获取UTF8需要的字节数
function UIUtils.GetUTF8CharLength(ch)
local utf8_look_for_table = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1,
}
return utf8_look_for_table[ch]
end