Skip to main content

ct_regex_internal/anchor/
basic.rs

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