Comparison

util/stanza.lua @ 4168:4919831b5b56

util.stanza: Clean up matching_tags() and replace :childtags() with it
author Matthew Wild <mwild1@gmail.com>
date Sat, 19 Feb 2011 02:31:06 +0000
parent 4136:19f2830fbe02
child 4179:aa07a381e5a6
comparison
equal deleted inserted replaced
4143:eccd3c87d717 4168:4919831b5b56
136 i = i + 1 136 i = i + 1
137 return a[i]; 137 return a[i];
138 end, self, i; 138 end, self, i;
139 end 139 end
140 140
141 function stanza_mt:matching_tags(name, xmlns) 141 function stanza_mt:childtags(name, xmlns)
142 xmlns = xmlns or self.attr.xmlns; 142 xmlns = xmlns or self.attr.xmlns;
143 local tags = self.tags; 143 local tags = self.tags;
144 local start_i, max_i = 1, #tags; 144 local start_i, max_i = 1, #tags;
145 return function () 145 return function ()
146 for i=start_i,max_i do 146 for i = start_i, max_i do
147 v = tags[i]; 147 local v = tags[i];
148 if (not name or v.name == name) 148 if (not name or v.name == name)
149 and (not xmlns or xmlns == v.attr.xmlns) then 149 and (not xmlns or xmlns == v.attr.xmlns) then
150 start_i = i+1; 150 start_i = i+1;
151 return v; 151 return v;
152 end 152 end
153 end 153 end
154 end, tags, i; 154 end;
155 end
156
157 function stanza_mt:childtags()
158 local i = 0;
159 return function (a)
160 i = i + 1
161 local v = self.tags[i]
162 if v then return v; end
163 end, self.tags[1], i;
164 end 155 end
165 156
166 function stanza_mt:maptags(callback) 157 function stanza_mt:maptags(callback)
167 local tags, curr_tag = self.tags, 1; 158 local tags, curr_tag = self.tags, 1;
168 local n_children, n_tags = #self, #tags; 159 local n_children, n_tags = #self, #tags;