Comparison

util/stanza.lua @ 829:b01fd698495e

util/stanza: Added clone function
author Waqas Hussain <waqas20@gmail.com>
date Fri, 20 Feb 2009 02:18:07 +0500
parent 776:89eb9f59993c
child 896:2c0b9e3c11c3
comparison
equal deleted inserted replaced
828:97ea39b7bf90 829:b01fd698495e
10 local t_insert = table.insert; 10 local t_insert = table.insert;
11 local t_concat = table.concat; 11 local t_concat = table.concat;
12 local t_remove = table.remove; 12 local t_remove = table.remove;
13 local t_concat = table.concat; 13 local t_concat = table.concat;
14 local s_format = string.format; 14 local s_format = string.format;
15 local s_match = string.match; 15 local s_match = string.match;
16 local tostring = tostring; 16 local tostring = tostring;
17 local setmetatable = setmetatable; 17 local setmetatable = setmetatable;
18 local getmetatable = getmetatable;
18 local pairs = pairs; 19 local pairs = pairs;
19 local ipairs = ipairs; 20 local ipairs = ipairs;
20 local type = type; 21 local type = type;
21 local next = next; 22 local next = next;
22 local print = print; 23 local print = print;
211 end 212 end
212 end 213 end
213 end 214 end
214 215
215 return stanza; 216 return stanza;
217 end
218
219 function clone(stanza)
220 local lookup_table = {};
221 local function _copy(object)
222 if type(object) ~= "table" then
223 return object;
224 elseif lookup_table[object] then
225 return lookup_table[object];
226 end
227 local new_table = {};
228 lookup_table[object] = new_table;
229 for index, value in pairs(object) do
230 new_table[_copy(index)] = _copy(value);
231 end
232 return setmetatable(new_table, getmetatable(object));
233 end
234 return _copy(stanza)
216 end 235 end
217 236
218 function message(attr, body) 237 function message(attr, body)
219 if not body then 238 if not body then
220 return stanza("message", attr); 239 return stanza("message", attr);