max_split -> limit, allow zero limit

This commit is contained in:
TurtleP 2021-12-26 11:32:43 -05:00
parent e5d00ef3a7
commit 2ef834d538

View File

@ -11,16 +11,16 @@ local stringx = setmetatable({}, {
})
--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 ""
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(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
assert(max_split > 0, "max_split must be non-zero and positive!")
if limit then
assert(limit >= 0, "max_split must be positive!")
end
--we try to create as little garbage as possible!
@ -53,7 +53,7 @@ function stringx.split(self, delim, max_split)
end
end
if has_whole_delim then
if #res < max_split then
if #res < limit then
table.insert(res, i)
else
break