From e0d362c7f25718abd881c90b163945d4b7b80fe1 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Tue, 11 Sep 2018 11:01:18 +0100 Subject: [PATCH] [Minor] Add shallowcopy utility --- lualib/lua_util.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 0ce0c18748..e0096daa19 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -664,6 +664,25 @@ exports.extract_specific_urls = function(params_or_task, lim, need_emails, filte return res end + +--[[[ +-- @function lua_util.shallowcopy(tbl) +-- Performs shallow (and fast) copy of a table or another Lua type +--]] +exports.shallowcopy = function(orig) + local orig_type = type(orig) + local copy + if orig_type == 'table' then + copy = {} + for orig_key, orig_value in pairs(orig) do + copy[orig_key] = orig_value + end + else + copy = orig + end + return copy +end + -- Debugging support local unconditional_debug = false local debug_modules = {} @@ -697,4 +716,5 @@ exports.debugm = function(mod, ...) end end + return exports -- 2.47.3