Skip to main content

ct_regex_internal/anchor/
basic.rs

1use std::fmt::Debug;
2use std::marker::PhantomData;
3use std::ops::ControlFlow;
4
5use crate::haystack::{Haystack, HaystackSlice};
6use crate::sealed::Sealed;
7
8pub trait Anchor: Sealed + Debug + Default + Clone + Copy {
9    /// Asserts that each anchor in this set could possibly succeed with the given haystack state.
10    /// In the presence of a start anchor, the haystack's position at the start doesn't need to be
11    /// checked again.
12    ///
13    /// The return value represents two things:
14    ///
15    /// - The outer [`ControlFlow`] represents whether the haystack has reached a point of
16    ///   no-return. If the value is [`ControlFlow::Break`], no more matches will succeed; no
17    ///   further attempts should be made to match against the haystack.
18    ///   For functions that return an option, it should be possible to try the return value with
19    ///   `.continue_value()?`.
20    ///
21    /// - If the value of the outer type is [`ControlFlow::Continue`], the inner [`bool`] represents
22    ///   whether the current position should be checked for a match. A value of `false` indicates
23    ///   that the haystack should be progressed before checking again.
24    fn assert<'a, H: Haystack<'a>>(hay: &H) -> ControlFlow<(), bool>;
25
26    /// Asserts that each anchor in this set could possibly succeed with the given haystack state.
27    /// In the presence of a start anchor, the haystack's position at the start doesn't need to be
28    /// checked again. This variant provide no information about whether the search should continue
29    /// and should be called by searches that intend to match the entirety of the provided haystack.
30    fn assert_fixed<'a, H: Haystack<'a>>(hay: &H) -> bool;
31}
32
33#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Start {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "Start")
    }
}Debug, #[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)]
34pub struct Start;
35
36impl Sealed for Start {}
37
38impl Anchor for Start {
39    fn assert<'a, H: Haystack<'a>>(hay: &H) -> ControlFlow<(), bool> {
40        if hay.is_start() {
41            ControlFlow::Continue(true)
42        } else {
43            ControlFlow::Break(())
44        }
45    }
46
47    fn assert_fixed<'a, H: Haystack<'a>>(_hay: &H) -> bool {
48        true
49    }
50}
51
52#[derive(#[automatically_derived]
impl<const N : usize> ::core::fmt::Debug for MinLen<N> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "MinLen")
    }
}Debug, #[automatically_derived]
impl<const N : usize> ::core::default::Default for MinLen<N> {
    #[inline]
    fn default() -> MinLen<N> { MinLen {} }
}Default, #[automatically_derived]
impl<const N : usize> ::core::clone::Clone for MinLen<N> {
    #[inline]
    fn clone(&self) -> MinLen<N> { *self }
}Clone, #[automatically_derived]
impl<const N : usize> ::core::marker::Copy for MinLen<N> { }Copy)]
53pub struct MinLen<const N: usize>;
54
55impl<const N: usize> Sealed for MinLen<N> {}
56
57impl<const N: usize> Anchor for MinLen<N> {
58    fn assert<'a, H: Haystack<'a>>(hay: &H) -> ControlFlow<(), bool> {
59        if hay.remainder_as_slice().as_bytes().len() >= N {
60            ControlFlow::Continue(true)
61        } else {
62            ControlFlow::Break(())
63        }
64    }
65
66    fn assert_fixed<'a, H: Haystack<'a>>(hay: &H) -> bool {
67        hay.remainder_as_slice().as_bytes().len() >= N
68    }
69}
70
71#[derive(#[automatically_derived]
impl<const N : usize> ::core::fmt::Debug for MaxLen<N> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "MaxLen")
    }
}Debug, #[automatically_derived]
impl<const N : usize> ::core::default::Default for MaxLen<N> {
    #[inline]
    fn default() -> MaxLen<N> { MaxLen {} }
}Default, #[automatically_derived]
impl<const N : usize> ::core::clone::Clone for MaxLen<N> {
    #[inline]
    fn clone(&self) -> MaxLen<N> { *self }
}Clone, #[automatically_derived]
impl<const N : usize> ::core::marker::Copy for MaxLen<N> { }Copy)]
72pub struct MaxLen<const N: usize>;
73
74impl<const N: usize> Sealed for MaxLen<N> {}
75
76impl<const N: usize> Anchor for MaxLen<N> {
77    fn assert<'a, H: Haystack<'a>>(_hay: &H) -> ControlFlow<(), bool> {
78        ControlFlow::Continue(true)
79    }
80
81    fn assert_fixed<'a, H: Haystack<'a>>(hay: &H) -> bool {
82        hay.remainder_as_slice().as_bytes().len() <= N
83    }
84}
85
86#[derive(#[automatically_derived]
impl<const N : usize> ::core::fmt::Debug for EndAndMaxLen<N> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "EndAndMaxLen")
    }
}Debug, #[automatically_derived]
impl<const N : usize> ::core::default::Default for EndAndMaxLen<N> {
    #[inline]
    fn default() -> EndAndMaxLen<N> { EndAndMaxLen {} }
}Default, #[automatically_derived]
impl<const N : usize> ::core::clone::Clone for EndAndMaxLen<N> {
    #[inline]
    fn clone(&self) -> EndAndMaxLen<N> { *self }
}Clone, #[automatically_derived]
impl<const N : usize> ::core::marker::Copy for EndAndMaxLen<N> { }Copy)]
87pub struct EndAndMaxLen<const N: usize>;
88
89impl<const N: usize> Sealed for EndAndMaxLen<N> {}
90
91impl<const N: usize> Anchor for EndAndMaxLen<N> {
92    fn assert<'a, H: Haystack<'a>>(hay: &H) -> ControlFlow<(), bool> {
93        if hay.remainder_as_slice().as_bytes().len() <= N {
94            ControlFlow::Continue(true)
95        } else {
96            ControlFlow::Continue(false)
97        }
98    }
99
100    fn assert_fixed<'a, H: Haystack<'a>>(hay: &H) -> bool {
101        hay.remainder_as_slice().as_bytes().len() <= N
102    }
103}
104
105#[derive(#[automatically_derived]
impl ::core::fmt::Debug for AnchorNone {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "AnchorNone")
    }
}Debug, #[automatically_derived]
impl ::core::default::Default for AnchorNone {
    #[inline]
    fn default() -> AnchorNone { AnchorNone {} }
}Default, #[automatically_derived]
impl ::core::clone::Clone for AnchorNone {
    #[inline]
    fn clone(&self) -> AnchorNone { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for AnchorNone { }Copy)]
