mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-12-23 03:55:01 +00:00
new split function
This commit is contained in:
parent
4b35142044
commit
ce1ba27728
1 changed files with 25 additions and 4 deletions
|
@ -42,16 +42,37 @@ function worldeditadditions.gsplit(text, pattern, plain)
|
|||
end
|
||||
|
||||
|
||||
-- Split a string into substrings separated by a pattern.
|
||||
--- Split a string into substrings separated by a pattern.
|
||||
-- @param text string The string to iterate over
|
||||
-- @param pattern string The separator pattern
|
||||
-- @param plain boolean If true (or truthy), pattern is interpreted as a
|
||||
-- plain string, not a Lua pattern
|
||||
-- @returns table A sequence table containing the substrings
|
||||
function worldeditadditions.split(text, pattern, plain)
|
||||
-- function worldeditadditions.split(text, pattern, plain)
|
||||
-- local ret = {}
|
||||
-- for match in worldeditadditions.gsplit(text, pattern, plain) do
|
||||
-- table.insert(ret, match)
|
||||
-- end
|
||||
-- return ret
|
||||
-- end
|
||||
|
||||
--- Split a string into substrings separated by a pattern.
|
||||
-- @param str string The string to iterate over
|
||||
-- @param dlm string The delimiter (separator) pattern
|
||||
-- @param plain boolean If true (or truthy), pattern is interpreted as a
|
||||
-- plain string, not a Lua pattern
|
||||
-- @returns table A sequence table containing the substrings
|
||||
function worldeditadditions.split (str,dlm,plain)
|
||||
local pos, ins = 0, 0
|
||||
local ret = {}
|
||||
for match in worldeditadditions.gsplit(text, pattern, plain) do
|
||||
table.insert(ret, match)
|
||||
repeat
|
||||
ins = str:find(dlm,pos,plain)
|
||||
table.insert(ret,str:sub(pos,ins - 1))
|
||||
pos = ins + #dlm
|
||||
until not str:find(dlm,pos,plain)
|
||||
print(pos..","..#str)
|
||||
if str:sub(pos,#str) ~= "" then
|
||||
table.insert(ret,str:sub(pos,#str))
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue