mirror of
https://github.com/pkulchenko/serpent.git
synced 2024-11-21 23:24:24 +00:00
Fixed order of elements in the array part with sortkeys=true
(fixes #13).
This commit is contained in:
parent
06873d12cd
commit
7d56d50a9c
@ -1,4 +1,4 @@
|
|||||||
local n, v = "serpent", 0.26 -- (C) 2012-13 Paul Kulchenko; MIT License
|
local n, v = "serpent", 0.261 -- (C) 2012-13 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}
|
||||||
@ -30,13 +30,13 @@ local function s(t, opts)
|
|||||||
local plain = type(n) == "string" and n:match("^[%l%u_][%w_]*$") and not keyword[n]
|
local plain = type(n) == "string" and n:match("^[%l%u_][%w_]*$") and not keyword[n]
|
||||||
local safe = plain and n or '['..safestr(n)..']'
|
local safe = plain and n or '['..safestr(n)..']'
|
||||||
return (path or '')..(plain and path and '.' or '')..safe, safe end
|
return (path or '')..(plain and path and '.' or '')..safe, safe end
|
||||||
local alphanumsort = type(opts.sortkeys) == 'function' and opts.sortkeys or function(k, o, n) -- k=keys, o=originaltable, n=padding
|
local alphanumsort = type(opts.sortkeys) == 'function' and opts.sortkeys or function(k, o, n) -- k=keys, o=originaltable, n=padding
|
||||||
local maxn, to = tonumber(n) or 12, {number = 'a', string = 'b'}
|
local maxn, to = tonumber(n) or 12, {number = 'a', string = 'b'}
|
||||||
local function padnum(d) return ("%0"..maxn.."d"):format(d) end
|
local function padnum(d) return ("%0"..maxn.."d"):format(d) end
|
||||||
table.sort(k, function(a,b)
|
table.sort(k, function(a,b)
|
||||||
-- sort numeric keys first: k[key] is non-nil for numeric keys
|
-- sort numeric keys first: `o[key] == key and type(key) == 'number'` is true for numeric keys
|
||||||
return (k[a] and 0 or to[type(a)] or 'z')..(tostring(a):gsub("%d+",padnum))
|
return (o[a] == a and type(a) == 'number' and 0 or to[type(a)] or 'z')..(tostring(a):gsub("%d+",padnum))
|
||||||
< (k[b] and 0 or to[type(b)] or 'z')..(tostring(b):gsub("%d+",padnum)) end) end
|
< (o[b] == b and type(b) == 'number' and 0 or to[type(b)] or 'z')..(tostring(b):gsub("%d+",padnum)) end) end
|
||||||
local function val2str(t, name, indent, insref, path, plainindex, level)
|
local function val2str(t, name, indent, insref, path, plainindex, level)
|
||||||
local ttype, level, mt = type(t), (level or 0), getmetatable(t)
|
local ttype, level, mt = type(t), (level or 0), getmetatable(t)
|
||||||
local spath, sname = safename(path, name)
|
local spath, sname = safename(path, name)
|
||||||
|
10
t/test.lua
10
t/test.lua
@ -285,6 +285,16 @@ do
|
|||||||
"deserialization of unsafe values disabled: failed")
|
"deserialization of unsafe values disabled: failed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do
|
||||||
|
local a = {1, 2, 3, 4, [false] = 0, [true] = 0}
|
||||||
|
local f = assert(loadstring('return '..serpent.line(a)),
|
||||||
|
"serializing table with numerical and boolean keys: failed")
|
||||||
|
local _a = f()
|
||||||
|
assert(#_a == #a, "table with array and hash part has the right number of elements: failed")
|
||||||
|
assert(_a[3] == a[3], "table with array and hash parts has the right order of elements 1/2: failed")
|
||||||
|
assert(_a[4] == a[4], "table with array and hash parts has the right order of elements 2/2: failed")
|
||||||
|
end
|
||||||
|
|
||||||
print("All tests passed.")
|
print("All tests passed.")
|
||||||
|
|
||||||
do
|
do
|
||||||
|
Loading…
Reference in New Issue
Block a user