diff options
Diffstat (limited to 'tex/context/base/util-tab.lua')
-rw-r--r-- | tex/context/base/util-tab.lua | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/tex/context/base/util-tab.lua b/tex/context/base/util-tab.lua index 28a6b8cc5..7dc9e09f8 100644 --- a/tex/context/base/util-tab.lua +++ b/tex/context/base/util-tab.lua @@ -10,10 +10,11 @@ utilities = utilities or {} utilities.tables = utilities.tables or { } local tables = utilities.tables -local format, gmatch, rep = string.format, string.gmatch, string.rep +local format, gmatch, rep, gsub = string.format, string.gmatch, string.rep, string.gsub local concat, insert, remove = table.concat, table.insert, table.remove local setmetatable, getmetatable, tonumber, tostring = setmetatable, getmetatable, tonumber, tostring local type, next, rawset, tonumber = type, next, rawset, tonumber +local lpegmatch = lpeg.match function tables.definetable(target) -- defines undefined tables local composed, t, n = nil, { }, 0 @@ -166,3 +167,45 @@ function tables.encapsulate(core,capsule,protect) } ) end end + +local function serialize(t,r) -- no mixes + r[#r+1] = "{" + local n = #t + if n > 0 then + for i=1,n do + local v = t[i] + local tv = type(v) + if tv == "table" then + serialize(v,r) + elseif tv == "string" then + r[#r+1] = format("%q,",v) + elseif tv == "number" then + r[#r+1] = format("%s,",v) + elseif tv == "boolean" then + r[#r+1] = format("%s,",tostring(v)) + end + end + else + for k, v in next, t do + local tv = type(v) + if tv == "table" then + r[#r+1] = format("[%q]=",k) + serialize(v,r) + elseif tv == "string" then + r[#r+1] = format("[%q]=%q,",k,v) + elseif tv == "number" then + r[#r+1] = format("[%q]=%s,",k,v) + elseif tv == "boolean" then + r[#r+1] = format("[%q]=%s,",k,tostring(v)) + end + end + end + r[#r+1] = "}" + return r +end + +function table.fastserialize(t,prefix) + return concat(serialize(t,{ prefix })) +end + +-- inspect(table.fastserialize { a = 1, b = { 4, { 5, 6 } }, c = { d = 7 } }) |