Skip to content

Commit 8d11302

Browse files
committed
fix: case-insensitive shuangpin input
1 parent 48f7286 commit 8d11302

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

lua/flash-zh/init.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ function M.zh_mode(str)
4545
local map = char_map.get()
4646
local regexs = {}
4747
while string.len(str) > 1 do
48-
local k = string.sub(str, 1, 2)
48+
local orig = string.sub(str, 1, 2)
49+
local k = orig:lower()
4950
-- Be defensive: unknown keys should not crash the search.
50-
regexs[#regexs + 1] = map.char2patterns[k] or ("[" .. k .. string.upper(k) .. "]")
51+
regexs[#regexs + 1] = map.char2patterns[k] or ("[" .. orig:lower() .. orig:upper() .. "]")
5152
str = string.sub(str, 3)
5253
end
5354
if string.len(str) == 1 then
54-
regexs[#regexs + 1] = map.char1patterns[str] or ("[" .. str .. string.upper(str) .. "]")
55+
local orig = str
56+
local k = orig:lower()
57+
regexs[#regexs + 1] = map.char1patterns[k] or ("[" .. orig:lower() .. orig:upper() .. "]")
5558
end
5659
local ret = table.concat(regexs)
5760
return ret, ret
@@ -60,7 +63,7 @@ end
6063
local function get_nodes(map)
6164
return {
6265
alpha = function(str)
63-
return "[" .. str .. string.upper(str) .. "]"
66+
return "[" .. str:lower() .. str:upper() .. "]"
6467
end,
6568
pinyin = function(str)
6669
return map.char2patterns[str]
@@ -103,13 +106,14 @@ function M.parser(str, prefix)
103106
if secondchar == "" then
104107
local prefix2 = M.copy(prefix)
105108
prefix[#prefix + 1] = { str = firstchar, type = "alpha" }
106-
prefix2[#prefix2 + 1] = { str = firstchar, type = "singlepin" }
109+
prefix2[#prefix2 + 1] = { str = firstchar:lower(), type = "singlepin" }
107110
return { prefix, prefix2 }
108111
elseif string.match(secondchar, "%a") then
109-
if map.char2patterns[firstchar .. secondchar] then
112+
local code = (firstchar .. secondchar):lower()
113+
if map.char2patterns[code] then
110114
local prefix2 = M.copy(prefix)
111115
prefix2[#prefix2 + 1] = { str = firstchar, type = "alpha" }
112-
prefix[#prefix + 1] = { str = firstchar .. secondchar, type = "pinyin" }
116+
prefix[#prefix + 1] = { str = code, type = "pinyin" }
113117
local str2 = string.sub(str, 2, -1)
114118
str = string.sub(str, 3, -1)
115119
return M.merge_table(M.parser(str, prefix), M.parser(str2, prefix2))

0 commit comments

Comments
 (0)