Skip to content

Commit

Permalink
chore: some adjustments for unicode.lua (#898)
Browse files Browse the repository at this point in the history
* Update unicode.lua

1.  config:get_string() 可能取得 nil , 增加預設字串
2.  seg:has_tag() , input == "" 提前判斷,可以避免 patterns 未設定的問題

* trim and add change log

---------

Co-authored-by: mirtlecn <[email protected]>
  • Loading branch information
shewer and mirtlecn authored Jun 1, 2024
1 parent 0d93df2 commit e33e1d2
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lua/unicode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@
-- 示例:输入 U62fc 得到「拼」
-- 触发前缀默认为 recognizer/patterns/unicode 的第 2 个字符,即 U
-- 2024.02.26: 限定编码最大值
-- 2024.06.01: 部分变量初始化,条件语句调整。

local path = 'recognizer/patterns/unicode'
local function unicode(input, seg, env)
if not seg:has_tag("unicode") or input == '' then return end
-- 获取 recognizer/patterns/unicode 的第 2 个字符作为触发前缀
env.unicode_keyword = env.unicode_keyword or
env.engine.schema.config:get_string('recognizer/patterns/unicode'):sub(2, 2)
if seg:has_tag("unicode") and env.unicode_keyword ~= '' and input:sub(1, 1) == env.unicode_keyword then
local ucodestr = input:match(env.unicode_keyword .. "(%x+)")
if ucodestr and #ucodestr > 1 then
local code = tonumber(ucodestr, 16)
if code > 0x10FFFF then
yield(Candidate("unicode", seg.start, seg._end, "数值超限!", ""))
return
end
local text = utf8.char(code)
yield(Candidate("unicode", seg.start, seg._end, text, string.format("U%x", code)))
if code < 0x10000 then
for i = 0, 15 do
local text = utf8.char(code * 16 + i)
yield(Candidate("unicode", seg.start, seg._end, text, string.format("U%x~%x", code, i)))
end
end
-- config:get_string(path) 可能取得 nil 造成error
if not env.unicode_keyword then
local pattern = env.engine.schema.config:get_string(path) or "UU"
env.unicode_keyword = pattern:sub(2,2)
end

local ucodestr = input:match(env.unicode_keyword .. "(%x+)")
if ucodestr and #ucodestr > 1 then
local code = tonumber(ucodestr, 16)
if code > 0x10FFFF then
yield(Candidate("unicode", seg.start, seg._end, "数值超限!", ""))
return
end
local text = utf8.char(code)
yield(Candidate("unicode", seg.start, seg._end, text, string.format("U%x", code)))
if code < 0x10000 then
for i = 0, 15 do
local text = utf8.char(code * 16 + i)
yield(Candidate("unicode", seg.start, seg._end, text, string.format("U%x~%x", code, i)))
end
end
end
end
Expand Down

0 comments on commit e33e1d2

Please sign in to comment.