diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-09-14 11:37:14 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-09-14 11:37:14 +0100 |
commit | c2a0484005d2e574830fec4245cee1ea9cce82c9 (patch) | |
tree | aec63e591909bfb7cc2d98dec9f7a21f76abb154 /lualib/lua_util.lua | |
parent | f2f389b273f1f92dd79b2250dc941a3a6bb1b0c0 (diff) | |
download | rspamd-c2a0484005d2e574830fec4245cee1ea9cce82c9.tar.gz rspamd-c2a0484005d2e574830fec4245cee1ea9cce82c9.zip |
[Minor] Add deepsort utility
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r-- | lualib/lua_util.lua | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 6b42dbc5d..7c925c12b 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -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 @@ -992,6 +994,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 --]] |