util.stanza: Add stanza:maptags() to apply a function over child tags (return nil...
authorMatthew Wild <mwild1@gmail.com>
Mon, 30 Aug 2010 03:53:41 +0000 (04:53 +0100)
committerMatthew Wild <mwild1@gmail.com>
Mon, 30 Aug 2010 03:53:41 +0000 (04:53 +0100)
util/stanza.lua

index 3ab4bb42f8cad52daf20b5da27a74df938f73225..50fe02f0b7cb9ded4fcbd0da7f791ca9b38b2bbf 100644 (file)
@@ -151,6 +151,30 @@ function stanza_mt:childtags()
                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;" };