mirror of
https://github.com/gvx/Ser.git
synced 2024-11-26 01:44:22 +00:00
Strip whitespace where possible
This can save a lot of space (in one case bringing the serialized size down from 11.5MB to 8.6MB).
This commit is contained in:
parent
8cf0f0df6e
commit
599c25f154
20
ser.lua
20
ser.lua
@ -36,9 +36,9 @@ local kw = {['and'] = true, ['break'] = true, ['do'] = true, ['else'] = true,
|
||||
['until'] = true, ['while'] = true}
|
||||
local function write_key_value_pair(k, v, memo, rev_memo, name)
|
||||
if type(k) == 'string' and k:match '^[_%a][_%w]*$' and not kw[k] then
|
||||
return (name and name .. '.' or '') .. k ..' = ' .. write(v, memo, rev_memo)
|
||||
return (name and name .. '.' or '') .. k ..'=' .. write(v, memo, rev_memo)
|
||||
else
|
||||
return (name or '') .. '[' .. write(k, memo, rev_memo) .. '] = ' .. write(v, memo, rev_memo)
|
||||
return (name or '') .. '[' .. write(k, memo, rev_memo) .. ']=' .. write(v, memo, rev_memo)
|
||||
end
|
||||
end
|
||||
|
||||
@ -53,19 +53,19 @@ end
|
||||
|
||||
local function write_table_ex(t, memo, rev_memo, srefs, name)
|
||||
if type(t) == 'function' then
|
||||
return '_[' .. name .. '] = loadstring ' .. make_safe(dump(t))
|
||||
return '_[' .. name .. ']=loadstring' .. make_safe(dump(t))
|
||||
end
|
||||
local m = {'_[', name, '] = {'}
|
||||
local m = {'_[', name, ']={'}
|
||||
local mi = 3
|
||||
for i = 1, #t do -- don't use ipairs here, we need the gaps
|
||||
local v = t[i]
|
||||
if v == t or is_cyclic(memo, v, t) then
|
||||
srefs[#srefs + 1] = {name, i, v}
|
||||
m[mi + 1] = 'nil, '
|
||||
m[mi + 1] = 'nil,'
|
||||
mi = mi + 1
|
||||
else
|
||||
m[mi + 1] = write(v, memo, rev_memo)
|
||||
m[mi + 2] = ', '
|
||||
m[mi + 2] = ','
|
||||
mi = mi + 2
|
||||
end
|
||||
end
|
||||
@ -75,7 +75,7 @@ local function write_table_ex(t, memo, rev_memo, srefs, name)
|
||||
srefs[#srefs + 1] = {name, k, v}
|
||||
else
|
||||
m[mi + 1] = write_key_value_pair(k, v, memo, rev_memo)
|
||||
m[mi + 2] = ', '
|
||||
m[mi + 2] = ','
|
||||
mi = mi + 2
|
||||
end
|
||||
end
|
||||
@ -110,13 +110,13 @@ return function(t)
|
||||
end
|
||||
|
||||
-- phase 4: add something about returning the main table
|
||||
if result[n]:sub(1, 5) == '_[0] ' then
|
||||
result[n] = 'return' .. result[n]:sub(7)
|
||||
if result[n]:sub(1, 5) == '_[0]=' then
|
||||
result[n] = 'return ' .. result[n]:sub(6)
|
||||
else
|
||||
result[n + 1] = 'return _[0]'
|
||||
end
|
||||
|
||||
-- phase 5: just concatenate everything
|
||||
result = concat(result, '\n')
|
||||
return n > 1 and 'local _ = {}\n' .. result or result
|
||||
return n > 1 and 'local _={}\n' .. result or result
|
||||
end
|
||||
|
28
tests.lua
28
tests.lua
@ -36,37 +36,37 @@ case({}, 'return {}', 'empty table')
|
||||
case({true}, 'return {true}', 'simple table')
|
||||
|
||||
case({{}}, [[
|
||||
local _ = {}
|
||||
_[1] = {}
|
||||
local _={}
|
||||
_[1]={}
|
||||
return {_[1]}]], 'empty table within a table')
|
||||
|
||||
local _t = {}
|
||||
_t.self = _t
|
||||
case(_t, [=[local _ = {}
|
||||
_[0] = {}
|
||||
_[0].self = _[0]
|
||||
case(_t, [=[local _={}
|
||||
_[0]={}
|
||||
_[0].self=_[0]
|
||||
return _[0]]=], 'simple cycle')
|
||||
|
||||
case_error({coroutine.create(function()end)}, './ser.lua:27: Trying to serialize unsupported type thread', 'unsupported type')
|
||||
|
||||
case({"a", foo = "bar", ["3f"] = true, _1 = false, ["00"] = 9}, 'return {"a", ["3f"] = true, _1 = false, ["00"] = 9, foo = "bar"}', 'various')
|
||||
case({"a", foo = "bar", ["3f"] = true, _1 = false, ["00"] = 9}, 'return {"a",["3f"]=true,_1=false,["00"]=9,foo="bar"}', 'various')
|
||||
|
||||
case({'\127\230\255\254\128\12\0128\n\31'}, 'return {"\\127\\230\\255\\254\\128\\12\\0128\\n\\31"}', 'non-ASCII or control characters in string value')
|
||||
|
||||
case({['\127\230\255\254\128\12\0128\n\31'] = '\0'}, 'return {["\\127\\230\\255\\254\\128\\12\\0128\\n\\31"] = "\\0"}', 'non-ASCII or control characters in string key')
|
||||
case({['\127\230\255\254\128\12\0128\n\31'] = '\0'}, 'return {["\\127\\230\\255\\254\\128\\12\\0128\\n\\31"]="\\0"}', 'non-ASCII or control characters in string key')
|
||||
|
||||
local x = {}
|
||||
case({x, {x}, x}, [=[
|
||||
local _ = {}
|
||||
_[2] = {}
|
||||
_[1] = {}
|
||||
_[0] = {_[1], _[2], _[1]}
|
||||
_[2][1] = _[1]
|
||||
local _={}
|
||||
_[2]={}
|
||||
_[1]={}
|
||||
_[0]={_[1],_[2],_[1]}
|
||||
_[2][1]=_[1]
|
||||
return _[0]]=], 'repeated table')
|
||||
|
||||
case({['end'] = true, ['false'] = false}, 'return {["false"] = false, ["end"] = true}', 'keywords as table keys')
|
||||
case({['end'] = true, ['false'] = false}, 'return {["false"]=false,["end"]=true}', 'keywords as table keys')
|
||||
|
||||
case({1/0, -1/0, 0/0}, 'return {1/0, -1/0, 0/0}', 'representation of infinity and NaN')
|
||||
case({1/0, -1/0, 0/0}, 'return {1/0,-1/0,0/0}', 'representation of infinity and NaN')
|
||||
|
||||
print(failed .. ' tests failed')
|
||||
print(succeeded .. ' tests succeeded')
|
||||
|
Loading…
Reference in New Issue
Block a user