util/serialization.lua

changeset 3865
c6af5de97b37
parent 3856
2cb50432bc9a
equal deleted inserted replaced
3864:345f1d5b7022 3865:c6af5de97b37
26 local indent = function(i) 26 local indent = function(i)
27 return string_rep("\t", i); 27 return string_rep("\t", i);
28 end 28 end
29 local function basicSerialize (o) 29 local function basicSerialize (o)
30 if type(o) == "number" or type(o) == "boolean" then 30 if type(o) == "number" or type(o) == "boolean" then
31 return tostring(o); 31 -- no need to check for NaN, as that's not a valid table index
32 if o == 1/0 then return "(1/0)";
33 elseif o == -1/0 then return "(-1/0)";
34 else return tostring(o); end
32 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise. 35 else -- assume it is a string -- FIXME make sure it's a string. throw an error otherwise.
33 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n")); 36 return (("%q"):format(tostring(o)):gsub("\\\n", "\\n"));
34 end 37 end
35 end 38 end
36 local function _simplesave(o, ind, t, func) 39 local function _simplesave(o, ind, t, func)
37 if type(o) == "number" then 40 if type(o) == "number" then
38 func(t, tostring(o)); 41 if o ~= o then func(t, "(0/0)");
42 elseif o == 1/0 then func(t, "(1/0)");
43 elseif o == -1/0 then func(t, "(-1/0)");
44 else func(t, tostring(o)); end
39 elseif type(o) == "string" then 45 elseif type(o) == "string" then
40 func(t, (("%q"):format(o):gsub("\\\n", "\\n"))); 46 func(t, (("%q"):format(o):gsub("\\\n", "\\n")));
41 elseif type(o) == "table" then 47 elseif type(o) == "table" then
42 if next(o) ~= nil then 48 if next(o) ~= nil then
43 func(t, "{\n"); 49 func(t, "{\n");

mercurial