mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
Add text.spliterator
This commit is contained in:
parent
a5c49754d9
commit
e648775cae
21
text.lua
21
text.lua
@ -45,6 +45,27 @@ 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)
|
||||||
|
local last_delim_end = 0
|
||||||
|
return function()
|
||||||
|
if last_delim_end >= #str then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local delim_start, delim_end = str:find(delim, last_delim_end + 1, plain)
|
||||||
|
local substr
|
||||||
|
if delim_start then
|
||||||
|
substr = str:sub(last_delim_end + 1, delim_start - 1)
|
||||||
|
else
|
||||||
|
substr = str:sub(last_delim_end + 1)
|
||||||
|
end
|
||||||
|
last_delim_end = delim_end or #str
|
||||||
|
return substr
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function split(text, delimiter, limit, plain)
|
function split(text, delimiter, limit, plain)
|
||||||
limit = limit or math.huge
|
limit = limit or math.huge
|
||||||
local parts = {}
|
local parts = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user