From 8071a14e22b392102852a381ec52b53b3b6330cc Mon Sep 17 00:00:00 2001 From: amzxyz Date: Sun, 11 Jan 2026 20:09:48 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=A6=96=E9=80=89=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E6=94=AF=E6=8C=81=E8=BF=9E=E7=BB=AD=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E7=9A=84=E7=AE=80=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_phrase.txt | 7 +++--- lua/super_filter.lua | 55 +++++++++++++++++++++++++++----------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/custom_phrase.txt b/custom_phrase.txt index 99fd0a9..fb47608 100644 --- a/custom_phrase.txt +++ b/custom_phrase.txt @@ -1,3 +1,4 @@ -#给自定义用户词扩展一个换行:\n, 制表符:\t, 回车符:\r, 空格:\s -吧 b 50 -静夜思\n\s\s李白\n床前明月光\n疑似地上霜\n举头望明月\n低头思故乡 jys 50 +#给自定义用户词扩展一个换行:\n, 制表符:\t, 空格:\s, 看不到的零宽空格\z +#连续多个前面的字符:{3}, 恢复自身:\{ +吧 b 5 +静夜思\n\s{3}李白\n床前明月光\n疑似地上霜\n举头望明月\n低头思故乡 jys 5 diff --git a/lua/super_filter.lua b/lua/super_filter.lua index eeeb178..8170d61 100644 --- a/lua/super_filter.lua +++ b/lua/super_filter.lua @@ -85,40 +85,53 @@ local function is_english_candidate(cand) return true end --- 文本格式化(转义 + 自动大写) +-- 文本格式化 (精简版:仅保留转义与重复) local escape_map = { - ["\\n"] = "\n", ["\\t"] = "\t", ["\\r"] = "\r", - ["\\\\"] = "\\", ["\\s"] = " ", ["\\d"] = "-", + ["\\n"] = "\n", -- 换行 + ["\\r"] = "\r", -- 回车 + ["\\t"] = "\t", -- 制表符 + ["\\s"] = " ", -- 空格 + ["\\z"] = "\226\128\139", -- 零宽空格 (隐形阻断符) } -local esc_pattern = "\\[ntrsd\\\\]" + +local utf8_char_pattern = "[%z\1-\127\194-\244][\128-\191]*" local function apply_escape_fast(text) - if not text or find(text, "\\", 1, true) == nil then return text, false end - local new_text = gsub(text, esc_pattern, function(esc) return escape_map[esc] or esc end) + -- 快速预检 + if not text or (not find(text, "\\", 1, true) and not find(text, "{", 1, true)) then + return text, false + end + + local new_text = text + if find(new_text, "\\{", 1, true) then + new_text = gsub(new_text, "\\{", "\1") + end + if find(new_text, "\\", 1, true) then + new_text = gsub(new_text, "\\[ntrsz]", escape_map) + end + if find(new_text, "{", 1, true) then + new_text = gsub(new_text, "(" .. utf8_char_pattern .. ")%{(%d+)}", function(char, count) + local n = tonumber(count) + if n and n > 0 and n < 200 then + return string.rep(char, n) + end + return char .. "{" .. count .. "}" + end) + end + if find(new_text, "\1", 1, true) then + new_text = gsub(new_text, "\1", "{") + end return new_text, new_text ~= text end - local function format_and_autocap(cand) local text = cand.text if not text or text == "" then return cand end - local changed = false - -- 转义替换 (\n, \t, \s 等) - -- 必须先处理转义,因为转义可能会改变字符串开头 (如 \sApple -> Apple) - if find(text, "\\", 1, true) then - local t2, ch = apply_escape_fast(text) - if ch then - text = t2 - changed = true - end - end - -- 输出结果 + local t2, changed = apply_escape_fast(text) if not changed then return cand end - - local nc = Candidate(cand.type, cand.start, cand._end, text, cand.comment) + local nc = Candidate(cand.type, cand.start, cand._end, t2, cand.comment) nc.preedit = cand.preedit return nc end - local function clone_candidate(c) local nc = Candidate(c.type, c.start, c._end, c.text, c.comment) nc.preedit = c.preedit