Comparison

util/stanza.lua @ 5414:efec29eb4cdd

util.stanza: :maptags(): Fixes to make loop more robust on item removal
author Matthew Wild <mwild1@gmail.com>
date Wed, 03 Apr 2013 13:38:27 +0100
parent 5090:61c7c53c06d5
child 5424:7318527c6dea
comparison
equal deleted inserted replaced
5413:0bf5e90be086 5414:efec29eb4cdd
151 function stanza_mt:maptags(callback) 151 function stanza_mt:maptags(callback)
152 local tags, curr_tag = self.tags, 1; 152 local tags, curr_tag = self.tags, 1;
153 local n_children, n_tags = #self, #tags; 153 local n_children, n_tags = #self, #tags;
154 154
155 local i = 1; 155 local i = 1;
156 while curr_tag <= n_tags do 156 while curr_tag <= n_tags and n_tags > 0 do
157 if self[i] == tags[curr_tag] then 157 if self[i] == tags[curr_tag] then
158 local ret = callback(self[i]); 158 local ret = callback(self[i]);
159 if ret == nil then 159 if ret == nil then
160 t_remove(self, i); 160 t_remove(self, i);
161 t_remove(tags, curr_tag); 161 t_remove(tags, curr_tag);
162 n_children = n_children - 1; 162 n_children = n_children - 1;
163 n_tags = n_tags - 1; 163 n_tags = n_tags - 1;
164 i = i - 1;
165 curr_tag = curr_tag - 1;
164 else 166 else
165 self[i] = ret; 167 self[i] = ret;
166 tags[i] = ret; 168 tags[i] = ret;
167 end 169 end
168 i = i + 1;
169 curr_tag = curr_tag + 1; 170 curr_tag = curr_tag + 1;
170 end 171 end
172 i = i + 1;
171 end 173 end
172 return self; 174 return self;
173 end 175 end
174 176
175 local xml_escape 177 local xml_escape