Mercurial > prosody-hg
comparison util/serialization.lua @ 9486:20aad0108999
util.serialization: Remove encoding of very large or very small numbers in scientific notation
Also difficult to describe this option, easier to remove it.
%.18g covers a very large range of numbers
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 12 Oct 2018 00:15:08 +0200 |
| parents | c667887d78ad |
| children | ed0090f8b709 |
comparison
equal
deleted
inserted
replaced
| 9485:c667887d78ad | 9486:20aad0108999 |
|---|---|
| 18 | 18 |
| 19 local pcall = pcall; | 19 local pcall = pcall; |
| 20 local envload = require"util.envload".envload; | 20 local envload = require"util.envload".envload; |
| 21 | 21 |
| 22 local pos_inf, neg_inf = math.huge, -math.huge; | 22 local pos_inf, neg_inf = math.huge, -math.huge; |
| 23 local m_log = math.log; | |
| 24 local m_log10 = math.log10 or function (n) | |
| 25 return m_log(n, 10); | |
| 26 end | |
| 27 local m_floor = math.floor; | |
| 28 -- luacheck: ignore 143/math | 23 -- luacheck: ignore 143/math |
| 29 local m_type = math.type or function (n) | 24 local m_type = math.type or function (n) |
| 30 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; | 25 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; |
| 31 end; | 26 end; |
| 32 | 27 |
| 122 local kend = opt.kend or "]"; | 117 local kend = opt.kend or "]"; |
| 123 local equals = opt.equals or " = "; | 118 local equals = opt.equals or " = "; |
| 124 local unquoted = opt.unquoted == nil and "^[%a_][%w_]*$" or opt.unquoted; | 119 local unquoted = opt.unquoted == nil and "^[%a_][%w_]*$" or opt.unquoted; |
| 125 local hex = opt.hex; | 120 local hex = opt.hex; |
| 126 local freeze = opt.freeze; | 121 local freeze = opt.freeze; |
| 127 local precision = opt.precision or 10; | |
| 128 | 122 |
| 129 -- serialize one table, recursively | 123 -- serialize one table, recursively |
| 130 -- t - table being serialized | 124 -- t - table being serialized |
| 131 -- o - array where tokens are added, concatenate to get final result | 125 -- o - array where tokens are added, concatenate to get final result |
| 132 -- - also used to detect cycles | 126 -- - also used to detect cycles |
| 235 elseif t == neg_inf then | 229 elseif t == neg_inf then |
| 236 return "(-1/0)"; | 230 return "(-1/0)"; |
| 237 elseif t ~= t then | 231 elseif t ~= t then |
| 238 return "(0/0)"; | 232 return "(0/0)"; |
| 239 end | 233 end |
| 240 local log = m_floor(m_log10(t)); | 234 return s_format("%.18g", t); |
| 241 if log > precision then | |
| 242 return s_format("%.18e", t); | |
| 243 else | |
| 244 return s_format("%.18g", t); | |
| 245 end | |
| 246 end | 235 end |
| 247 | 236 |
| 248 -- Are these faster than tostring? | 237 -- Are these faster than tostring? |
| 249 types["nil"] = function() | 238 types["nil"] = function() |
| 250 return "nil"; | 239 return "nil"; |
