text.spliterator: add assert to prevent inf. loop

This commit is contained in:
Lars Mueller 2022-09-28 21:18:49 +02:00
parent 715bf47aba
commit 3d585154eb

@ -1,5 +1,6 @@
-- Localize globals -- Localize globals
local math, modlib, pairs, setmetatable, string, table = math, modlib, pairs, setmetatable, string, table local assert, math, modlib, setmetatable, string, table
= assert, math, modlib, setmetatable, string, table
-- Set environment -- Set environment
local _ENV = {} local _ENV = {}
@ -45,12 +46,14 @@ function hexdump(text)
return table.concat(dump) return table.concat(dump)
end end
-- Iterator of possibly empty substrings between two matches of the delimiter
-- Filter the iterator to exclude empty strings or consider using `:gmatch"[...]+"` instead
function spliterator(str, delim, plain) function spliterator(str, delim, plain)
assert(delim ~= "")
local last_delim_end = 0 local last_delim_end = 0
-- Iterator of possibly empty substrings between two matches of the delimiter
-- To exclude empty strings, filter the iterator or use `:gmatch"[...]+"` instead
return function() return function()
if last_delim_end >= #str then if not last_delim_end then
return return
end end
@ -61,7 +64,7 @@ function spliterator(str, delim, plain)
else else
substr = str:sub(last_delim_end + 1) substr = str:sub(last_delim_end + 1)
end end
last_delim_end = delim_end or #str last_delim_end = delim_end
return substr return substr
end end
end end