From: Petr Špaček Date: Sat, 16 Mar 2019 10:06:21 +0000 (+0100) Subject: sandbox: table_print sorts table keys X-Git-Tag: v4.0.0~11^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f83bcf847016acd6164d47ed766b5107063a1ed1;p=thirdparty%2Fknot-resolver.git sandbox: table_print sorts table keys This makes it much easier to navigate in complex data structures. AFAIK table_print is not used for anything except user interface so it is not performance critical and we can re-sort table every time. --- diff --git a/daemon/lua/sandbox.lua.in b/daemon/lua/sandbox.lua.in index 562d1d7dc..0d27f5477 100644 --- a/daemon/lua/sandbox.lua.in +++ b/daemon/lua/sandbox.lua.in @@ -359,6 +359,20 @@ function table_print (tt, indent, done) indent = indent or 0 local result = "" -- Convert to printable string (escape unprintable) + local function ordered_iter(unordered_tt) + local keys = {} + for k in pairs(unordered_tt) do + table.insert(keys, tostring(k)) + end + table.sort(keys) + local i = 0 + return function() + i = i + 1 + if keys[i] then + return keys[i], unordered_tt[keys[i]] + end + end + end local function printable(value) value = tostring(value) local bytes = {} @@ -372,7 +386,7 @@ function table_print (tt, indent, done) return table.concat(bytes) end if type(tt) == "table" then - for key, value in pairs (tt) do + for key, value in ordered_iter(tt) do result = result .. string.rep (" ", indent) if type (value) == "table" and not done [value] then done [value] = true