Skip to main content

ct_regex_internal/matcher/
capture.rs

1use std::fmt::{self, Debug};
2use std::hash::Hash;
3use std::iter::FusedIterator;
4use std::marker::PhantomData;
5
6use crate::expr::IndexedCaptures;
7use crate::haystack::{HaystackItem, HaystackOf};
8use crate::matcher::Matcher;
9use crate::sealed::Sealed;
10
11#[derive(#[automatically_derived]
impl<'a, I: ::core::fmt::Debug, H: ::core::fmt::Debug, A: ::core::fmt::Debug,
    const N : usize> ::core::fmt::Debug for AllCapturesGroup<'a, I, H, A, N>
    where I: HaystackItem, H: HaystackOf<'a, I>, A: Matcher<I>,
    A::AllCaptures<'a, H>: ::core::fmt::Debug {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "AllCapturesGroup", "captures", &self.captures, "start",
            &&self.start)
    }
}Debug, #[automatically_derived]
impl<'a, I: ::core::clone::Clone, H: ::core::clone::Clone,
    A: ::core::clone::Clone, const N : usize> ::core::clone::Clone for
    AllCapturesGroup<'a, I, H, A, N> where I: HaystackItem,
    H: HaystackOf<'a, I>, A: Matcher<I>,
    A::AllCaptures<'a, H>: ::core::clone::Clone {
    #[inline]
    fn clone(&self) -> AllCapturesGroup<'a, I, H, A, N> {
        AllCapturesGroup {
            captures: ::core::clone::Clone::clone(&self.captures),
            start: ::core::clone::Clone::clone(&self.start),
        }
    }
}Clone, #[automatically_derived]
impl<'a, I: ::core::hash::Hash, H: ::core::hash::Hash, A: ::core::hash::Hash,
    const N : usize> ::core::hash::Hash for AllCapturesGroup<'a, I, H, A, N>
    where I: HaystackItem, H: HaystackOf<'a, I>, A: Matcher<I>,
    A::AllCaptures<'a, H>: ::core::hash::Hash {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.captures, state);
        ::core::hash::Hash::hash(&self.start, state)
    }
}Hash)]
12pub struct AllCapturesGroup<'a, I, H, A, const N: usize>
13where
14    I: HaystackItem,
15    H: HaystackOf<'a, I>,
16    A: Matcher<I>,
17{
18    captures: A::AllCaptures<'a, H>,
19    start: usize,
20}
21
22impl<'a, I, H, A, const N: usize> Iterator for AllCapturesGroup<'a, I, H, A, N>
23where
24    I: HaystackItem,
25    H: HaystackOf<'a, I>,
26    A: Matcher<I>,
27{
28    type Item = (usize, IndexedCaptures);
29
30    fn next(&mut self) -> Option<Self::Item> {
31        let (state_fork, mut caps_fork) = self.captures.next()?;
32        caps_fork.push(N, self.start..state_fork);
33        Some((state_fork, caps_fork))
34    }
35}
36
37impl<'a, I, H, A, const N: usize> FusedIterator for AllCapturesGroup<'a, I, H, A, N>
38where
39    I: HaystackItem,
40    H: HaystackOf<'a, I>,
41    A: Matcher<I>,
42{}
43
44#[derive(#[automatically_derived]
impl<I: ::core::default::Default + HaystackItem, A: ::core::default::Default +
    Matcher<I>, const N : usize> ::core::default::Default for
    CaptureGroup<I, A, N> {
    #[inline]
    fn default() -> CaptureGroup<I, A, N> {
        CaptureGroup(::core::default::Default::default(),
            ::core::default::Default::default())
    }
}Default, #[automatically_derived]
impl<I: ::core::clone::Clone + HaystackItem, A: ::core::clone::Clone +
    Matcher<I>, const N : usize> ::core::clone::Clone for
    CaptureGroup<I, A, N> {
    #[inline]
    fn clone(&self) -> CaptureGroup<I, A, N> {
        CaptureGroup(::core::clone::Clone::clone(&self.0),
            ::core::clone::Clone::clone(&self.1))
    }
}Clone, #[automatically_derived]
impl<I: ::core::marker::Copy + HaystackItem, A: ::core::marker::Copy +
    Matcher<I>, const N : usize> ::core::marker::Copy for
    CaptureGroup<I, A, N> {
}Copy)]
45pub struct CaptureGroup<I: HaystackItem, A: Matcher<I>, const N: usize>(
46    pub(crate) PhantomData<I>,
47    pub(crate) PhantomData<A>,
48);
49
50impl<I: HaystackItem, A: Matcher<I>, const N: usize> Sealed for CaptureGroup<I, A, N> {}
51
52impl<I: HaystackItem, A: Matcher<I>, const N: usize> Matcher<I> for CaptureGroup<I, A, N> {
53    type AllMatches<'a, H: HaystackOf<'a, I>> = A::AllMatches<'a, H>;
54    type AllCaptures<'a, H: HaystackOf<'a, I>> = AllCapturesGroup<'a, I, H, A, N>;
55
56    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
57        A::matches(hay)
58    }
59
60    fn all_matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> Self::AllMatches<'a, H> {
61        A::all_matches(hay)
62    }
63
64    fn captures<'a, H: HaystackOf<'a, I>>(hay: &mut H, caps: &mut IndexedCaptures) -> bool {
65        let start = hay.index();
66        if A::captures(hay, caps) {
67            caps.push(N, start..hay.index());
68            true
69        } else {
70            false
71        }
72    }
73
74    fn all_captures<'a, H: HaystackOf<'a, I>>(
75        hay: &mut H,
76        caps: &mut IndexedCaptures,
77    ) -> Self::AllCaptures<'a, H> {
78        AllCapturesGroup {
79            start: hay.index(),
80            captures: A::all_captures(hay, caps),
81        }
82    }
83}
84
85impl<I: HaystackItem, A: Matcher<I>, const N: usize> Debug for CaptureGroup<I, A, N> {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        f.write_fmt(format_args!("({0:?})", A::default()))write!(f, "({:?})", A::default())
88    }
89}