[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Saving a table in file
- From: Tony Finch <dot@...>
- Date: Mon, 8 Mar 2010 16:07:07 +0000
On Sun, 7 Mar 2010, Doug Rogers wrote:
>
> Why not just use two named functions for the API?
You only need one function, since you can use loadstring() to deserialize.
Tony.
--
f.anthony.n.finch <dot@dotat.at> http://dotat.at/
GERMAN BIGHT HUMBER: SOUTHWEST 5 TO 7. MODERATE OR ROUGH. SQUALLY SHOWERS.
MODERATE OR GOOD.
local szt = {}
local function char(c) return ("\\%03d"):format(c:byte()) end
local function szstr(s) return ('"%s"'):format(s:gsub("[^ !#-~]", char)) end
local function szfun(f) return "loadstring"..szstr(string.dump(f)) end
local function szany(...) return szt[type(...)](...) end
local function sztbl(t,code,var)
for k,v in pairs(t) do
local ks = szany(k,code,var)
local vs = szany(v,code,var)
code[#code+1] = ("%s[%s]=%s"):format(var[t],ks,vs)
end
return "{}"
end
local function memo(sz)
return function(d,code,var)
if var[d] == nil then
var[1] = var[1] + 1
var[d] = ("_[%d]"):format(var[1])
local index = #code+1
code[index] = "" -- reserve place during recursion
code[index] = ("%s=%s"):format(var[d],sz(d,code,var))
end
return var[d]
end
end
szt["nil"] = tostring
szt["boolean"] = tostring
szt["number"] = tostring
szt["string"] = szstr
szt["function"] = memo(szfun)
szt["table"] = memo(sztbl)
local function serialize(d)
local code = { "local _ = {}" }
local value = szany(d,code,{0})
code[#code+1] = "return "..value
if #code == 2 then return code[2]
else return table.concat(code, "\n")
end
end
return serialize