Skip to main content

ct_regex_internal/matcher/
quantifier.rs

1use std::fmt::{self, Debug};
2use std::marker::PhantomData;
3use std::{iter, vec};
4
5use crate::expr::IndexedCaptures;
6use crate::haystack::{HaystackItem, HaystackOf};
7use crate::matcher::{LazyMatcher, Matcher, impl_all_captures_single, impl_all_matches_single};
8
9#[derive(#[automatically_derived]
impl<I: ::core::default::Default + HaystackItem, A: ::core::default::Default +
    Matcher<I>, const N : usize> ::core::default::Default for
    QuantifierN<I, A, N> {
    #[inline]
    fn default() -> QuantifierN<I, A, N> {
        QuantifierN(::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 QuantifierN<I, A, N>
    {
    #[inline]
    fn clone(&self) -> QuantifierN<I, A, N> {
        QuantifierN(::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 QuantifierN<I, A, N>
    {
}Copy)]
10pub struct QuantifierN<I: HaystackItem, A: Matcher<I>, const N: usize>(
11    pub PhantomData<I>,
12    pub PhantomData<A>,
13);
14
15impl<I: HaystackItem, A: Matcher<I>, const N: usize> Matcher<I> for QuantifierN<I, A, N> {
16    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
17        let mut count = 0;
18        while A::matches(hay) {
19            count += 1;
20        }
21        count == N
22    }
23
24    I
crate::matcher::AllMatchesSingle
I
&mut H
hay
Self::AllMatches<'a, H>
crate::matcher::all_matches_single::<I, H, Self>(hay);impl_all_matches_single!(I);
25
26    fn captures<'a, H: HaystackOf<'a, I>>(hay: &mut H, caps: &mut IndexedCaptures) -> bool {
27        let mut count = 0;
28        while A::captures(hay, caps) {
29            count += 1;
30        }
31        count == N
32    }
33
34    I
crate::matcher::AllCapturesSingle
I
&mut H
hay
&mut IndexedCaptures
caps
Self::AllCaptures<'a, H>
crate::matcher::all_captures_single::<I, H, Self>(hay, caps);impl_all_captures_single!(I);
35}
36
37impl<I: HaystackItem, A: Matcher<I>, const N: usize> Debug for QuantifierN<I, A, N> {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        f.write_fmt(format_args!("{0:?}{{{1}}}", A::default(), N))write!(f, "{:?}{{{N}}}", A::default())
40    }
41}
42
43pub type AllMatchesMultiple = iter::Rev<vec::IntoIter<usize>>;
44pub type AllCapturesMultiple = iter::Rev<vec::IntoIter<(usize, IndexedCaptures)>>;
45
46#[derive(#[automatically_derived]
impl<I: ::core::default::Default + HaystackItem, A: ::core::default::Default +
    Matcher<I>, const N : usize> ::core::default::Default for
    QuantifierNOrMore<I, A, N> {
    #[inline]
    fn default() -> QuantifierNOrMore<I, A, N> {
        QuantifierNOrMore(::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
    QuantifierNOrMore<I, A, N> {
    #[inline]
    fn clone(&self) -> QuantifierNOrMore<I, A, N> {
        QuantifierNOrMore(::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
    QuantifierNOrMore<I, A, N> {
}Copy)]
47pub struct QuantifierNOrMore<I: HaystackItem, A: Matcher<I>, const N: usize>(
48    pub PhantomData<I>,
49    pub PhantomData<A>,
50);
51
52impl<I: HaystackItem, A: Matcher<I>, const N: usize> Matcher<I> for QuantifierNOrMore<I, A, N> {
53    type AllMatches<'a, H: HaystackOf<'a, I>> = AllMatchesMultiple;
54    type AllCaptures<'a, H: HaystackOf<'a, I>> = AllCapturesMultiple;
55
56    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
57        let mut count = 0;
58        while A::matches(hay) {
59            count += 1;
60        }
61        count >= N
62    }
63
64    // This **has to be** evaluated eagerly, so we use a vec.
65    fn all_matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> Self::AllMatches<'a, H> {
66        Self::lazy_all_matches(hay)
67            .collect::<Vec<_>>()
68            .into_iter()
69            .rev()
70    }
71
72    fn captures<'a, H: HaystackOf<'a, I>>(hay: &mut H, caps: &mut IndexedCaptures) -> bool {
73        let mut count = 0;
74        while A::captures(hay, caps) {
75            count += 1;
76        }
77        count >= N
78    }
79
80    fn all_captures<'a, H: HaystackOf<'a, I>>(
81        hay: &mut H,
82        caps: &mut IndexedCaptures,
83    ) -> Self::AllCaptures<'a, H> {
84        Self::lazy_all_captures(hay, caps)
85            .collect::<Vec<_>>()
86            .into_iter()
87            .rev()
88    }
89}
90
91impl<I: HaystackItem, A: Matcher<I>, const N: usize> Debug for QuantifierNOrMore<I, A, N> {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        f.write_fmt(format_args!("{0:?}{{{1},}}", A::default(), N))write!(f, "{:?}{{{N},}}", A::default())
94    }
95}
96
97#[derive(#[automatically_derived]
impl<I: ::core::default::Default + HaystackItem, A: ::core::default::Default +
    Matcher<I>, const N : usize, const M : usize> ::core::default::Default for
    QuantifierNToM<I, A, N, M> {
    #[inline]
    fn default() -> QuantifierNToM<I, A, N, M> {
        QuantifierNToM(::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, const M : usize> ::core::clone::Clone for
    QuantifierNToM<I, A, N, M> {
    #[inline]
    fn clone(&self) -> QuantifierNToM<I, A, N, M> {
        QuantifierNToM(::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, const M : usize> ::core::marker::Copy for
    QuantifierNToM<I, A, N, M> {
}Copy)]
98pub struct QuantifierNToM<I: HaystackItem, A: Matcher<I>, const N: usize, const M: usize>(
99    pub PhantomData<I>,
100    pub PhantomData<A>,
101);
102
103impl<I: HaystackItem, A: Matcher<I>, const N: usize, const M: usize> Matcher<I> for QuantifierNToM<I, A, N, M> {
104    type AllMatches<'a, H: HaystackOf<'a, I>> = AllMatchesMultiple;
105    type AllCaptures<'a, H: HaystackOf<'a, I>> = AllCapturesMultiple;
106
107    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
108        let mut count = 0;
109        loop {
110            if count >= N && count == M {
111                return true;
112            }
113            if A::matches(hay) {
114                count += 1;
115            } else {
116                break;
117            }
118        }
119        N <= count && count <= M
120    }
121
122    fn all_matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> Self::AllMatches<'a, H> {
123        Self::lazy_all_matches(hay)
124            .collect::<Vec<_>>()
125            .into_iter()
126            .rev()
127    }
128
129    fn captures<'a, H: HaystackOf<'a, I>>(hay: &mut H, caps: &mut IndexedCaptures) -> bool {
130        let mut count = 0;
131        loop {
132            if count >= N && count == M {
133                return true;
134            }
135            if A::captures(hay, caps) {
136                count += 1;
137            } else {
138                break;
139            }
140        }
141        N <= count && count <= M
142    }
143
144    fn all_captures<'a, H: HaystackOf<'a, I>>(
145        hay: &mut H,
146        caps: &mut IndexedCaptures,
147    ) -> Self::AllCaptures<'a, H> {
148        Self::lazy_all_captures(hay, caps)
149            .collect::<Vec<_>>()
150            .into_iter()
151            .rev()
152    }
153}
154
155impl<I: HaystackItem, A: Matcher<I>, const N: usize, const M: usize> Debug for QuantifierNToM<I, A, N, M> {
156    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157        f.write_fmt(format_args!("{0:?}{{{1},{2}}}", A::default(), N, M))write!(f, "{:?}{{{N},{M}}}", A::default())
158    }
159}