mirror of
https://github.com/pkulchenko/serpent.git
synced 2024-11-21 23:24:24 +00:00
Added maxlength
option to limit the space taken by table elements.
This commit is contained in:
parent
7f46cfda57
commit
5b5f694082
@ -73,6 +73,7 @@ Similar to `pcall` and `loadstring` calls, `load` returns status as the first va
|
|||||||
* nohuge (true/False) -- disable checking numbers against undefined and huge values
|
* nohuge (true/False) -- disable checking numbers against undefined and huge values
|
||||||
* maxlevel (number) -- specify max level up to which to expand nested tables
|
* maxlevel (number) -- specify max level up to which to expand nested tables
|
||||||
* maxnum (number) -- specify max number of elements in a table
|
* maxnum (number) -- specify max number of elements in a table
|
||||||
|
* maxlength (number) -- specify max length for all table elements
|
||||||
* numformat (string; "%.17g") -- specify format for numeric values (shortest possible round-trippable double)
|
* numformat (string; "%.17g") -- specify format for numeric values (shortest possible round-trippable double)
|
||||||
* valignore (table) -- allows to specify a list of values to ignore (as keys)
|
* valignore (table) -- allows to specify a list of values to ignore (as keys)
|
||||||
* keyallow (table) -- allows to specify the list of keys to be serialized. Any keys not in this list are not included in final output (as keys)
|
* keyallow (table) -- allows to specify the list of keys to be serialized. Any keys not in this list are not included in final output (as keys)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
local n, v = "serpent", 0.287 -- (C) 2012-17 Paul Kulchenko; MIT License
|
local n, v = "serpent", 0.288 -- (C) 2012-17 Paul Kulchenko; MIT License
|
||||||
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
|
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
|
||||||
local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
|
local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
|
||||||
local badtype = {thread = true, userdata = true, cdata = true}
|
local badtype = {thread = true, userdata = true, cdata = true}
|
||||||
@ -15,6 +15,7 @@ local function s(t, opts)
|
|||||||
local name, indent, fatal, maxnum = opts.name, opts.indent, opts.fatal, opts.maxnum
|
local name, indent, fatal, maxnum = opts.name, opts.indent, opts.fatal, opts.maxnum
|
||||||
local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
|
local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
|
||||||
local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge)
|
local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge)
|
||||||
|
local maxlen = tonumber(opts.maxlength)
|
||||||
local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge)
|
local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge)
|
||||||
local numformat = opts.numformat or "%.17g"
|
local numformat = opts.numformat or "%.17g"
|
||||||
local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0
|
local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0
|
||||||
@ -59,9 +60,10 @@ local function s(t, opts)
|
|||||||
end -- new value falls through to be serialized
|
end -- new value falls through to be serialized
|
||||||
end
|
end
|
||||||
if ttype == "table" then
|
if ttype == "table" then
|
||||||
if level >= maxl then return tag..'{}'..comment('max', level) end
|
if level >= maxl then return tag..'{}'..comment('maxlvl', level) end
|
||||||
seen[t] = insref or spath
|
seen[t] = insref or spath
|
||||||
if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
|
if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
|
||||||
|
if maxlen and maxlen < 0 then return tag..'{}'..comment('maxlen', level) end
|
||||||
local maxn, o, out = math.min(#t, maxnum or #t), {}, {}
|
local maxn, o, out = math.min(#t, maxnum or #t), {}, {}
|
||||||
for key = 1, maxn do o[key] = key end
|
for key = 1, maxn do o[key] = key end
|
||||||
if not maxnum or #o < maxnum then
|
if not maxnum or #o < maxnum then
|
||||||
@ -87,6 +89,10 @@ local function s(t, opts)
|
|||||||
sref[#sref] = path..space..'='..space..tostring(seen[value] or val2str(value,nil,indent,path))
|
sref[#sref] = path..space..'='..space..tostring(seen[value] or val2str(value,nil,indent,path))
|
||||||
else
|
else
|
||||||
out[#out+1] = val2str(value,key,indent,insref,seen[t],plainindex,level+1)
|
out[#out+1] = val2str(value,key,indent,insref,seen[t],plainindex,level+1)
|
||||||
|
if maxlen then
|
||||||
|
maxlen = maxlen - #out[#out]
|
||||||
|
if maxlen < 0 then break end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local prefix = string.rep(indent or '', level)
|
local prefix = string.rep(indent or '', level)
|
||||||
|
Loading…
Reference in New Issue
Block a user