ct_regex_internal/haystack/ext/
bstr.rs

1use std::ops::Range;
2
3use bstr::BStr;
4
5use crate::haystack::{HaystackIter, HaystackSlice, IntoHaystack};
6
7impl<'a> HaystackSlice<'a> for &'a BStr {
8    type Item = u8;
9}
10
11/// A haystack type for matching against the [`u8`]s in a [`&'a BStr`](bstr::BStr). This type is a
12/// very basic example of how the haystack traits can be implemented outside of the crate itself.
13#[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for BStrStack<'a> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "BStrStack",
            "inner", &self.inner, "index", &&self.index)
    }
}Debug, #[automatically_derived]
impl<'a> ::core::clone::Clone for BStrStack<'a> {
    #[inline]
    fn clone(&self) -> BStrStack<'a> {
        BStrStack {
            inner: ::core::clone::Clone::clone(&self.inner),
            index: ::core::clone::Clone::clone(&self.index),
        }
    }
}Clone)]
14pub struct BStrStack<'a> {
15    inner: &'a BStr,
16    index: usize,
17}
18
19impl<'a> IntoHaystack<'a, BStrStack<'a>> for &'a BStr {
20    fn into_haystack(self) -> BStrStack<'a> {
21        BStrStack {
22            inner: self,
23            index: 0,
24        }
25    }
26}
27
28impl<'a> Iterator for BStrStack<'a> {
29    type Item = u8;
30
31    fn next(&mut self) -> Option<Self::Item> {
32        let byte = self.inner.get(self.index).copied();
33
34        if byte.is_some() {
35            self.index += 1;
36        }
37
38        byte
39    }
40}
41
42impl<'a> HaystackIter<'a> for BStrStack<'a> {
43    type Slice = &'a BStr;
44
45    fn current_item(&self) -> Option<Self::Item> {
46        self.inner.get(self.index).copied()
47    }
48
49    fn current_index(&self) -> usize {
50        self.index
51    }
52
53    fn whole_slice(&self) -> Self::Slice {
54        self.inner
55    }
56
57    fn remainder_as_slice(&self) -> Self::Slice {
58        &self.inner[self.index..]
59    }
60
61    fn slice_with(&self, range: Range<usize>) -> Self::Slice {
62        &self.inner[range]
63    }
64
65    fn go_to(&mut self, index: usize) {
66        self.index = index;
67    }
68}