Diff

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
line wrap: on
line diff
--- a/util/serialization.lua	Mon Mar 18 09:50:23 2019 +0000
+++ b/util/serialization.lua	Sun Mar 17 20:40:01 2019 +0100
@@ -163,7 +163,9 @@
 		local indent = s_rep(indentwith, d);
 		local numkey = 1;
 		local ktyp, vtyp;
+		local had_items = false;
 		for k,v in next,t do
+			had_items = true;
 			o[l], l = itemstart, l + 1;
 			o[l], l = indent, l + 1;
 			ktyp, vtyp = type(k), type(v);
@@ -194,14 +196,10 @@
 			else
 				o[l], l = ser(v), l + 1;
 			end
-			-- last item?
-			if next(t, k) ~= nil then
-				o[l], l = itemsep, l + 1;
-			else
-				o[l], l = itemlast, l + 1;
-			end
+			o[l], l = itemsep, l + 1;
 		end
-		if next(t) ~= nil then
+		if had_items then
+			o[l - 1] = itemlast;
 			o[l], l = s_rep(indentwith, d-1), l + 1;
 		end
 		o[l], l = tend, l +1;