forked from etternagame/etterna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextend_table.lua
39 lines (32 loc) · 996 Bytes
/
extend_table.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
-- Utilities for better table manipulation
-- split a string into a table
-- http://lua-users.org/wiki/SplitJoin (but with error messages)
function string.split(self, sSeparator, nMax, bRegexp)
assert(sSeparator ~= "", "empty separator is not allowed.")
assert(nMax == nil or nMax >= 1, "max must be a positive number.")
local aRecord = {}
if self:len() > 0 then
local bPlain = not bRegexp
nMax = nMax or -1
local nField = 1
nStart = 1
local nFirst, nLast = self:find(sSeparator, nStart, bPlain)
while nFirst and nMax ~= 0 do
aRecord[nField] = self:sub(nStart, nFirst - 1)
nField = nField + 1
nStart = nLast + 1
nFirst, nLast = self:find(sSeparator, nStart, bPlain)
nMax = nMax - 1
end
aRecord[nField] = self:sub(nStart)
end
return aRecord
end
-- table.concat alias for convenience.
table.join = table.concat
-- insert multiple elements into a table at once
function table.push(self, ...)
for _, v in ipairs({...}) do
table.insert(self, v)
end
end