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