diff options
Diffstat (limited to 'benchmarks/quick-xml/src/pubmed_len.rs')
| -rw-r--r-- | benchmarks/quick-xml/src/pubmed_len.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/benchmarks/quick-xml/src/pubmed_len.rs b/benchmarks/quick-xml/src/pubmed_len.rs new file mode 100644 index 0000000..592e6e3 --- /dev/null +++ b/benchmarks/quick-xml/src/pubmed_len.rs @@ -0,0 +1,82 @@ +use quick_xml::events::Event; +use quick_xml::Reader; + +pub fn pubmed_len(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + let mut in_content = 0usize; + let mut total = 0usize; + + loop { + match reader.read_event() { + Ok(Event::Start(e)) => { + if in_content != 0 { + in_content += 1; + } else { + if e.name().as_ref() == b"abstract" + || e.name().as_ref()== b"body" { + in_content = 1; + } + } + } + + Ok(Event::End(_)) => { + if in_content != 0 { + in_content -= 1; + } + } + + Ok(Event::Text(e)) => { + if in_content != 0 { + total += e.len(); + } + } + + Ok(Event::Eof) => break, + + Ok(_) => {} + Err(_) => unreachable!("buffer is valid PMC XML"), + } + } + + total +} + +pub fn pubmed_len_with_entities(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + let mut in_content = 0usize; + let mut total = 0usize; + + loop { + match reader.read_event() { + Ok(Event::Start(e)) => { + if in_content != 0 { + in_content += 1; + } else if e.name().as_ref() == b"abstract" + || e.name().as_ref() == b"body" + { + in_content = 1; + } + } + + Ok(Event::End(_)) => { + if in_content != 0 { + in_content -= 1; + } + } + + Ok(Event::Text(e)) => { + if in_content != 0 { + total += e.decode().unwrap().len(); + } + } + + Ok(Event::Eof) => break, + + Ok(_) => {} + Err(_) => unreachable!("buffer is valid PMC XML"), + } + } + + total +} + |
