mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 14:14:36 +00:00
max_split -> limit, allow zero limit
This commit is contained in:
parent
e5d00ef3a7
commit
2ef834d538
14
stringx.lua
14
stringx.lua
@ -11,18 +11,18 @@ local stringx = setmetatable({}, {
|
|||||||
})
|
})
|
||||||
|
|
||||||
--split a string on a delimiter into an ordered table
|
--split a string on a delimiter into an ordered table
|
||||||
function stringx.split(self, delim, max_split)
|
function stringx.split(self, delim, limit)
|
||||||
delim = delim or ""
|
delim = delim or ""
|
||||||
max_split = max_split ~= nil and max_split or math.huge
|
limit = (limit ~= nil and limit) or math.huge
|
||||||
|
|
||||||
assert:type(self, "string", "stringx.split - self", 1)
|
assert:type(self, "string", "stringx.split - self", 1)
|
||||||
assert:type(delim, "string", "stringx.split - delim", 1)
|
assert:type(delim, "string", "stringx.split - delim", 1)
|
||||||
assert:type(max_split, "number", "stringx.split - max_split", 1)
|
assert:type(limit, "number", "stringx.split - limit", 1)
|
||||||
|
|
||||||
if max_split then
|
if limit then
|
||||||
assert(max_split > 0, "max_split must be non-zero and positive!")
|
assert(limit >= 0, "max_split must be positive!")
|
||||||
end
|
end
|
||||||
|
|
||||||
--we try to create as little garbage as possible!
|
--we try to create as little garbage as possible!
|
||||||
--only one table to contain the result, plus the split strings.
|
--only one table to contain the result, plus the split strings.
|
||||||
--so we do two passes, and work with the bytes underlying the string
|
--so we do two passes, and work with the bytes underlying the string
|
||||||
@ -53,7 +53,7 @@ function stringx.split(self, delim, max_split)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if has_whole_delim then
|
if has_whole_delim then
|
||||||
if #res < max_split then
|
if #res < limit then
|
||||||
table.insert(res, i)
|
table.insert(res, i)
|
||||||
else
|
else
|
||||||
break
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user