/usr/share/lua/5.1/luacheck/globbing.lua is in lua-check 0.13.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | local fs = require "luacheck.fs"
local utils = require "luacheck.utils"
-- Only ?, *, ** and simple character classes (with ranges and negation) are supported.
-- Hidden files are not treated specially. Special characters can't be escaped.
local globbing = {}
local cur_dir = fs.current_dir()
local function is_regular_path(glob)
return not glob:find("[*?%[]")
end
local function get_parts(path)
local parts = {}
for part in path:gmatch("[^"..utils.dir_sep.."]+") do
table.insert(parts, part)
end
return parts
end
local function glob_pattern_escaper(c)
return ((c == "*" or c == "?") and "." or "%")..c
end
local function glob_range_escaper(c)
return c == "-" and c or ("%"..c)
end
local function glob_part_to_pattern(glob_part)
local buffer = {"^"}
local i = 1
while i <= #glob_part do
local bracketless
bracketless, i = glob_part:match("([^%[]*)()", i)
table.insert(buffer, (bracketless:gsub("%p", glob_pattern_escaper)))
if glob_part:sub(i, i) == "[" then
table.insert(buffer, "[")
i = i + 1
local first_char = glob_part:sub(i, i)
if first_char == "!" then
table.insert(buffer, "^")
i = i + 1
elseif first_char == "]" then
table.insert(buffer, "%]")
i = i + 1
end
bracketless, i = glob_part:match("([^%]]*)()", i)
if bracketless:sub(1, 1) == "-" then
table.insert(buffer, "%-")
bracketless = bracketless:sub(2)
end
local last_dash = ""
if bracketless:sub(-1) == "-" then
last_dash = "-"
bracketless = bracketless:sub(1, -2)
end
table.insert(buffer, (bracketless:gsub("%p", glob_range_escaper)))
table.insert(buffer, last_dash.."]")
i = i + 1
end
end
table.insert(buffer, "$")
return table.concat(buffer)
end
local function part_match(glob_part, path_part)
return utils.pmatch(path_part, glob_part_to_pattern(glob_part))
end
local function parts_match(glob_parts, glob_i, path_parts, path_i)
local glob_part = glob_parts[glob_i]
if not glob_part then
-- Reached glob end, path matches the glob or its subdirectory.
-- E.g. path "foo/bar/baz/src.lua" matches glob "foo/*/baz".
return true
end
if glob_part == "**" then
-- "**" can consume any number of path parts.
for i = path_i, #path_parts + 1 do
if parts_match(glob_parts, glob_i + 1, path_parts, i) then
return true
end
end
return false
end
local path_part = path_parts[path_i]
return path_part and part_match(glob_part, path_part) and parts_match(glob_parts, glob_i + 1, path_parts, path_i + 1)
end
-- Checks if a path matches a globbing pattern.
function globbing.match(glob, path)
glob = fs.normalize(fs.join(cur_dir, glob))
path = fs.normalize(fs.join(cur_dir, path))
if is_regular_path(glob) then
return fs.is_subpath(glob, path)
end
local glob_base, path_base
glob_base, glob = fs.split_base(glob)
path_base, path = fs.split_base(path)
if glob_base ~= path_base then
return false
end
local glob_parts = get_parts(glob)
local path_parts = get_parts(path)
return parts_match(glob_parts, 1, path_parts, 1)
end
return globbing
|