diff options
| author | steven-vd <steven@vandorp.lu> | 2026-08-16 15:00:32 +0200 |
|---|---|---|
| committer | steven-vd <steven@vandorp.lu> | 2026-08-17 09:31:42 +0200 |
| commit | ee081ca70322cd01d0642fdb1860e6bfbfb48e85 (patch) | |
| tree | 295fdba93b08b24562c6d49e971bbf3425eac50c /benchmarks/quick-xml/src/count_highway_crossings.rs | |
Diffstat (limited to 'benchmarks/quick-xml/src/count_highway_crossings.rs')
| -rw-r--r-- | benchmarks/quick-xml/src/count_highway_crossings.rs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/benchmarks/quick-xml/src/count_highway_crossings.rs b/benchmarks/quick-xml/src/count_highway_crossings.rs new file mode 100644 index 0000000..9b68142 --- /dev/null +++ b/benchmarks/quick-xml/src/count_highway_crossings.rs @@ -0,0 +1,74 @@ +use quick_xml::events::Event; +use quick_xml::Reader; + +pub fn count_highway_crossings(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + reader.config_mut().check_end_names = false; + + let mut count = 0usize; + let mut in_node = false; + + loop { + match reader.read_event() { + Ok(Event::Eof) => break, + + Ok(Event::Start(e)) if e.name().as_ref() == b"node" => { + in_node = true; + } + + Ok(Event::End(e)) if e.name().as_ref() == b"node" => { + in_node = false; + } + + Ok(Event::Start(e)) | Ok(Event::Empty(e)) + if in_node && e.name().as_ref() == b"tag" => + { + let mut attrs = e.attributes(); + if attrs.next().unwrap().unwrap().value.as_ref() == b"highway" + && attrs.next().unwrap().unwrap().value.as_ref() == b"crossing" { + count += 1; + } + } + + Ok(_) => {} + Err(_) => break, + } + } + + count +} + +pub fn count_highway_crossings_cheat_and_early_out(buffer: &[u8]) -> usize { + let mut reader = Reader::from_reader(buffer); + reader.config_mut().check_end_names = false; + + let mut count = 0usize; + + loop { + match reader.read_event() { + Ok(Event::Eof) => break, + + Ok(Event::Start(e)) | Ok(Event::Empty(e)) + if e.name().as_ref() == b"tag" => + { + let mut attrs = e.attributes(); + if attrs.next().unwrap().unwrap().value.as_ref() == b"highway" + && attrs.next().unwrap().unwrap().value.as_ref() == b"crossing" { + count += 1; + } + } + + Ok(Event::Start(e)) | Ok(Event::Empty(e)) + if e.name().as_ref() == b"way" => + { + break; + } + + Ok(_) => {} + Err(_) => break, + } + } + + count +} + |
