Comparison

util/serialization.lua @ 3745:87f6eabd90c9

util.serialization: Proper serialization of Infinity, -Infinity and NaN.
author Waqas Hussain <waqas20@gmail.com>
date Mon, 13 Dec 2010 20:45:08 +0500
parent 3736:73399dd525e8
child 5021:85b2689dbcfe
comparison
equal deleted inserted replaced
3744:ed76b64da9d1 3745:87f6eabd90c9
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");