From d96d015887e6fcbe1ca8defcec44cb794421785d Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 21 Sep 2013 13:31:56 +0200 Subject: sync with Context as of 2013-09-21 --- lualibs-util-tab.lua | 53 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'lualibs-util-tab.lua') diff --git a/lualibs-util-tab.lua b/lualibs-util-tab.lua index f18c719..ae44269 100644 --- a/lualibs-util-tab.lua +++ b/lualibs-util-tab.lua @@ -10,13 +10,14 @@ utilities = utilities or {} utilities.tables = utilities.tables or { } local tables = utilities.tables -local format, gmatch, gsub = string.format, string.gmatch, string.gsub +local format, gmatch, gsub, sub = string.format, string.gmatch, string.gsub, string.sub local concat, insert, remove = table.concat, table.insert, table.remove local setmetatable, getmetatable, tonumber, tostring = setmetatable, getmetatable, tonumber, tostring local type, next, rawset, tonumber, tostring, load, select = type, next, rawset, tonumber, tostring, load, select local lpegmatch, P, Cs, Cc = lpeg.match, lpeg.P, lpeg.Cs, lpeg.Cc local sortedkeys, sortedpairs = table.sortedkeys, table.sortedpairs local formatters = string.formatters +local utftoeight = utf.toeight local splitter = lpeg.tsplitat(".") @@ -300,11 +301,20 @@ local f_hashed_number = formatters["[%q]=%s,"] local f_hashed_boolean = formatters["[%q]=%l,"] local f_hashed_table = formatters["[%q]="] -local f_indexed_string = formatters["%q,"] -local f_indexed_number = formatters["%s,"] -local f_indexed_boolean = formatters["%l,"] +local f_indexed_string = formatters["[%s]=%q,"] +local f_indexed_number = formatters["[%s]=%s,"] +local f_indexed_boolean = formatters["[%s]=%l,"] +local f_indexed_table = formatters["[%s]="] -function table.fastserialize(t,prefix) -- so prefix should contain the = | not sorted +local f_ordered_string = formatters["%q,"] +local f_ordered_number = formatters["%s,"] +local f_ordered_boolean = formatters["%l,"] + +function table.fastserialize(t,prefix) + + -- prefix should contain the = + -- not sorted + -- only number and string indices (currently) local r = { prefix or "return" } local m = 1 @@ -314,21 +324,37 @@ function table.fastserialize(t,prefix) -- so prefix should contain the = | not s m = m + 1 r[m] = "{" if n > 0 then - for i=1,n do + for i=0,n do local v = t[i] local tv = type(v) if tv == "string" then - m = m + 1 r[m] = f_indexed_string(v) + m = m + 1 r[m] = f_ordered_string(v) elseif tv == "number" then - m = m + 1 r[m] = f_indexed_number(v) + m = m + 1 r[m] = f_ordered_number(v) elseif tv == "table" then fastserialize(v) elseif tv == "boolean" then - m = m + 1 r[m] = f_indexed_boolean(v) + m = m + 1 r[m] = f_ordered_boolean(v) end end - else - for k, v in next, t do + end + for k, v in next, t do + local tk = type(k) + if tk == "number" then + if k > n or k < 0 then + local tv = type(v) + if tv == "string" then + m = m + 1 r[m] = f_indexed_string(k,v) + elseif tv == "number" then + m = m + 1 r[m] = f_indexed_number(k,v) + elseif tv == "table" then + m = m + 1 r[m] = f_indexed_table(k) + fastserialize(v) + elseif tv == "boolean" then + m = m + 1 r[m] = f_indexed_boolean(k,v) + end + end + else local tv = type(v) if tv == "string" then m = m + 1 r[m] = f_hashed_string(k,v) @@ -375,6 +401,7 @@ function table.load(filename,loader) if filename then local t = (loader or io.loaddata)(filename) if t and t ~= "" then + local t = utftoeight(t) t = load(t) if type(t) == "function" then t = t() @@ -518,6 +545,10 @@ local spaces = utilities.strings.newrepeater(" ") local serialize = table.serialize -- the extensive one, the one we started with +-- there is still room for optimization: index run, key run, but i need to check with the +-- latest lua for the value of #n (with holes) .. anyway for tracing purposes we want +-- indices / keys being sorted, so it will never be real fast + function table.serialize(root,name,specification) if type(specification) == "table" then -- cgit v1.2.3