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