From af07a158a82fabfe9dacacf8e0576c8b494cc6d4 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Fri, 23 Jul 2021 18:05:54 -0700 Subject: [PATCH] shaved some milliseconds off split --- worldeditadditions/utils/strings/split.lua | 31 ++++++++++++++-------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/worldeditadditions/utils/strings/split.lua b/worldeditadditions/utils/strings/split.lua index 73af851..c271aef 100644 --- a/worldeditadditions/utils/strings/split.lua +++ b/worldeditadditions/utils/strings/split.lua @@ -42,13 +42,13 @@ 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. -- Deprecated -- @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.dsplit(text, pattern, plain) local ret = {} for match in worldeditadditions.gsplit(text, pattern, plain) do table.insert(ret, match) @@ -62,15 +62,24 @@ end -- @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.fsplit (str,dlm,plain) - local pos, ins = 0, 0 - local ret = {} - 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) +function worldeditadditions.split (str,dlm,plain) + local pos, ret = 0, {} + local ins, i = str:find(dlm,pos,plain) + -- if plain shaves off some time in the while statement + if plain then + while ins do + table.insert(ret,str:sub(pos,ins - 1)) + pos = ins + #dlm + ins = str:find(dlm,pos,true) + end + else + while ins do + table.insert(ret,str:sub(pos,ins - 1)) + pos = i + 1 + ins, i = str:find(dlm,pos) + end + end + -- print(pos..","..#str) if str:sub(pos,#str) ~= "" then table.insert(ret,str:sub(pos,#str)) end