summaryrefslogtreecommitdiff
path: root/script/core/matchkey.lua
blob: 8397749604227a844f65a44c98f7461e7f0769cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
---@param input string
---@param other string
---@param fast  boolean
---@return boolean isMatch
---@return number  deviation
return function (input, other, fast)
    if input == other then
        return true, 0
    end
    if input == '' then
        return true, 0
    end
    if #input > #other then
        return false, 0
    end
    local lMe = input:lower()
    local lOther = other:lower()
    if lMe == lOther:sub(1, #lMe) then
        return true, 0
    end
    if fast and input:sub(1, 1) ~= other:sub(1, 1) then
        return false, 0
    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, 0
        end
    end
    return true, 1
end