106pub struct AnchorNone;
107
108impl Sealed for AnchorNone {}
109
110impl Anchor for AnchorNone {
111    fn assert<'a, H: Haystack<'a>>(_hay: &H) -> ControlFlow<(), bool> {
112        ControlFlow::Continue(true)
113    }
114
115    fn assert_fixed<'a, H: Haystack<'a>>(_hay: &H) -> bool {
116        true
117    }
118}
119
120#[derive(#[automatically_derived]
impl<A: ::core::fmt::Debug + Anchor, B: ::core::fmt::Debug + Anchor>
    ::core::fmt::Debug for AnchorPair<A, B> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "AnchorPair",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl<A: ::core::default::Default + Anchor, B: ::core::default::Default +
    Anchor> ::core::default::Default for AnchorPair<A, B> {
    #[inline]
    fn default() -> AnchorPair<A, B> {
        AnchorPair(::core::default::Default::default())
    }
}Default, #[automatically_derived]
impl<A: ::core::clone::Clone + Anchor, B: ::core::clone::Clone + Anchor>
    ::core::clone::Clone for AnchorPair<A, B> {
    #[inline]
    fn clone(&self) -> AnchorPair<A, B> {
        AnchorPair(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<A: ::core::marker::Copy + Anchor, B: ::core::marker::Copy + Anchor>
    ::core::marker::Copy for AnchorPair<A, B> {
}Copy)]
121pub struct AnchorPair<A: Anchor, B: Anchor>(pub PhantomData<(A, B)>);
122
123impl<A: Anchor, B: Anchor> Sealed for AnchorPair<A, B> {}
124
125impl<A: Anchor, B: Anchor> Anchor for AnchorPair<A, B> {
126    fn assert<'a, H: Haystack<'a>>(hay: &H) -> ControlFlow<(), bool> {
127        ControlFlow::Continue(
128            A::assert(hay)? && B::assert(hay)?
129        )
130    }
131
132    fn assert_fixed<'a, H: Haystack<'a>>(hay: &H) -> bool {
133        A::assert_fixed(hay) && B::assert_fixed(hay)
134    }
135}
136
137#[derive(#[automatically_derived]
impl<A: ::core::fmt::Debug + Anchor, B: ::core::fmt::Debug + Anchor,
    C: ::core::fmt::Debug + Anchor> ::core::fmt::Debug for AnchorSet<A, B, C>
    {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_tuple_field1_finish(f, "AnchorSet",
            &&self.0)
    }
}Debug, #[automatically_derived]
impl<A: ::core::default::Default + Anchor, B: ::core::default::Default +
    Anchor, C: ::core::default::Default + Anchor> ::core::default::Default for
    AnchorSet<A, B, C> {
    #[inline]
    fn default() -> AnchorSet<A, B, C> {
        AnchorSet(::core::default::Default::default())
    }
}Default, #[automatically_derived]
impl<A: ::core::clone::Clone + Anchor, B: ::core::clone::Clone + Anchor,
    C: ::core::clone::Clone + Anchor> ::core::clone::Clone for
    AnchorSet<A, B, C> {
    #[inline]
    fn clone(&self) -> AnchorSet<A, B, C> {
        AnchorSet(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<A: ::core::marker::Copy + Anchor, B: ::core::marker::Copy + Anchor,
    C: ::core::marker::Copy + Anchor> ::core::marker::Copy for
    AnchorSet<A, B, C> {
}Copy)]
138pub struct AnchorSet<A: Anchor, B: Anchor, C: Anchor>(pub PhantomData<(A, B, C)>);
139
140impl<A: Anchor, B: Anchor, C: Anchor> Sealed for AnchorSet<A, B, C> {}
141
142impl<A: Anchor, B: Anchor, C: Anchor> Anchor for AnchorSet<A, B, C> {
143    fn assert<'a, H: Haystack<'a>>(hay: &H) -> ControlFlow<(), bool> {
144        ControlFlow::Continue(
145            A::assert(hay)? && B::assert(hay)? && C::assert(hay)?
146        )
147    }
148
149    fn assert_fixed<'a, H: Haystack<'a>>(hay: &H) -> bool {
150        A::assert_fixed(hay) && B::assert_fixed(hay) && C::assert_fixed(hay)
151    }
152}