]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add deepsort utility
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 14 Sep 2020 10:37:14 +0000 (11:37 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 14 Sep 2020 10:37:14 +0000 (11:37 +0100)
lualib/lua_util.lua

index 6b42dbc5d9856c62895a42bb65f73e1a20da81e1..7c925c12b7e79fadb7f68e0ad81c1be9bc6fa830 100644 (file)
@@ -982,7 +982,9 @@ local function deepcopy(orig)
     for orig_key, orig_value in next, orig, nil do
       copy[deepcopy(orig_key)] = deepcopy(orig_value)
     end
-    setmetatable(copy, deepcopy(getmetatable(orig)))
+    if getmetatable(orig) then
+      setmetatable(copy, deepcopy(getmetatable(orig)))
+    end
   else -- number, string, boolean, etc
     copy = orig
   end
@@ -991,6 +993,25 @@ end
 
 exports.deepcopy = deepcopy
 
+--[[[
+-- @function lua_util.deepsort(table)
+-- params: {
+- - table
+-- }
+-- Performs recursive in-place sort of a table
+--]]
+local function deepsort(tbl, sort_func)
+  local orig_type = type(tbl)
+  if orig_type == 'table' then
+    table.sort(tbl, sort_func)
+    for _, orig_value in next, tbl, nil do
+      deepsort(orig_value)
+    end
+  end
+end
+
+exports.deepsort = deepsort
+
 --[[[
 -- @function lua_util.shallowcopy(tbl)
 -- Performs shallow (and fast) copy of a table or another Lua type