lua之屏蔽字替换为 '*'
来源:互联网 发布:网络投资那个好 编辑:程序博客网 时间:2024/05/30 23:21
local socket = require "socket"local function utf8len(ch)if not ch thenreturn -1endif ch < 0x80 thenreturn 1elseif ch < 0xC0 thenreturn -1elseif ch < 0xE0 thenreturn 2elseif ch < 0xF0 thenreturn 3elseif ch < 0xF8 thenreturn 4elseif ch < 0xFC thenreturn 5elseif ch < 0xFE thenreturn 6elsereturn -1endendlocal function getutf8tbl(input)if not input thenreturn nil, nilendlocal tbl = {}local tbllen = {}local len = #inputlocal i = 1while i <= len dolocal j = utf8len(string.byte(string.sub(input, i, i)))if j <= 0 or i + j - 1 > len thenreturn nil, nilendtable.insert(tbl, string.sub(input, i, i + j - 1))table.insert(tbllen, j)i = i + jendreturn tbl, tbllenendlocal f0 = socket.gettime()local data = {}local maxlen = 0local firstword = {}for line in io.lines("forbidden_words.txt") dolocal len = string.len(line)if data[len] == nil thendata[len] = {}enddata[len][line] = trueif len > maxlen thenmaxlen = lenendlocal wordlen = utf8len(string.byte(string.sub(line, 1, 1)))if wordlen > 0 thenfirstword[string.sub(line, 1, wordlen)] = trueendendlocal f1 = socket.gettime()print(f1 - f0)local fout = io.open("out.txt", "w")for str in io.lines("test.txt") dolocal t0 = socket.gettime()local tbl, tbllen = getutf8tbl(str)if not tbl thenprint(str .. " input is invalid")endlocal count = 0local len = #tblfor i = 1, len dolocal wordlen = 0if tbl[i] ~= '*' and firstword[tbl[i]] thenfor j = 1, len - i + 1 dowordlen = wordlen + tbllen[i + j -1]if wordlen > maxlen then --optimizationbreakendlocal t = data[wordlen]if t thenlocal word = table.concat(tbl, nil, i, i + j - 1)count = count + 1if t[word] thenfor k = i, i + j - 1 dotbl[k] = '*'endbreakendendendendendlocal t1 = socket.gettime()fout:write(table.concat(tbl), t1 - t0, '\n')print(count, t1 - t0)endfout:close()
阅读全文
0 0
- lua之屏蔽字替换为 '*'
- lua替换VIP图片为label
- 屏蔽字然后使用*替换(转)
- 新年第一贴:用SQL实现替换屏蔽字方法
- angular之设置屏蔽字
- Java逆向之利用lua动态替换java中的代码
- Android逆向之利用lua动态替换java中方法
- lua 屏蔽密码功能实现
- Lua的字符替换
- lua 字符串替换
- 《Unix环境高级编程》:为进程打印屏蔽字
- Android 自定义字体,字替换为图片
- 使用正则替换文章屏蔽词
- lua替换CCSprite的图片
- lua中字符匹配替换
- lua实现模版字符串替换
- LUA 捕获、正则、截取、替换
- JAVA敏感词替换为*(包含中文词替换一个字替换一个*)
- AngularJS模块详解
- spring MVC 的MultipartFile转File读取
- spring boot 集成shiro的配置
- java的几种对象(po,dto,dao等)
- TensorFlow word2vec_basic 代码分析
- lua之屏蔽字替换为 '*'
- 真正的程序员到底应该是什么样子的?
- weblogic控制台web项目部署
- 有一个已经排好序的数组。输入一个数,按原来规律插入数组
- 文本编辑器
- IntelliJ IDEA 中打开多个文件会发现之前打开的找不到了。这是因为没有设置的原因
- Nginx 安装配置
- 关于Bugly热更新sdk使用的一些小细节
- Kotlin最简单的入门教程——基础类型(一)