# HG changeset patch # User Waqas Hussain # Date 1298211416 -18000 # Node ID a361c578c1f2c5a9e8436ace2b7516a2d44a749a # Parent e992650185c864b4a5a441c7a6a2e981a1ac9827 util.stanza: Rewrite clone() to be more optimized. diff -r e992650185c8 -r a361c578c1f2 util/stanza.lua --- a/util/stanza.lua Mon Feb 14 19:12:16 2011 +0000 +++ b/util/stanza.lua Sun Feb 20 19:16:56 2011 +0500 @@ -329,24 +329,21 @@ return stanza; end -function clone(stanza) - local lookup_table = {}; - local function _copy(object) - if type(object) ~= "table" then - return object; - elseif lookup_table[object] then - return lookup_table[object]; +local function _clone(stanza) + local attr, tags = {}, {}; + for k,v in pairs(stanza.attr) do attr[k] = v; end + local new = { name = stanza.name, attr = attr, tags = tags }; + for i=1,#stanza do + local child = stanza[i]; + if child.name then + child = _clone(child); + t_insert(tags, child); end - local new_table = {}; - lookup_table[object] = new_table; - for index, value in pairs(object) do - new_table[_copy(index)] = _copy(value); - end - return setmetatable(new_table, getmetatable(object)); + t_insert(new, child); end - - return _copy(stanza) + return setmetatable(new, stanza_mt); end +clone = _clone; function message(attr, body) if not body then