Skip to main content

ct_regex_internal/matcher/
helper.rs

1use std::option;
2
3use crate::expr::IndexedCaptures;
4use crate::haystack::{HaystackItem, HaystackOf};
5use crate::matcher::Matcher;
6
7pub type AllMatchesSingle = option::IntoIter<usize>;
8
9pub fn all_matches_single<'a, I: HaystackItem, H: HaystackOf<'a, I>, M: Matcher<I>>(
10    hay: &mut H,
11) -> AllMatchesSingle {
12    if M::matches(hay) {
13        Some(hay.index()).into_iter()
14    } else {
15        None.into_iter()
16    }
17}
18
19// This is just a helper to get around the fact that there are no default associated types yet.
20#[macro_export]
21#[doc(hidden)]
22macro_rules! impl_all_matches_single {
23    ($I:ident) => {
24        type AllMatches<'a, H: HaystackOf<'a, $I>> = $crate::matcher::AllMatchesSingle;
25
26        fn all_matches<'a, H: HaystackOf<'a, $I>>(hay: &mut H) -> Self::AllMatches<'a, H> {
27            $crate::matcher::all_matches_single::<$I, H, Self>(hay)
28        }
29    };
30}
31
32#[doc(inline)]
33pub use impl_all_matches_single;
34
35pub type AllCapturesSingle = option::IntoIter<(usize, IndexedCaptures)>;
36
37pub fn all_captures_single<'a, I: HaystackItem, H: HaystackOf<'a, I>, M: Matcher<I>>(
38    hay: &mut H,
39    caps: &mut IndexedCaptures,
40) -> AllCapturesSingle {
41    if M::captures(hay, caps) {
42        Some((hay.index(), caps.clone())).into_iter()
43    } else {
44        None.into_iter()
45    }
46}
47
48// This is just a helper to get around the fact that there are no default associated types yet.
49#[macro_export]
50#[doc(hidden)]
51macro_rules! impl_all_captures_single {
52    ($I:ident) => {
53        type AllCaptures<'a, H: HaystackOf<'a, $I>> = $crate::matcher::AllCapturesSingle;
54
55        fn all_captures<'a, H: HaystackOf<'a, $I>>(
56            hay: &mut H,
57            caps: &mut IndexedCaptures,
58        ) -> Self::AllCaptures<'a, H> {
59            $crate::matcher::all_captures_single::<$I, H, Self>(hay, caps)
60        }
61    };
62}
63
64#[doc(inline)]
65pub use impl_all_captures_single;