-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.lua
90 lines (79 loc) · 1.84 KB
/
Utils.lua
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
function want(name)
local out;
if xpcall(
function() out = require(name) end,
function(e) out = e end)
then
return out -- success
else
return nil, out -- error
end
end
function string.split(s, sep)
local fields = {}
local sep = sep or " "
local pattern = string.format("([^%s]+)", sep)
s:gsub(pattern, function(c) fields[#fields + 1] = c end)
return fields
end
do
local matches =
{
["^"] = "%^";
["$"] = "%$";
["("] = "%(";
[")"] = "%)";
["%"] = "%%";
["."] = "%.";
["["] = "%[";
["]"] = "%]";
["*"] = "%*";
["+"] = "%+";
["-"] = "%-";
["?"] = "%?";
["\0"] = "%z";
}
string.escape = function(s)
return s:gsub(".", matches)
end
end
function string.beginswith(s, t)
-- TODO better? s:sub(1, #t) == t
return s:find("^"..t:escape()) ~= nil
end
function string.endswith(s, t)
-- TODO better? s:sub(-#t) == t
return s:find(t:escape().."$") ~= nil
end
function string.replace(str, this, that)
-- return str:gsub(regexEscape(this), that:gsub("%%", "%%%%")) -- For some unknown reason this isn't working
local t = that:gsub("%%", "%%%%") -- only % needs to be escaped for 'that'
return str:gsub(this:escape(), t)
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
function table.concat(t, ...)
for i,v in ipairs({...}) do
for ii,vv in ipairs(v) do
t[#t+1] = vv
end
end
return t
end
-- TODO can I make beginswith a parameter?
function table.concat_if(t, s, ...)
for i,v in ipairs({...}) do
for ii,vv in ipairs(v) do
if vv:lower():beginswith(s) then
t[#t+1] = vv
end
end
end
return t
end