-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.lua
More file actions
120 lines (105 loc) · 2.41 KB
/
utils.lua
File metadata and controls
120 lines (105 loc) · 2.41 KB
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
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
function M.utf8to32(utf8str)
local seq, val = 0, nil
for i = 1, #utf8str do
local c = string.byte(utf8str, i)
if seq == 0 then
seq = c < 0x80 and 1 or c < 0xE0 and 2 or c < 0xF0 and 3 or
c < 0xF8 and 4 or c < 0xFC and 5 or c < 0xFE and 6 or
error("invalid UTF-8 character sequence")
val = bit.band(c, 2^(8-seq) - 1)
else
val = bit.bor(bit.lshift(val, 6), bit.band(c, 0x3F))
end
seq = seq - 1
end
return val
end
return M