参考了DFA算法的简单说明与案例实现
构造过滤器
local FilterData = {
}
local FilterTree = {
}
local IsEnd = "IsEnd"
function FilterData. Load ( )
for _, v in ipairs ( exd[ "GT_" .. "FilterText" ] ) do
local FilterStr = v. MaskWord
local CurMap = FilterTree
ForEachString ( FilterStr,
function ( Str)
if CurMap[ Str] == nil then
CurMap[ Str] = {
}
CurMap[ Str] [ IsEnd] = false
CurMap = CurMap[ Str]
else
CurMap = CurMap[ Str]
end
end ,
true )
CurMap[ IsEnd] = true
end
end
查找
function FilterData. MaskWordFilter ( Str)
local BeginIndex = 1
local MatchingTable = {
}
while BeginIndex < # Str do
local NowMap = FilterTree
local CurStr = string. sub ( Str, BeginIndex, # Str)
local CurIndex = BeginIndex
local LeftMatchIndex = 0
local RightMatchIndex = 0
local i = 1
local AllowSpecialSymbols = false
while true do
local char = string. sub ( CurStr, i, i)
local Ascll = string. byte ( char)
if Ascll > 128 then
char = string. sub ( CurStr, i, i+ 2 )
i = i + 3
if NowMap[ char] ~= nil then
if LeftMatchIndex == 0 then
LeftMatchIndex = CurIndex
end
if NowMap[ char] [