From 69714292a5e0c4a3c566f95928a81f09bfaf5726 Mon Sep 17 00:00:00 2001 From: DeepChirp <66902050+DeepChirp@users.noreply.github.com> Date: Thu, 23 Oct 2025 08:09:32 +0800 Subject: [PATCH] feat: add % support to calculator (#1298) --- lua/calc_translator.lua | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lua/calc_translator.lua b/lua/calc_translator.lua index 4f19799..3b4ecc0 100644 --- a/lua/calc_translator.lua +++ b/lua/calc_translator.lua @@ -1,5 +1,6 @@ -- 计算器插件 -- author: https://github.com/ChaosAlphard +-- contributor: https://github.com/DeepChirp local calc = {} function calc.init( env ) @@ -189,13 +190,27 @@ local function replaceToFactorial( str ) return str:gsub( '([0-9]+)!', 'fact(%1)' ) end +-- 处理百分号 +local function replacePercent(str) + str = str .. ' ' + -- 先处理括号形式 ( ... )% + str = str:gsub("(%b())%%(%D)", function(block, tail) + return "(" .. block .. "/100)" .. tail + end) + -- 再处理纯数字形式 123% 12.3% + str = str:gsub("(%d+%.?%d*)%%(%D)", function(num, tail) + return "(" .. num .. "/100)" .. tail + end) + return str:sub(1, -2) +end + -- 简单计算器 function calc.func( input, seg, env ) if not seg:has_tag( 'calculator' ) or input == '' then return end -- 提取算式 local express = truncateFromStart( input, env.prefix ) if express == '' then return end -- 防止用户写错了正则表达式造成错误 - local code = replaceToFactorial( express ) + local code = replacePercent( replaceToFactorial( express ) ) local success, result = pcall( load( 'return ' .. code, 'calculate', 't', calcPlugin ) ) if success and result and (type( result ) == 'string' or type( result ) == 'number') and #tostring( result ) > 0 then yield_calc_cand( seg, result, '', express, env.show_prefix )