-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.lua
103 lines (89 loc) · 1.94 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
91
92
93
94
95
96
97
98
99
100
101
102
103
local M = {}
function M.getTime (iso8601)
local y, m, d, H, M, S = iso8601:match('(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)')
return os.time{ year = y, month = m, day = d, hour = H, min = M, sec = S } + 28800
end
function M.hasValue (tab, val)
if not tab then
return false
end
for _, value in ipairs(tab) do
if value == val then return true end
end
return false
end
function M.subrange(t, first, last)
local sub = {}
for i=first,last do
sub[#sub + 1] = t[i]
end
return sub
end
-- only for arrays
table.filter = function(t, filterIter)
local out = {}
for i, v in ipairs(t) do
if filterIter(v, i, t) then
table.insert(out, v)
end
end
return out
end
table.map = function(t, mapFunc)
local out = {}
for i, v in ipairs(t) do
out[i] = mapFunc(v, i, t)
end
return out
end
table.reverse = function (t)
local reversedTable = {}
local itemCount = #t
for k, v in ipairs(t) do
reversedTable[itemCount + 1 - k] = v
end
return reversedTable
end
function M.tableConcat(t1, t2)
for i=1, #t2 do
t1[#t1+1] = t2[i]
end
return t1
end
function string.split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = '(.-)' .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= '' then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
--- URL-encode a string according to RFC 3986.
-- Based on http://lua-users.org/wiki/StringRecipes
-- @param str the string to encode
-- @return the URL-encoded string
function M.urlEncode(str)
if str then
str = string.gsub(str, '\n', '\r\n')
str =
string.gsub(
str,
'([^%w:/%-%_%.%~])',
function(c)
return string.format('%%%02X', string.byte(c))
end
)
end
return str
end
return M