Update split.lua

This commit is contained in:
VorTechnix 2024-09-12 09:54:40 -07:00
parent 005615df02
commit e24997dd80
No known key found for this signature in database
GPG Key ID: 091E91A69545D5BA

@ -1,89 +1,90 @@
-- Licence: GPLv2 (MPL-2.0 is compatible, so we can use this here) -- Licence: GPLv2 (MPL-2.0 is compatible, so we can use this here)
-- Source: https://stackoverflow.com/a/43582076/1460422 -- Source: https://stackoverflow.com/a/43582076/1460422
-- gsplit: iterate over substrings in a string separated by a pattern -- gsplit: iterate over substrings in a string separated by a pattern
-- --
-- Parameters: -- Parameters:
-- text (string) - the string to iterate over -- text (string) - the string to iterate over
-- pattern (string) - the separator pattern -- pattern (string) - the separator pattern
-- plain (boolean) - if true (or truthy), pattern is interpreted as a plain -- plain (boolean) - if true (or truthy), pattern is interpreted as a plain
-- string, not a Lua pattern -- string, not a Lua pattern
-- --
-- Returns: iterator -- Returns: iterator
-- --
-- Usage: -- Usage:
-- for substr in gsplit(text, pattern, plain) do -- for substr in gsplit(text, pattern, plain) do
-- doSomething(substr) -- doSomething(substr)
-- end -- end
local function gsplit(text, pattern, plain) local function gsplit(text, pattern, plain)
local splitStart, length = 1, #text local splitStart, length = 1, #text
return function () return function ()
if splitStart then if splitStart then
local sepStart, sepEnd = string.find(text, pattern, splitStart, plain) local sepStart, sepEnd = string.find(text, pattern, splitStart, plain)
local ret local ret
if not sepStart then if not sepStart then
ret = string.sub(text, splitStart) ret = string.sub(text, splitStart)
splitStart = nil splitStart = nil
elseif sepEnd < sepStart then elseif sepEnd < sepStart then
-- Empty separator! -- Empty separator!
ret = string.sub(text, splitStart, sepStart) ret = string.sub(text, splitStart, sepStart)
if sepStart < length then if sepStart < length then
splitStart = sepStart + 1 splitStart = sepStart + 1
else else
splitStart = nil splitStart = nil
end end
else else
ret = sepStart > splitStart and string.sub(text, splitStart, sepStart - 1) or '' ret = sepStart > splitStart and string.sub(text, splitStart, sepStart - 1) or ''
splitStart = sepEnd + 1 splitStart = sepEnd + 1
end end
return ret return ret
end end
end end
end end
--- Split a string into substrings separated by a pattern. -- Deprecated --- Split a string into substrings separated by a pattern. -- Deprecated
-- @param text string The string to iterate over -- @param text string The string to iterate over
-- @param pattern string The separator pattern -- @param pattern string The separator pattern
-- @param plain boolean If true (or truthy), pattern is interpreted as a -- @param plain boolean If true (or truthy), pattern is interpreted as a
-- plain string, not a Lua pattern -- plain string, not a Lua pattern
-- @returns table A sequence table containing the substrings -- @returns table A sequence table containing the substrings
local function dsplit(text, pattern, plain) local function dsplit(text, pattern, plain)
local ret = {} local ret = {}
for match in gsplit(text, pattern, plain) do for match in gsplit(text, pattern, plain) do
table.insert(ret, match) table.insert(ret, match)
end end
return ret return ret
end end
--- Split a string into substrings separated by a pattern. --- Split a string into substrings separated by a pattern.
-- @param str string The string to iterate over -- @param str string The string to iterate over
-- @param dlm string The delimiter (separator) pattern -- @param dlm string The delimiter (separator) pattern (default: "%s+")
-- @param plain boolean If true (or truthy), pattern is interpreted as a -- @param plain boolean If true (or truthy), pattern is interpreted as a
-- plain string, not a Lua pattern -- plain string, not a Lua pattern
-- @returns table A sequence table containing the substrings -- @returns table A sequence table containing the substrings
local function split(str,dlm,plain) local function split(str,dlm,plain)
local pos, ret = 0, {} if not dlm then dlm = "%s+" end
local ins, i = str:find(dlm,pos,plain) local pos, ret = 0, {}
-- "if plain" shaves off some time in the while statement local ins, i = str:find(dlm,pos,plain)
if plain then -- "if plain" shaves off some time in the while statement
while ins do if plain then
table.insert(ret,str:sub(pos,ins - 1)) while ins do
pos = ins + #dlm table.insert(ret,str:sub(pos,ins - 1))
ins = str:find(dlm,pos,true) pos = ins + #dlm
end ins = str:find(dlm,pos,true)
else end
while ins do else
table.insert(ret,str:sub(pos,ins - 1)) while ins do
pos = i + 1 table.insert(ret,str:sub(pos,ins - 1))
ins, i = str:find(dlm,pos) pos = i + 1
end ins, i = str:find(dlm,pos)
end end
-- print(pos..","..#str) end
if str:sub(pos,#str) ~= "" then -- print(pos..","..#str)
table.insert(ret,str:sub(pos,#str)) if str:sub(pos,#str) ~= "" then
end table.insert(ret,str:sub(pos,#str))
return ret end
end return ret
end
return split return split