ct_regex_internal/haystack/ext/
arcstr.rs

1use std::{marker::PhantomData, ops::Range};
2
3use arcstr::{ArcStr, Substr};
4
5use crate::haystack::{HaystackIter, HaystackSlice, IntoHaystack, get_first_char};
6
7fn get_item<I>((_, item): (usize, I)) -> I { item }
8
9impl<'a> HaystackSlice<'a> for Substr {
10    type Item = char;
11}
12
13/// A haystack type for matching against the [`char`]s in an [`ArcStr`](arcstr::ArcStr). Rather than
14/// actual `ArcStr`s, this type internally stores [`Substr`](arcstr::Substr)s. Although
15/// [`IntoHaystack`] is implemented for `ArcStr`, the associated `Slice` type for this `Haystack` is
16/// `Substr`.
17#[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)]
18pub struct ArcStrStack<'a> {
19    inner: ArcStr,
20    index: usize,
21    _phantom: PhantomData<&'a ()>,
22}
23
24impl<'a> IntoHaystack<'a, ArcStrStack<'a>> for ArcStr {
25    fn into_haystack(self) -> ArcStrStack<'a> {
26        ArcStrStack {
27            inner: self,
28            index: 0,
29            _phantom: PhantomData,
30        }
31    }
32}
33
34impl<'a> Iterator for ArcStrStack<'a> {
35    type Item = char;
36
37    fn next(&mut self) -> Option<Self::Item> {
38        let (width, first) = get_first_char(&self.inner);
39        // The width won't exceed the remaining slice, so it can't overflow then length.
40        self.index += width;
41        first
42    }
43}
44
45impl<'a> HaystackIter<'a> for ArcStrStack<'a> {
46    type Slice = Substr;
47
48    fn current_item(&self) -> Option<Self::Item> {
49        get_item(get_first_char(&self.inner))
50    }
51
52    fn current_index(&self) -> usize {
53        self.index
54    }
55
56    fn whole_slice(&self) -> Self::Slice {
57        Substr::full(self.inner.clone())
58    }
59
60    fn remainder_as_slice(&self) -> Self::Slice {
61        self.inner.substr(self.index..)
62    }
63
64    fn slice_with(&self, range: Range<usize>) -> Self::Slice {
65        self.inner.substr(range)
66    }
67
68    fn go_to(&mut self, index: usize) {
69        self.index = index;
70    }
71}