Skip to main content

ct_regex_internal/haystack/ext/
arcstr.rs

1use std::marker::PhantomData;
2use std::ops::Range;
3
4use arcstr::{ArcStr, Substr};
5
6use crate::haystack::{
7    Haystack, HaystackSlice, IntoHaystack, first_char, first_char_and_width,
8};
9
10impl<'a> HaystackSlice<'a> for Substr {
11    type Item = char;
12
13    fn slice_with(&self, range: Range<usize>) -> Self {
14        self.substr(range)
15    }
16
17    fn as_bytes(&self) -> &[u8] {
18        Substr::as_str(self).as_bytes()
19    }
20}
21
22/// A haystack type for matching against the [`char`]s in an [`ArcStr`]. Although [`IntoHaystack`]
23/// is implemented for `ArcStr`, the associated `Slice` type for this `Haystack` is `Substr`.
24#[derive(#[automatically_derived]
impl<'a> ::core::fmt::Debug for ArcStrStack<'a> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "ArcStrStack",
            "inner", &self.inner, "index", &self.index, "_phantom",
            &&self._phantom)
    }
}Debug, #[automatically_derived]
impl<'a> ::core::clone::Clone for ArcStrStack<'a> {
    #[inline]
    fn clone(&self) -> ArcStrStack<'a> {
        ArcStrStack {
            inner: ::core::clone::Clone::clone(&self.inner),
            index: ::core::clone::Clone::clone(&self.index),
            _phantom: ::core::clone::Clone::clone(&self._phantom),
        }
    }
}Clone, #[automatically_derived]
impl<'a> ::core::hash::Hash for ArcStrStack<'a> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.inner, state);
        ::core::hash::Hash::hash(&self.index, state);
        ::core::hash::Hash::hash(&self._phantom, state)
    }
}Hash)]
25pub struct ArcStrStack<'a> {
26    inner: ArcStr,
27    index: usize,
28    _phantom: PhantomData<&'a ()>,
29}
30
31impl<'a> IntoHaystack<'a, ArcStrStack<'a>> for ArcStr {
32    fn into_haystack(self) -> ArcStrStack<'a> {
33        ArcStrStack {
34            inner: self,
35            index: 0,
36            _phantom: PhantomData,
37        }
38    }
39}
40
41impl<'a> Iterator for ArcStrStack<'a> {
42    type Item = char;
43
44    fn next(&mut self) -> Option<Self::Item> {
45        let (width, first) = first_char_and_width(&self.inner);
46        // The width won't exceed the remaining slice, so it can't overflow then length.
47        self.index += width;
48        first
49    }
50}
51
52impl<'a> Haystack<'a> for ArcStrStack<'a> {
53    type Slice = Substr;
54
55    fn item(&self) -> Option<Self::Item> {
56        first_char(&self.inner[self.index..])
57    }
58
59    fn prev_item(&self) -> Option<Self::Item> {
60        let prev_index = self.inner.floor_char_boundary(self.index.checked_sub(1)?);
61        first_char(&self.inner[prev_index..])
62    }
63
64    fn index(&self) -> usize {
65        self.index
66    }
67
68    fn inner_slice(&self) -> Self::Slice {
69        Substr::full(self.inner.clone())
70    }
71
72    fn remainder_as_slice(&self) -> Self::Slice {
73        self.inner.substr(self.index..)
74    }
75
76    fn go_to(&mut self, index: usize) {
77        self.index = index;
78    }
79}