Last active
August 27, 2016 04:00
-
-
Save litefeel/45a3311ea44c55debfe441600275f43d to your computer and use it in GitHub Desktop.
lua版干扰屏蔽字编码解码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- split utf8 char | |
function string.splitUtf8(str) | |
local list = {} | |
local p = 1 | |
for i = 1, #str do | |
local c = string.byte(str, i) | |
if c < 0x80 then | |
-- ascii | |
table.insert(list, string.sub(str, p, i)) | |
p = i + 1 | |
elseif c >= 0xC0 then | |
-- the first byte of multiple-byte character | |
if p <= i - 1 then | |
-- insert previous character | |
table.insert(list, string.sub(str, p, i - 1)) | |
p = i | |
end | |
end | |
end | |
if p <= #str then | |
table.insert(list, string.sub(str, p, #str)) | |
end | |
return list | |
end | |
-- 干扰屏蔽字 | |
-- 假设屏蔽字不会出现单字屏蔽字,用^符号分割所有字符 | |
function string.encodeSpam(str) | |
return table.concat(string.splitUtf8(str), "^") | |
end | |
-- 假设^符号不会在原始字符串中用到,直接移除所有的^ | |
function string.decodeSpam(str) | |
return string.gsub(str, "%^", "") | |
end | |
-- 如果^符号会出现在原始字符串中,就需要分离所有字符串,隔位移除一个字符 | |
function string.decodeSpam2(str) | |
local list = string.splitUtf8(str) | |
local list2 = {} | |
for i = 1, #list, 2 do | |
table.insert(list2, list[i]) | |
end | |
return table.concat(list2, '') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment