Skip to content

Use an iterator for reader::tagged_docs. #25760

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 3 commits into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,23 +436,29 @@ pub mod reader {
}
}

pub fn tagged_docs<F>(d: Doc, tg: usize, mut it: F) -> bool where
F: FnMut(Doc) -> bool,
{
let mut pos = d.start;
while pos < d.end {
let elt_tag = try_or!(tag_at(d.data, pos), false);
let elt_size = try_or!(tag_len_at(d.data, elt_tag), false);
pos = elt_size.next + elt_size.val;
if elt_tag.val == tg {
let doc = Doc { data: d.data, start: elt_size.next,
end: pos };
if !it(doc) {
return false;
pub fn tagged_docs<'a>(d: Doc<'a>, tag: usize) -> TaggedDocsIterator<'a> {
TaggedDocsIterator {
iter: docs(d),
tag: tag,
}
}

pub struct TaggedDocsIterator<'a> {
iter: DocsIterator<'a>,
tag: usize,
}

impl<'a> Iterator for TaggedDocsIterator<'a> {
type Item = Doc<'a>;

fn next(&mut self) -> Option<Doc<'a>> {
while let Some((tag, doc)) = self.iter.next() {
if tag == self.tag {
return Some(doc);
}
}
None
}
return true;
}

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