Comparison

util/serialization.lua @ 9863:aebfd1601ae2

util.serialization: Optimize handling of last table separator Fewer next() calls and a step towards allowing use of a different iterator.
author Kim Alvefur <zash@zash.se>
date Sun, 17 Mar 2019 20:40:01 +0100
parent 9692:affcbccc1dff
child 9864:9d80e679c371
comparison
equal deleted inserted replaced
9862:f5623a1b33e5 9863:aebfd1601ae2
161 161
162 o[l], l = tstart, l + 1; 162 o[l], l = tstart, l + 1;
163 local indent = s_rep(indentwith, d); 163 local indent = s_rep(indentwith, d);
164 local numkey = 1; 164 local numkey = 1;
165 local ktyp, vtyp; 165 local ktyp, vtyp;
166 local had_items = false;
166 for k,v in next,t do 167 for k,v in next,t do
168 had_items = true;
167 o[l], l = itemstart, l + 1; 169 o[l], l = itemstart, l + 1;
168 o[l], l = indent, l + 1; 170 o[l], l = indent, l + 1;
169 ktyp, vtyp = type(k), type(v); 171 ktyp, vtyp = type(k), type(v);
170 if k == numkey then 172 if k == numkey then
171 -- next index in array part 173 -- next index in array part
192 if vtyp == "table" then 194 if vtyp == "table" then
193 l = serialize_table(v, o, l, d+1); 195 l = serialize_table(v, o, l, d+1);
194 else 196 else
195 o[l], l = ser(v), l + 1; 197 o[l], l = ser(v), l + 1;
196 end 198 end
197 -- last item? 199 o[l], l = itemsep, l + 1;
198 if next(t, k) ~= nil then 200 end
199 o[l], l = itemsep, l + 1; 201 if had_items then
200 else 202 o[l - 1] = itemlast;
201 o[l], l = itemlast, l + 1;
202 end
203 end
204 if next(t) ~= nil then
205 o[l], l = s_rep(indentwith, d-1), l + 1; 203 o[l], l = s_rep(indentwith, d-1), l + 1;
206 end 204 end
207 o[l], l = tend, l +1; 205 o[l], l = tend, l +1;
208 206
209 if multirefs then 207 if multirefs then