summaryrefslogtreecommitdiff
path: root/server/src/path_filter.lua
blob: 403c938cb6b56efacb4d3fd7d9a9eedc722436cc (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
local m = require 'lpeglabel'

local m_cut  = m.S'\\'
local m_path = (1-m_cut)^1

local function match_any(p, pass)
    return m.P{p + pass * m.V(1)}
end

local function compile_format(fmt)
    local function next(cur)
        local pos, fn = fmt:find('%*+', cur)
        if not pos then
            return m.P(fmt:sub(cur)) * -1
        end
        local word = m.P(fmt:sub(cur, pos-1))
        return word * match_any(next(fn+1), 1)
    end
    return next(1)
end

local function compile_exp(exp)
    exp = exp:gsub('/', '\\')
    if exp:sub(1, 1) == '\\' then
        exp = exp:sub(2)
    else
        exp = '**\\' .. exp
    end
    if exp:sub(-1) == '\\' then
        exp = exp:sub(1, -2)
    end
    local function next(cur)
        local pos, fn = exp:find('[\\]+', cur)
        if not pos then
            return compile_format(exp:sub(cur))
        end
        local fmt = exp:sub(cur, pos-1)
        if fmt == '**' then
            return match_any(next(fn+1), m_path * m_cut)
        elseif fmt == '' then
            return m_cut
        else
            if fn < #exp then
                return m.P(fmt) * m_cut * next(fn+1)
            else
                return m.P(fmt) * m_cut
            end
        end
    end
    return next(1)
end

local function compile_exps(exp)
    local matcher
    for _, exp in ipairs(exp) do
        exp = exp:lower()
        if matcher then
            matcher = matcher + compile_exp(exp)
        else
            matcher = compile_exp(exp)
        end
    end
    return matcher
end

return function (exp)
    local matcher = compile_exps(exp)
    return function (path)
        local filename = path:lower()
        return not not matcher:match(filename)
    end
end