rsonpath/classification/structural/shared/
mask_64.rs

1use crate::classification::{
2    structural::{BracketType, Structural},
3    QuoteClassifiedBlock,
4};
5use std::ops::Deref;
6
7const SIZE: usize = 64;
8
9pub(crate) struct StructuralsBlock<B> {
10    pub(crate) quote_classified: QuoteClassifiedBlock<B, u64, SIZE>,
11    pub(crate) structural_mask: u64,
12}
13
14impl<B> StructuralsBlock<B> {
15    #[inline(always)]
16    pub(crate) fn new(block: QuoteClassifiedBlock<B, u64, SIZE>, structural_mask: u64) -> Self {
17        Self {
18            quote_classified: block,
19            structural_mask,
20        }
21    }
22
23    #[inline(always)]
24    pub(crate) fn is_empty(&self) -> bool {
25        self.structural_mask == 0
26    }
27
28    #[inline(always)]
29    pub(crate) fn get_idx(&self) -> u32 {
30        self.structural_mask.trailing_zeros()
31    }
32}
33
34impl<B: Deref<Target = [u8]>> Iterator for StructuralsBlock<B> {
35    type Item = Structural;
36
37    #[inline(always)]
38    fn next(&mut self) -> Option<Structural> {
39        let idx = self.get_idx() as usize;
40        (idx < SIZE).then(|| {
41            let bit_mask = 1 << idx;
42
43            self.structural_mask ^= bit_mask;
44
45            // The last match being a catch-all *is important*.
46            // It has major performance implications, since the jump table generated here is a hot path for the engine.
47            // Changing this match must be accompanied with benchmark runs to make sure perf does not regress.
48            match self.quote_classified.block[idx] {
49                b':' => Structural::Colon(idx),
50                b'{' => Structural::Opening(BracketType::Curly, idx),
51                b'[' => Structural::Opening(BracketType::Square, idx),
52                b',' => Structural::Comma(idx),
53                b'}' => Structural::Closing(BracketType::Curly, idx),
54                _ => Structural::Closing(BracketType::Square, idx),
55            }
56        })
57    }
58}
59
60impl<B: Deref<Target = [u8]>> std::iter::FusedIterator for StructuralsBlock<B> {}