Comparison

util/stanza.lua @ 4179:aa07a381e5a6

util.stanza: Rewrite clone() to be more optimized.
author Waqas Hussain <waqas20@gmail.com>
date Sun, 20 Feb 2011 19:16:56 +0500
parent 4168:4919831b5b56
child 4184:7e3a570f33f4
comparison
equal deleted inserted replaced
4178:760f644c0ca3 4179:aa07a381e5a6
318 end 318 end
319 319
320 return stanza; 320 return stanza;
321 end 321 end
322 322
323 function clone(stanza) 323 local function _clone(stanza)
324 local lookup_table = {}; 324 local attr, tags = {}, {};
325 local function _copy(object) 325 for k,v in pairs(stanza.attr) do attr[k] = v; end
326 if type(object) ~= "table" then 326 local new = { name = stanza.name, attr = attr, tags = tags };
327 return object; 327 for i=1,#stanza do
328 elseif lookup_table[object] then 328 local child = stanza[i];
329 return lookup_table[object]; 329 if child.name then
330 end 330 child = _clone(child);
331 local new_table = {}; 331 t_insert(tags, child);
332 lookup_table[object] = new_table; 332 end
333 for index, value in pairs(object) do 333 t_insert(new, child);
334 new_table[_copy(index)] = _copy(value); 334 end
335 end 335 return setmetatable(new, stanza_mt);
336 return setmetatable(new_table, getmetatable(object)); 336 end
337 end 337 clone = _clone;
338
339 return _copy(stanza)
340 end
341 338
342 function message(attr, body) 339 function message(attr, body)
343 if not body then 340 if not body then
344 return stanza("message", attr); 341 return stanza("message", attr);
345 else 342 else