From: Petr Špaček Date: Fri, 17 Jul 2020 08:08:25 +0000 (+0200) Subject: krprint: order keys while pretty-printing tables X-Git-Tag: v5.2.0~4^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=481066c77083eae56decf4776d86e75af8f73f89;p=thirdparty%2Fknot-resolver.git krprint: order keys while pretty-printing tables Code was copied from Lua sandbox, the old copy is to be removed once new pretty-printer is finished. --- diff --git a/daemon/lua/krprint.lua b/daemon/lua/krprint.lua index 0f9cfc803..5f2db3356 100644 --- a/daemon/lua/krprint.lua +++ b/daemon/lua/krprint.lua @@ -96,6 +96,31 @@ function base_class.boolean(_, val) return tostring(val) end +local function ordered_iter(unordered_tt) + local keys = {} + for k in pairs(unordered_tt) do + table.insert(keys, k) + end + table.sort(keys, + function (a, b) + if type(a) ~= type(b) then + return type(a) < type(b) + end + if type(a) == 'number' then + return a < b + else + return tostring(a) < tostring(b) + end + end) + local i = 0 + return function() + i = i + 1 + if keys[i] ~= nil then + return keys[i], unordered_tt[keys[i]] + end + end +end + function base_class.table(self, tab) assert(type(tab) == 'table') if self.done[tab] then @@ -106,7 +131,7 @@ function base_class.table(self, tab) local items = {'{'} local previdx = 0 self:indent_inc() - for idx, val in pairs(tab) do + for idx, val in ordered_iter(tab) do local errors, valok, valexpr, valnote, idxok, idxexpr, idxnote errors = {} valok, valexpr, valnote = pcall(self.val2expr, self, val)