Skip to content

Commit 8e039a8

Browse files
committed
Introduce TaggedDocsIterator and use it to implement reader::tagged_docs.
1 parent ba0e1cd commit 8e039a8

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/librbml/lib.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -436,23 +436,31 @@ pub mod reader {
436436
}
437437
}
438438

439-
pub fn tagged_docs<F>(d: Doc, tg: usize, mut it: F) -> bool where
439+
pub fn tagged_docs<F>(d: Doc, tg: usize, it: F) -> bool where
440440
F: FnMut(Doc) -> bool,
441441
{
442-
let mut pos = d.start;
443-
while pos < d.end {
444-
let elt_tag = try_or!(tag_at(d.data, pos), false);
445-
let elt_size = try_or!(tag_len_at(d.data, elt_tag), false);
446-
pos = elt_size.next + elt_size.val;
447-
if elt_tag.val == tg {
448-
let doc = Doc { data: d.data, start: elt_size.next,
449-
end: pos };
450-
if !it(doc) {
451-
return false;
442+
TaggedDocsIterator {
443+
iter: docs(d),
444+
tag: tg,
445+
}.all(it)
446+
}
447+
448+
pub struct TaggedDocsIterator<'a> {
449+
iter: DocsIterator<'a>,
450+
tag: usize,
451+
}
452+
453+
impl<'a> Iterator for TaggedDocsIterator<'a> {
454+
type Item = Doc<'a>;
455+
456+
fn next(&mut self) -> Option<Doc<'a>> {
457+
while let Some((tag, doc)) = self.iter.next() {
458+
if tag == self.tag {
459+
return Some(doc);
452460
}
453461
}
462+
None
454463
}
455-
return true;
456464
}
457465

458466
pub fn with_doc_data<T, F>(d: Doc, f: F) -> T where

0 commit comments

Comments
 (0)