Changeset

3477:6350b114e0e4

util.stanza: Add stanza:maptags() to apply a function over child tags (return nil to remove tag from stanza)
author Matthew Wild <mwild1@gmail.com>
date Mon, 30 Aug 2010 04:53:41 +0100
parents 3476:193bb0936a4e
children 3478:4621c92d2368
files util/stanza.lua
diffstat 1 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/stanza.lua	Mon Aug 30 04:37:53 2010 +0100
+++ b/util/stanza.lua	Mon Aug 30 04:53:41 2010 +0100
@@ -151,6 +151,30 @@
 		end, self.tags[1], i;
 end
 
+function stanza_mt:maptags(callback)
+	local tags, curr_tag = self.tags, 1;
+	local n_children, n_tags = #self, #tags;
+	
+	local i = 1;
+	while curr_tag <= n_tags do
+		if self[i] == tags[curr_tag] then
+			local ret = callback(self[i]);
+			if ret == nil then
+				t_remove(self, i);
+				t_remove(tags, curr_tag);
+				n_children = n_children - 1;
+				n_tags = n_tags - 1;
+			else
+				self[i] = ret;
+				tags[i] = ret;
+			end
+			i = i + 1;
+			curr_tag = curr_tag + 1;
+		end
+	end
+	return self;
+end
+
 local xml_escape
 do
 	local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };