Mercurial > prosody-hg
comparison util/serialization.lua @ 10061:5c71693c8345
Merge 0.11->trunk
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 08 Jul 2019 02:44:32 +0200 |
| parents | 37278b420c74 7a36b7ac309b |
| children | ec14d4fce855 |
comparison
equal
deleted
inserted
replaced
| 10060:7a36b7ac309b | 10061:5c71693c8345 |
|---|---|
| 14 local s_rep = string.rep; | 14 local s_rep = string.rep; |
| 15 local s_char = string.char; | 15 local s_char = string.char; |
| 16 local s_match = string.match; | 16 local s_match = string.match; |
| 17 local t_concat = table.concat; | 17 local t_concat = table.concat; |
| 18 | 18 |
| 19 local to_hex = require "util.hex".to; | |
| 20 | |
| 19 local pcall = pcall; | 21 local pcall = pcall; |
| 20 local envload = require"util.envload".envload; | 22 local envload = require"util.envload".envload; |
| 21 | 23 |
| 22 local pos_inf, neg_inf = math.huge, -math.huge; | 24 local pos_inf, neg_inf = math.huge, -math.huge; |
| 23 -- luacheck: ignore 143/math | |
| 24 local m_type = math.type or function (n) | 25 local m_type = math.type or function (n) |
| 25 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; | 26 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; |
| 26 end; | 27 end; |
| 27 | 28 |
| 28 local char_to_hex = {}; | 29 local function rawpairs(t) |
| 29 for i = 0,255 do | 30 return next, t, nil; |
| 30 char_to_hex[s_char(i)] = s_format("%02x", i); | |
| 31 end | |
| 32 | |
| 33 local function to_hex(s) | |
| 34 return (s_gsub(s, ".", char_to_hex)); | |
| 35 end | 31 end |
| 36 | 32 |
| 37 local function fatal_error(obj, why) | 33 local function fatal_error(obj, why) |
| 38 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); | 34 error("Can't serialize "..type(obj) .. (why and ": ".. why or "")); |
| 39 end | 35 end |
| 121 local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted; | 117 local unquoted = opt.unquoted == true and "^[%a_][%w_]*$" or opt.unquoted; |
| 122 local hex = opt.hex; | 118 local hex = opt.hex; |
| 123 local freeze = opt.freeze; | 119 local freeze = opt.freeze; |
| 124 local maxdepth = opt.maxdepth or 127; | 120 local maxdepth = opt.maxdepth or 127; |
| 125 local multirefs = opt.multiref; | 121 local multirefs = opt.multiref; |
| 122 local table_pairs = opt.table_iterator or rawpairs; | |
| 126 | 123 |
| 127 -- serialize one table, recursively | 124 -- serialize one table, recursively |
| 128 -- t - table being serialized | 125 -- t - table being serialized |
| 129 -- o - array where tokens are added, concatenate to get final result | 126 -- o - array where tokens are added, concatenate to get final result |
| 130 -- - also used to detect cycles | 127 -- - also used to detect cycles |
| 162 | 159 |
| 163 o[l], l = tstart, l + 1; | 160 o[l], l = tstart, l + 1; |
| 164 local indent = s_rep(indentwith, d); | 161 local indent = s_rep(indentwith, d); |
| 165 local numkey = 1; | 162 local numkey = 1; |
| 166 local ktyp, vtyp; | 163 local ktyp, vtyp; |
| 167 for k,v in next,t do | 164 local had_items = false; |
| 165 for k,v in table_pairs(t) do | |
| 166 had_items = true; | |
| 168 o[l], l = itemstart, l + 1; | 167 o[l], l = itemstart, l + 1; |
| 169 o[l], l = indent, l + 1; | 168 o[l], l = indent, l + 1; |
| 170 ktyp, vtyp = type(k), type(v); | 169 ktyp, vtyp = type(k), type(v); |
| 171 if k == numkey then | 170 if k == numkey then |
| 172 -- next index in array part | 171 -- next index in array part |
| 193 if vtyp == "table" then | 192 if vtyp == "table" then |
| 194 l = serialize_table(v, o, l, d+1); | 193 l = serialize_table(v, o, l, d+1); |
| 195 else | 194 else |
| 196 o[l], l = ser(v), l + 1; | 195 o[l], l = ser(v), l + 1; |
| 197 end | 196 end |
| 198 -- last item? | 197 o[l], l = itemsep, l + 1; |
| 199 if next(t, k) ~= nil then | 198 end |
| 200 o[l], l = itemsep, l + 1; | 199 if had_items then |
| 201 else | 200 o[l - 1] = itemlast; |
| 202 o[l], l = itemlast, l + 1; | |
| 203 end | |
| 204 end | |
| 205 if next(t) ~= nil then | |
| 206 o[l], l = s_rep(indentwith, d-1), l + 1; | 201 o[l], l = s_rep(indentwith, d-1), l + 1; |
| 207 end | 202 end |
| 208 o[l], l = tend, l +1; | 203 o[l], l = tend, l +1; |
| 209 | 204 |
| 210 if multirefs then | 205 if multirefs then |
