Changeset

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
parents 4178:760f644c0ca3
children 4180:cd3fb3393e13
files util/stanza.lua
diffstat 1 files changed, 12 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/util/stanza.lua	Sun Feb 13 19:28:29 2011 +0000
+++ b/util/stanza.lua	Sun Feb 20 19:16:56 2011 +0500
@@ -320,24 +320,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