Skip to main content

ct_regex_internal/matcher/
primitive.rs

1use std::fmt::{self, Debug};
2
3use crate::expr::IndexedCaptures;
4use crate::haystack::{HaystackItem, HaystackOf};
5use crate::matcher::{Matcher, impl_all_captures_single, impl_all_matches_single};
6use crate::sealed::Sealed;
7
8// Note: this type is no longer produced in macro generated type expressions. It hasn't been removed
9// because it makes testing other Matcher types with hand written code far easier.
10#[derive(#[automatically_derived]
impl<const N : u8> ::core::default::Default for Byte<N> {
    #[inline]
    fn default() -> Byte<N> { Byte {} }
}Default, #[automatically_derived]
impl<const N : u8> ::core::clone::Clone for Byte<N> {
    #[inline]
    fn clone(&self) -> Byte<N> { *self }
}Clone, #[automatically_derived]
impl<const N : u8> ::core::marker::Copy for Byte<N> { }Copy)]
11pub struct Byte<const N: u8>;
12
13impl<const N: u8> Sealed for Byte<N> {}
14
15impl<const N: u8> Matcher<u8> for Byte<N> {
16    fn matches<'a, H: HaystackOf<'a, u8>>(hay: &mut H) -> bool {
17        if hay.item() == Some(N) {
18            hay.progress();
19            true
20        } else {
21            false
22        }
23    }
24
25    u8
crate::matcher::AllMatchesSingle
u8
&mut H
hay
Self::AllMatches<'a, H>
crate::matcher::all_matches_single::<u8, H, Self>(hay);impl_all_matches_single!(u8);
26    u8
crate::matcher::AllCapturesSingle
u8
&mut H
hay
&mut IndexedCaptures
caps
Self::AllCaptures<'a, H>
crate::matcher::all_captures_single::<u8, H, Self>(hay, caps);impl_all_captures_single!(u8);
27}
28
29impl<const N: u8> Debug for Byte<N> {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.write_fmt(format_args!("{0:#04x}", N))write!(f, "{N:#04x}")
32    }
33}
34
35// Note: this type is no longer produced in macro generated type expressions. It hasn't been removed
36// because it makes testing other Matcher types with hand written code far easier.
37#[derive(#[automatically_derived]
impl<const A : u8, const B : u8> ::core::default::Default for ByteRange<A, B>
    {
    #[inline]
    fn default() -> ByteRange<A, B> { ByteRange {} }
}Default, #[automatically_derived]
impl<const A : u8, const B : u8> ::core::clone::Clone for ByteRange<A, B> {
    #[inline]
    fn clone(&self) -> ByteRange<A, B> { *self }
}Clone, #[automatically_derived]
impl<const A : u8, const B : u8> ::core::marker::Copy for ByteRange<A, B> { }Copy)]
38pub struct ByteRange<const A: u8, const B: u8>;
39
40impl<const A: u8, const B: u8> Sealed for ByteRange<A, B> {}
41
42impl<const A: u8, const B: u8> Matcher<u8> for ByteRange<A, B> {
43    fn matches<'a, H: HaystackOf<'a, u8>>(hay: &mut H) -> bool {
44        if let Some(byte) = hay.item()
45            && A <= byte
46            && byte <= B
47        {
48            hay.progress();
49            true
50        } else {
51            false
52        }
53    }
54
55    u8
crate::matcher::AllMatchesSingle
u8
&mut H
hay
Self::AllMatches<'a, H>
crate::matcher::all_matches_single::<u8, H, Self>(hay);impl_all_matches_single!(u8);
56    u8
crate::matcher::AllCapturesSingle
u8
&mut H
hay
&mut IndexedCaptures
caps
Self::AllCaptures<'a, H>
crate::matcher::all_captures_single::<u8, H, Self>(hay, caps);impl_all_captures_single!(u8);
57}
58
59impl<const A: u8, const B: u8> Debug for ByteRange<A, B> {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.write_fmt(format_args!("[{0:#04x}-{1:#04x}]", A, B))write!(f, "[{A:#04x}-{B:#04x}]")
62    }
63}
64
65// Note: this type is no longer produced in macro generated type expressions. It hasn't been removed
66// because it makes testing other Matcher types with hand written code far easier.
67#[derive(#[automatically_derived]
impl<const N : char> ::core::default::Default for Scalar<N> {
    #[inline]
    fn default() -> Scalar<N> { Scalar {} }
}Default, #[automatically_derived]
impl<const N : char> ::core::clone::Clone for Scalar<N> {
    #[inline]
    fn clone(&self) -> Scalar<N> { *self }
}Clone, #[automatically_derived]
impl<const N : char> ::core::marker::Copy for Scalar<N> { }Copy)]
68pub struct Scalar<const N: char>;
69
70impl<const N: char> Sealed for Scalar<N> {}
71
72impl<const N: char> Matcher<char> for Scalar<N> {
73    fn matches<'a, H: HaystackOf<'a, char>>(hay: &mut H) -> bool {
74        if hay.item() == Some(N) {
75            hay.progress();
76            true
77        } else {
78            false
79        }
80    }
81
82    char
crate::matcher::AllMatchesSingle
char
&mut H
hay
Self::AllMatches<'a, H>
crate::matcher::all_matches_single::<char, H, Self>(hay);impl_all_matches_single!(char);
83    char
crate::matcher::AllCapturesSingle
char
&mut H
hay
&mut IndexedCaptures
caps
Self::AllCaptures<'a, H>
crate::matcher::all_captures_single::<char, H, Self>(hay, caps);impl_all_captures_single!(char);
84}
85
86impl<const N: char> Debug for Scalar<N> {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        f.write_fmt(format_args!("{0}", N.escape_debug()))write!(f, "{}", N.escape_debug())
89    }
90}
91
92// Note: this type is no longer produced in macro generated type expressions. It hasn't been removed
93// because it makes testing other Matcher types with hand written code far easier.
94#[derive(#[automatically_derived]
impl<const A : char, const B : char> ::core::default::Default for
    ScalarRange<A, B> {
    #[inline]
    fn default() -> ScalarRange<A, B> { ScalarRange {} }
}Default, #[automatically_derived]
impl<const A : char, const B : char> ::core::clone::Clone for
    ScalarRange<A, B> {
    #[inline]
    fn clone(&self) -> ScalarRange<A, B> { *self }
}Clone, #[automatically_derived]
impl<const A : char, const B : char> ::core::marker::Copy for
    ScalarRange<A, B> {
}Copy)]
95pub struct ScalarRange<const A: char, const B: char>;
96
97impl<const A: char, const B: char> Sealed for ScalarRange<A, B> {}
98
99impl<const A: char, const B: char> Matcher<char> for ScalarRange<A, B> {
100    fn matches<'a, H: HaystackOf<'a, char>>(hay: &mut H) -> bool {
101        if let Some(scalar) = hay.item()
102            && A <= scalar
103            && scalar <= B
104        {
105            hay.progress();
106            true
107        } else {
108            false
109        }
110    }
111
112    char
crate::matcher::AllMatchesSingle
char
&mut H
hay
Self::AllMatches<'a, H>
crate::matcher::all_matches_single::<char, H, Self>(hay);impl_all_matches_single!(char);
113    char
crate::matcher::AllCapturesSingle
char
&mut H
hay
&mut IndexedCaptures
caps
Self::AllCaptures<'a, H>
crate::matcher::all_captures_single::<char, H, Self>(hay, caps);impl_all_captures_single!(char);
114}
115
116impl<const A: char, const B: char> Debug for ScalarRange<A, B> {
117    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118        f.write_fmt(format_args!("[{0}-{1}]", A.escape_debug(), B.escape_debug()))write!(f, "[{}-{}]", A.escape_debug(), B.escape_debug())
119    }
120}
121
122#[derive(#[automatically_derived]
impl ::core::default::Default for Always {
    #[inline]
    fn default() -> Always { Always {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for Always {
    #[inline]
    fn clone(&self) -> Always { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Always { }Copy)]
123pub struct Always;
124
125impl Sealed for Always {}
126
127impl<I: HaystackItem> Matcher<I> for Always {
128    fn matches<'a, H: HaystackOf<'a, I>>(_hay: &mut H) -> bool {
129        true
130    }
131
132    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);
133    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);
134}
135
136impl Debug for Always {
137    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138        f.write_fmt(format_args!("()"))write!(f, "()")
139    }
140}
141
142#[derive(#[automatically_derived]
impl ::core::default::Default for Start {
    #[inline]
    fn default() -> Start { Start {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for Start {
    #[inline]
    fn clone(&self) -> Start { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Start { }Copy)]
143pub struct Start;
144
145impl Sealed for Start {}
146
147impl<I: HaystackItem> Matcher<I> for Start {
148    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
149        hay.is_start()
150    }
151
152    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);
153    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);
154}
155
156impl Debug for Start {
157    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158        f.write_fmt(format_args!("\\A"))write!(f, "\\A")
159    }
160}
161
162#[derive(#[automatically_derived]
impl ::core::default::Default for End {
    #[inline]
    fn default() -> End { End {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for End {
    #[inline]
    fn clone(&self) -> End { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for End { }Copy)]
163pub struct End;
164
165impl Sealed for End {}
166
167impl<I: HaystackItem> Matcher<I> for End {
168    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
169        hay.is_end()
170    }
171
172    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);
173    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);
174}
175
176impl Debug for End {
177    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
178        f.write_fmt(format_args!("\\z"))write!(f, "\\z")
179    }
180}
181
182#[derive(#[automatically_derived]
impl ::core::default::Default for LineStart {
    #[inline]
    fn default() -> LineStart { LineStart {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for LineStart {
    #[inline]
    fn clone(&self) -> LineStart { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LineStart { }Copy)]
183pub struct LineStart;
184
185impl Sealed for LineStart {}
186
187impl<I: HaystackItem> Matcher<I> for LineStart {
188    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
189        hay.is_line_start()
190    }
191
192    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);
193    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);
194}
195
196impl Debug for LineStart {
197    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198        f.write_fmt(format_args!("^"))write!(f, "^")
199    }
200}
201
202#[derive(#[automatically_derived]
impl ::core::default::Default for LineEnd {
    #[inline]
    fn default() -> LineEnd { LineEnd {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for LineEnd {
    #[inline]
    fn clone(&self) -> LineEnd { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LineEnd { }Copy)]
203pub struct LineEnd;
204
205impl Sealed for LineEnd {}
206
207impl<I: HaystackItem> Matcher<I> for LineEnd {
208    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
209        hay.is_line_end()
210    }
211
212    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);
213    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);
214}
215
216impl Debug for LineEnd {
217    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218        f.write_fmt(format_args!("$"))write!(f, "$")
219    }
220}
221
222#[derive(#[automatically_derived]
impl ::core::default::Default for CRLFStart {
    #[inline]
    fn default() -> CRLFStart { CRLFStart {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for CRLFStart {
    #[inline]
    fn clone(&self) -> CRLFStart { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for CRLFStart { }Copy)]
223pub struct CRLFStart;
224
225impl Sealed for CRLFStart {}
226
227impl<I: HaystackItem> Matcher<I> for CRLFStart {
228    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
229        hay.is_crlf_start()
230    }
231
232    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);
233    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);
234}
235
236impl Debug for CRLFStart {
237    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238        f.write_fmt(format_args!("^"))write!(f, "^")
239    }
240}
241
242#[derive(#[automatically_derived]
impl ::core::default::Default for CRLFEnd {
    #[inline]
    fn default() -> CRLFEnd { CRLFEnd {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for CRLFEnd {
    #[inline]
    fn clone(&self) -> CRLFEnd { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for CRLFEnd { }Copy)]
243pub struct CRLFEnd;
244
245impl Sealed for CRLFEnd {}
246
247impl<I: HaystackItem> Matcher<I> for CRLFEnd {
248    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
249        hay.is_crlf_end()
250    }
251
252    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);
253    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);
254}
255
256impl Debug for CRLFEnd {
257    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
258        f.write_fmt(format_args!("$"))write!(f, "$")
259    }
260}