From a59859b225e41cfd625eb3fccdc7e67988061788 Mon Sep 17 00:00:00 2001 From: payonel Date: Wed, 10 Feb 2016 13:01:46 -0800 Subject: [PATCH] severe text split inefficiency found and fixed --- .../opencomputers/loot/OpenOS/lib/text.lua | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/text.lua b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/text.lua index 163afe255..3e5839bcf 100644 --- a/src/main/resources/assets/opencomputers/loot/OpenOS/lib/text.lua +++ b/src/main/resources/assets/opencomputers/loot/OpenOS/lib/text.lua @@ -118,31 +118,42 @@ function text.removeEscapes(txt) end ------------------------------------------------------------------------------- - -function --[[@delayloaded-start@]] text.split(input, delimiters, dropDelims) +function --[[@delayloaded-start@]] text.split(input, delimiters, dropDelims, di) checkArg(1, input, "string") checkArg(2, delimiters, "table") checkArg(3, dropDelims, "boolean", "nil") + checkArg(4, di, "number", "nil") - local input_table = text.internal.table_view(input) - local delim_table = tx.select(delimiters, function(e,i,t) - return text.internal.table_view(e) - end) - local parts = tx.partition(input_table, function(e,i,t) - local ns, ne = tx.first(t,delim_table,i) - if not ns or dropDelims or ns == i then - return ns, ne - else -- not droping delims and ns>i - return i, ns-1 + if #input == 0 then return {} end + di = di or 1 + local result = {input} + if di > #delimiters then return result end + + local function add(part, index, r, s, e) + local sub = part:sub(s,e) + if #sub == 0 then return index end + local subs = r and text.split(sub,delimiters,dropDelims,r) or {sub} + for i=1,#subs do + table.insert(result, index+i-1, subs[i]) end - end, dropDelims) - return tx.select(parts, function(e,i,t) - local inner = '' - tx.foreach(e, function(ee,ii,tt) - inner = inner..ee - end) - return inner - end) + return index+#subs + end + + local i,d=1,delimiters[di] + while true do + local next = table.remove(result,i) + if not next then break end + local si,ei = next:find(d) + if si and ei and ei~=0 then -- delim found + i=add(next, i, di+1, 1, si-1) + i=dropDelims and i or add(next, i, false, si, ei) + i=add(next, i, di, ei+1) + else + i=add(next, i, di+1, 1, #next) + end + end + + return result end --[[@delayloaded-end@]] -----------------------------------------------------------------------------