From 4ca61ec457822dd14966afa0752340ae8ce180a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Fri, 20 Nov 2020 21:57:09 +0800 Subject: no longer beta --- script/core/matchkey.lua | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 script/core/matchkey.lua (limited to 'script/core/matchkey.lua') diff --git a/script/core/matchkey.lua b/script/core/matchkey.lua new file mode 100644 index 00000000..45c86eff --- /dev/null +++ b/script/core/matchkey.lua @@ -0,0 +1,33 @@ +return function (me, other, fast) + if me == other then + return true + end + if me == '' then + return true + end + if #me > #other then + return false + end + local lMe = me:lower() + local lOther = other:lower() + if lMe == lOther:sub(1, #lMe) then + return true + end + if fast and me:sub(1, 1) ~= other:sub(1, 1) then + return false + end + local chars = {} + for i = 1, #lOther do + local c = lOther:sub(i, i) + chars[c] = (chars[c] or 0) + 1 + end + for i = 1, #lMe do + local c = lMe:sub(i, i) + if chars[c] and chars[c] > 0 then + chars[c] = chars[c] - 1 + else + return false + end + end + return true +end -- cgit v1.2.3