summaryrefslogtreecommitdiff
path: root/benchmarks/quick-xml/src/count_highway_crossings.rs
blob: 9b68142970c5d116cd908d5ffe18d3b43d63835b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
}