improve: 改进 v_filter 的性能 (#147)

This commit is contained in:
t123yh 2023-04-07 21:05:58 +08:00 committed by GitHub
parent d859ee255e
commit 58e5a08ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -237,29 +237,36 @@ end
-- 把候选项应改为「ā á ǎ à …… van vain」让单个字符的排在前面
function v_filter(input, env)
local code = env.engine.context.input -- 当前编码
local l = {}
for cand in input:iter() do
-- 特殊情况处理
if (cand.text == "Vs.") then
yield(cand)
end
-- 特殊情况处理
local arr = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}
for _, v in ipairs(arr) do
if (v == cand.text and string.len(code) == 2 and string.find(code, "v") == 1) then
-- 仅当当前输入以 v 开头,并且编码长度为 2才进行处理
if (string.len(code) == 2 and string.find(code, "v") == 1) then
local l = {}
for cand in input:iter() do
-- 特殊情况处理
if (cand.text == "Vs.") then
yield(cand)
break
end
-- 特殊情况处理
local arr = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }
for _, v in ipairs(arr) do
if (v == cand.text) then
yield(cand)
break
end
end
-- 候选项为单个字符的,提到前面来。
if (utf8.len(cand.text) == 1) then
yield(cand)
else
table.insert(l, cand)
end
end
-- 以 v 开头、2 个长度的编码、候选项为单个字符的,提到前面来。
if (string.len(code) == 2 and string.find(code, "v") == 1 and utf8.len(cand.text) == 1) then
for _, cand in ipairs(l) do
yield(cand)
end
else
for cand in input:iter() do
yield(cand)
else
table.insert(l, cand)
end
end
for _, cand in ipairs(l) do
yield(cand)
end
end
-------------------------------------------------------------