util.stanza: Optimisation, remove useless if...then in stanza:children() iterator
[prosody.git] / util / stanza.lua
index ad982d429483bc6eb89df251bb3167e5efae01ad..3ab4bb42f8cad52daf20b5da27a74df938f73225 100644 (file)
@@ -1,6 +1,6 @@
 -- Prosody IM
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Copyright (C) 2008-2010 Matthew Wild
+-- Copyright (C) 2008-2010 Waqas Hussain
 -- 
 -- This project is MIT/X11 licensed. Please see the
 -- COPYING file in the source package for more information.
@@ -122,10 +122,26 @@ function stanza_mt:children()
        local i = 0;
        return function (a)
                        i = i + 1
-                       local v = a[i]
-                       if v then return v; end
+                       return a[i];
                end, self, i;
 end
+
+function stanza_mt:matched_children(name, xmlns)
+       xmlns = xmlns or self.attr.xmlns;
+       local tags = self.tags;
+       local start_i, max_i = 1, #tags;
+       return function ()
+                       for i=start_i,max_i do
+                               v = tags[i];
+                               if (not name or v.name == name)
+                               and (not xmlns or xmlns == v.attr.xmlns) then
+                                       start_i = i+1;
+                                       return v;
+                               end
+                       end
+               end, tags, i;
+end
+
 function stanza_mt:childtags()
        local i = 0;
        return function (a)