From 06a575ed69bd45f26283fabae2800278cadf813c Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Thu, 7 May 2015 22:53:47 -0700 Subject: [PATCH] Added numeric format to preserve floating point precision (closes #17). --- README.md | 1 + src/serpent.lua | 5 +++-- t/test.lua | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec1888a..1de57e9 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ Similar to `pcall` and `loadstring` calls, `load` returns status as the first va * nohuge (true/False) -- disable checking numbers against undefined and huge values * maxlevel (number) -- specify max level up to which to expand nested tables * maxnum (number) -- specify max number of elements in a table +* numformat (string; "%.17g") -- specify format for numeric values (shortest possible round-trippable double) * valignore (table) -- allows to specify a list of values to ignore (as keys) * keyallow (table) -- allows to specify the list of keys to be serialized. Any keys not in this list are not included in final output (as keys) * valtypeignore (table) -- allows to specify a list of value *types* to ignore (as keys) diff --git a/src/serpent.lua b/src/serpent.lua index 3e7f97e..6961a24 100644 --- a/src/serpent.lua +++ b/src/serpent.lua @@ -1,4 +1,4 @@ -local n, v = "serpent", 0.282 -- (C) 2012-15 Paul Kulchenko; MIT License +local n, v = "serpent", 0.283 -- (C) 2012-15 Paul Kulchenko; MIT License local c, d = "Paul Kulchenko", "Lua serializer and pretty printer" local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'} local badtype = {thread = true, userdata = true, cdata = true} @@ -16,11 +16,12 @@ local function s(t, opts) local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge) local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge) + local numformat = opts.numformat or "%.17g" local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0 local function gensym(val) return '_'..(tostring(tostring(val)):gsub("[^%w]",""):gsub("(%d%w+)", -- tostring(val) is needed because __tostring may return a non-string value function(s) if not syms[s] then symn = symn+1; syms[s] = symn end return tostring(syms[s]) end)) end - local function safestr(s) return type(s) == "number" and tostring(huge and snum[tostring(s)] or s) + local function safestr(s) return type(s) == "number" and tostring(huge and snum[tostring(s)] or numformat:format(s)) or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026 or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end diff --git a/t/test.lua b/t/test.lua index ddf28b2..532515e 100644 --- a/t/test.lua +++ b/t/test.lua @@ -308,6 +308,18 @@ do assert(pcall(serpent.line, a) == true, "`__tostring` method that returns an error is still serialized correctly.") end +do + local a = serpent.line({ pi = math.pi }) + local ok, t = serpent.load(a) + assert(ok, "pi is serialized and deserialized (1/2): failed") + assert(math.pi == t.pi, "pi is deserialized with the same value as the original (1/2): failed") + + a = serpent.line({pi = math.pi}, {numformat = "%1.16e"}) + ok, t = serpent.load(a) + assert(ok, "pi is serialized and deserialized (2/2): failed") + assert(math.pi == t.pi, "pi is deserialized with the same value as the original with '%1.16e' format (2/2): failed") +end + -- based on https://gist.github.com/mpeterv/8360307 local function random_var(is_key, deep) local key = math.random(1000)