Fixed an issue missing numerical keys (fixes #8).

This commit is contained in:
Paul Kulchenko 2013-06-12 11:04:56 -07:00
parent 2d09171357
commit f7b2a819c0
2 changed files with 13 additions and 2 deletions

View File

@ -1,4 +1,4 @@
local n, v = "serpent", 0.231 -- (C) 2012-13 Paul Kulchenko; MIT License
local n, v = "serpent", 0.24 -- (C) 2012-13 Paul Kulchenko; MIT License
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 badtype = {thread = true, userdata = true, cdata = true}
@ -56,7 +56,7 @@ local function s(t, opts)
if next(t) == nil then return tag..'{}'..comment(t, level) end -- table empty
local maxn, o, out = #t, {}, {}
for key = 1, maxn do table.insert(o, key) end
for key in pairs(t) do if not o[key] then table.insert(o, key) end end
for key in pairs(t) do if not o[key] or key > maxn then table.insert(o, key) end end
if opts.sortkeys then alphanumsort(o, t, opts.sortkeys) end
for n, key in ipairs(o) do
local value, ktype, plainindex = t[key], type(key), n <= maxn and not sparse

View File

@ -213,4 +213,15 @@ do
assert(_a.ud.ud == _a.ud.ud.ud, "userdata with __serialize that returns userdata 4: failed")
end
-- test that numerical keys are all present in the serialized table
do
local a = {[4]=1,[5]=1,[6]=1,[7]=1,[8]=1,[9]=1,[10]=1}
local f = assert(loadstring(serpent.dump(a)),
"serializing table with numerical keys: failed")
local _a = f()
for k,v in pairs(a) do
assert(_a[k] == v, "numerical keys are all present: failed")
end
end
print("All tests passed.")