Fix split function
This commit is contained in:
parent
7a96882dbb
commit
5e15c6d011
12
util.lua
12
util.lua
@ -59,26 +59,26 @@ end
|
|||||||
|
|
||||||
-- Python-style version from http://lua-users.org/wiki/SplitJoin
|
-- Python-style version from http://lua-users.org/wiki/SplitJoin
|
||||||
-- Why is this not in the standard Lua library?!
|
-- Why is this not in the standard Lua library?!
|
||||||
local function split(sSeparator, nMax, bRegexp)
|
local function split(str, sSeparator, nMax, bRegexp)
|
||||||
assert(sSeparator ~= '')
|
assert(sSeparator ~= '')
|
||||||
assert(nMax == nil or nMax >= 1)
|
assert(nMax == nil or nMax >= 1)
|
||||||
|
|
||||||
local aRecord = {}
|
local aRecord = {}
|
||||||
|
|
||||||
if self:len() > 0 then
|
if str:len() > 0 then
|
||||||
local bPlain = not bRegexp
|
local bPlain = not bRegexp
|
||||||
nMax = nMax or -1
|
nMax = nMax or -1
|
||||||
|
|
||||||
local nField, nStart = 1, 1
|
local nField, nStart = 1, 1
|
||||||
local nFirst,nLast = self:find(sSeparator, nStart, bPlain)
|
local nFirst,nLast = str:find(sSeparator, nStart, bPlain)
|
||||||
while nFirst and nMax ~= 0 do
|
while nFirst and nMax ~= 0 do
|
||||||
aRecord[nField] = self:sub(nStart, nFirst-1)
|
aRecord[nField] = str:sub(nStart, nFirst-1)
|
||||||
nField = nField+1
|
nField = nField+1
|
||||||
nStart = nLast+1
|
nStart = nLast+1
|
||||||
nFirst,nLast = self:find(sSeparator, nStart, bPlain)
|
nFirst,nLast = str:find(sSeparator, nStart, bPlain)
|
||||||
nMax = nMax-1
|
nMax = nMax-1
|
||||||
end
|
end
|
||||||
aRecord[nField] = self:sub(nStart)
|
aRecord[nField] = str:sub(nStart)
|
||||||
end
|
end
|
||||||
|
|
||||||
return aRecord
|
return aRecord
|
||||||
|
Loading…
x
Reference in New Issue
Block a user