Software /
code /
prosody
Comparison
util/stanza.lua @ 4146:a361c578c1f2
util.stanza: Rewrite clone() to be more optimized.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Sun, 20 Feb 2011 19:16:56 +0500 |
parent | 4136:19f2830fbe02 |
child | 4153:676588aed2d7 |
comparison
equal
deleted
inserted
replaced
4145:e992650185c8 | 4146:a361c578c1f2 |
---|---|
327 end | 327 end |
328 | 328 |
329 return stanza; | 329 return stanza; |
330 end | 330 end |
331 | 331 |
332 function clone(stanza) | 332 local function _clone(stanza) |
333 local lookup_table = {}; | 333 local attr, tags = {}, {}; |
334 local function _copy(object) | 334 for k,v in pairs(stanza.attr) do attr[k] = v; end |
335 if type(object) ~= "table" then | 335 local new = { name = stanza.name, attr = attr, tags = tags }; |
336 return object; | 336 for i=1,#stanza do |
337 elseif lookup_table[object] then | 337 local child = stanza[i]; |
338 return lookup_table[object]; | 338 if child.name then |
339 end | 339 child = _clone(child); |
340 local new_table = {}; | 340 t_insert(tags, child); |
341 lookup_table[object] = new_table; | 341 end |
342 for index, value in pairs(object) do | 342 t_insert(new, child); |
343 new_table[_copy(index)] = _copy(value); | 343 end |
344 end | 344 return setmetatable(new, stanza_mt); |
345 return setmetatable(new_table, getmetatable(object)); | 345 end |
346 end | 346 clone = _clone; |
347 | |
348 return _copy(stanza) | |
349 end | |
350 | 347 |
351 function message(attr, body) | 348 function message(attr, body) |
352 if not body then | 349 if not body then |
353 return stanza("message", attr); | 350 return stanza("message", attr); |
354 else | 351 else |