Skip to content

Commit 63ddbd8

Browse files
committed
fix: add missing glob module, closes #266
1 parent 2a42aff commit 63ddbd8

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
--(c) 2008-2011 David Manura. Licensed under the same terms as Lua (MIT).
2+
3+
--Permission is hereby granted, free of charge, to any person obtaining a copy
4+
--of this software and associated documentation files (the "Software"), to deal
5+
--in the Software without restriction, including without limitation the rights
6+
--to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
--copies of the Software, and to permit persons to whom the Software is
8+
--furnished to do so, subject to the following conditions:
9+
10+
--The above copyright notice and this permission notice shall be included in
11+
--all copies or substantial portions of the Software.
12+
13+
--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
--IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
--FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
--AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
--LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
--OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
--THE SOFTWARE.
20+
--(end license)
21+
22+
local M = {_TYPE='module', _NAME='globtopattern', _VERSION='0.2.1.20120406'}
23+
24+
function M.globtopattern(g)
25+
-- Some useful references:
26+
-- - apr_fnmatch in Apache APR. For example,
27+
-- http://apr.apache.org/docs/apr/1.3/group__apr__fnmatch.html
28+
-- which cites POSIX 1003.2-1992, section B.6.
29+
30+
local p = "^" -- pattern being built
31+
local i = 0 -- index in g
32+
local c -- char at index i in g.
33+
34+
-- unescape glob char
35+
local function unescape()
36+
if c == '\\' then
37+
i = i + 1; c = g:sub(i,i)
38+
if c == '' then
39+
p = '[^]'
40+
return false
41+
end
42+
end
43+
return true
44+
end
45+
46+
-- escape pattern char
47+
local function escape(c)
48+
return c:match("^%w$") and c or '%' .. c
49+
end
50+
51+
-- Convert tokens at end of charset.
52+
local function charset_end()
53+
while 1 do
54+
if c == '' then
55+
p = '[^]'
56+
return false
57+
elseif c == ']' then
58+
p = p .. ']'
59+
break
60+
else
61+
if not unescape() then break end
62+
local c1 = c
63+
i = i + 1; c = g:sub(i,i)
64+
if c == '' then
65+
p = '[^]'
66+
return false
67+
elseif c == '-' then
68+
i = i + 1; c = g:sub(i,i)
69+
if c == '' then
70+
p = '[^]'
71+
return false
72+
elseif c == ']' then
73+
p = p .. escape(c1) .. '%-]'
74+
break
75+
else
76+
if not unescape() then break end
77+
p = p .. escape(c1) .. '-' .. escape(c)
78+
end
79+
elseif c == ']' then
80+
p = p .. escape(c1) .. ']'
81+
break
82+
else
83+
p = p .. escape(c1)
84+
i = i - 1 -- put back
85+
end
86+
end
87+
i = i + 1; c = g:sub(i,i)
88+
end
89+
return true
90+
end
91+
92+
-- Convert tokens in charset.
93+
local function charset()
94+
i = i + 1; c = g:sub(i,i)
95+
if c == '' or c == ']' then
96+
p = '[^]'
97+
return false
98+
elseif c == '^' or c == '!' then
99+
i = i + 1; c = g:sub(i,i)
100+
if c == ']' then
101+
-- ignored
102+
else
103+
p = p .. '[^'
104+
if not charset_end() then return false end
105+
end
106+
else
107+
p = p .. '['
108+
if not charset_end() then return false end
109+
end
110+
return true
111+
end
112+
113+
-- Convert tokens.
114+
while 1 do
115+
i = i + 1; c = g:sub(i,i)
116+
if c == '' then
117+
p = p .. '$'
118+
break
119+
elseif c == '?' then
120+
p = p .. '.'
121+
elseif c == '*' then
122+
p = p .. '.*'
123+
elseif c == '[' then
124+
if not charset() then break end
125+
elseif c == '\\' then
126+
i = i + 1; c = g:sub(i,i)
127+
if c == '' then
128+
p = p .. '\\$'
129+
break
130+
end
131+
p = p .. escape(c)
132+
else
133+
p = p .. escape(c)
134+
end
135+
end
136+
return p
137+
end
138+
139+
return M

0 commit comments

Comments
 (0)