mirror of
https://github.com/pkulchenko/serpent.git
synced 2024-11-21 23:24:24 +00:00
This is needed to produce a valid Lua code when a local locale setting makes `format` to produce floating point numbers with a comma. Limitation: only commas are being fixed in floating point numbers.
This commit is contained in:
parent
879580fb21
commit
6e41f2ca07
@ -69,6 +69,7 @@ Similar to `pcall` and `loadstring` calls, `load` returns status as the first va
|
|||||||
* sparse (true/false) -- force sparse encoding (no nil filling based on `#t`).
|
* sparse (true/false) -- force sparse encoding (no nil filling based on `#t`).
|
||||||
* compact (true/false) -- remove spaces.
|
* compact (true/false) -- remove spaces.
|
||||||
* fatal (true/False) -- raise fatal error on non-serilizable values.
|
* fatal (true/False) -- raise fatal error on non-serilizable values.
|
||||||
|
* fixradix (true/False) -- change radix character set depending on locale to decimal dot.
|
||||||
* nocode (true/False) -- disable bytecode serialization for easy comparison.
|
* nocode (true/False) -- disable bytecode serialization for easy comparison.
|
||||||
* nohuge (true/False) -- disable checking numbers against undefined and huge values.
|
* nohuge (true/False) -- disable checking numbers against undefined and huge values.
|
||||||
* maxlevel (number) -- specify max level up to which to expand nested tables.
|
* maxlevel (number) -- specify max level up to which to expand nested tables.
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
local n, v = "serpent", "0.302" -- (C) 2012-18 Paul Kulchenko; MIT License
|
local n, v = "serpent", "0.303" -- (C) 2012-18 Paul Kulchenko; MIT License
|
||||||
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
|
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 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}
|
local badtype = {thread = true, userdata = true, cdata = true}
|
||||||
@ -23,9 +23,16 @@ local function s(t, opts)
|
|||||||
local function gensym(val) return '_'..(tostring(tostring(val)):gsub("[^%w]",""):gsub("(%d%w+)",
|
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
|
-- 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
|
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 numformat:format(s))
|
local function safestr(s) return type(s) == "number" and (huge and snum[tostring(s)] or numformat:format(s))
|
||||||
or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026
|
or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026
|
||||||
or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end
|
or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end
|
||||||
|
-- handle radix changes in some locales
|
||||||
|
if opts.fixradix and (".1f"):format(1.2) ~= "1.2" then
|
||||||
|
local origsafestr = safestr
|
||||||
|
safestr = function(s) return type(s) == "number"
|
||||||
|
and (nohuge and snum[tostring(s)] or numformat:format(s):gsub(",",".")) or origsafestr(s)
|
||||||
|
end
|
||||||
|
end
|
||||||
local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end
|
local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end
|
||||||
local function globerr(s,l) return globals[s] and globals[s]..comment(s,l) or not fatal
|
local function globerr(s,l) return globals[s] and globals[s]..comment(s,l) or not fatal
|
||||||
and safestr(select(2, pcall(tostring, s))) or error("Can't serialize "..tostring(s)) end
|
and safestr(select(2, pcall(tostring, s))) or error("Can't serialize "..tostring(s)) end
|
||||||
|
18
t/test.lua
18
t/test.lua
@ -328,13 +328,23 @@ end
|
|||||||
do
|
do
|
||||||
local a = serpent.line({ pi = math.pi })
|
local a = serpent.line({ pi = math.pi })
|
||||||
local ok, t = serpent.load(a)
|
local ok, t = serpent.load(a)
|
||||||
assert(ok, "pi is serialized and deserialized (1/2): failed")
|
assert(ok, "pi is serialized and deserialized (1/3): failed")
|
||||||
assert(math.pi == t.pi, "pi is deserialized with the same value as the original (1/2): failed")
|
assert(math.pi == t.pi, "pi is deserialized with the same value as the original (1/3): failed")
|
||||||
|
|
||||||
a = serpent.line({pi = math.pi}, {numformat = "%1.16e"})
|
a = serpent.line({pi = math.pi}, {numformat = "%1.16e"})
|
||||||
ok, t = serpent.load(a)
|
ok, t = serpent.load(a)
|
||||||
assert(ok, "pi is serialized and deserialized (2/2): failed")
|
assert(ok, "pi is serialized and deserialized (2/3): failed")
|
||||||
assert(math.pi == t.pi, "pi is deserialized with the same value as the original with '%1.16e' format (2/2): failed")
|
assert(math.pi == t.pi, "pi is deserialized with the same value as the original with '%1.16e' format (2/3): failed")
|
||||||
|
|
||||||
|
local locale = os.setlocale()
|
||||||
|
os.setlocale("German_Germany")
|
||||||
|
|
||||||
|
a = serpent.line({pi = math.pi}, {fixradix = true})
|
||||||
|
ok, t = serpent.load(a)
|
||||||
|
assert(ok, "pi is serialized and deserialized with fr_FR locale (3/3): failed")
|
||||||
|
assert(math.pi == t.pi, "pi is deserialized with the same value as the original with fr_FR locale (3/3): failed")
|
||||||
|
|
||||||
|
os.setlocale(locale)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- based on https://gist.github.com/mpeterv/8360307
|
-- based on https://gist.github.com/mpeterv/8360307
|
||||||
|
Loading…
Reference in New Issue
Block a user