Create common.serialize function
This commit is contained in:
@@ -164,6 +164,20 @@ function common.bench(name, fn, ...)
|
||||
end
|
||||
|
||||
|
||||
function common.serialize(val)
|
||||
if type(val) == "string" then
|
||||
return string.format("%q", val)
|
||||
elseif type(val) == "table" then
|
||||
local t = {}
|
||||
for k, v in pairs(val) do
|
||||
table.insert(t, "[" .. common.serialize(k) .. "]=" .. common.serialize(v))
|
||||
end
|
||||
return "{" .. table.concat(t, ",") .. "}"
|
||||
end
|
||||
return tostring(val)
|
||||
end
|
||||
|
||||
|
||||
function common.home_encode(text)
|
||||
if HOME then
|
||||
local n = #HOME
|
||||
|
||||
+1
-10
@@ -11,15 +11,6 @@ local Doc
|
||||
|
||||
local core = {}
|
||||
|
||||
local function table_serialize(t)
|
||||
local ls = {"{"}
|
||||
for i = 1, #t do
|
||||
ls[#ls + 1] = string.format(" %q,", t[i])
|
||||
end
|
||||
ls[#ls + 1] = "}"
|
||||
return table.concat(ls, "\n")
|
||||
end
|
||||
|
||||
local function load_projects()
|
||||
local ok, t = pcall(dofile, USERDIR .. "/recent_projects.lua")
|
||||
core.recent_projects = (ok and t or {})
|
||||
@@ -42,7 +33,7 @@ end
|
||||
local function save_projects()
|
||||
local fp = io.open(USERDIR .. "/recent_projects.lua", "w")
|
||||
if fp then
|
||||
fp:write("return ", table_serialize(core.recent_projects), "\n")
|
||||
fp:write("return ", common.serialize(core.recent_projects), "\n")
|
||||
fp:close()
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user