Skip to main content

ct_regex_internal/matcher/
literal.rs

1use std::fmt::{self, Debug};
2use std::marker::PhantomData;
3
4use crate::expr::IndexedCaptures;
5use crate::haystack::{HaystackItem, HaystackOf, HaystackSlice};
6use crate::matcher::{Matcher, impl_all_captures_single, impl_all_matches_single};
7use crate::sealed::Sealed;
8
9pub trait Literal: Default + Clone + Copy {
10    const LITERAL: &[u8];
11}
12
13#[derive(#[automatically_derived]
impl<L: ::core::default::Default + Literal> ::core::default::Default for
    LiteralMatcher<L> {
    #[inline]
    fn default() -> LiteralMatcher<L> {
        LiteralMatcher(::core::default::Default::default())
    }
}Default, #[automatically_derived]
impl<L: ::core::clone::Clone + Literal> ::core::clone::Clone for
    LiteralMatcher<L> {
    #[inline]
    fn clone(&self) -> LiteralMatcher<L> {
        LiteralMatcher(::core::clone::Clone::clone(&self.0))
    }
}Clone, #[automatically_derived]
impl<L: ::core::marker::Copy + Literal> ::core::marker::Copy for
    LiteralMatcher<L> {
}Copy)]
14pub struct LiteralMatcher<L: Literal>(pub PhantomData<L>);
15
16impl<L: Literal> Sealed for LiteralMatcher<L> {}
17
18impl<L: Literal, I: HaystackItem> Matcher<I> for LiteralMatcher<L> {
19    fn matches<'a, H: HaystackOf<'a, I>>(hay: &mut H) -> bool {
20        let success = hay.remainder_as_slice()
21            .as_bytes()
22            .starts_with(L::LITERAL);
23
24        // This should avoid unnecessary branching here, because hay's state is undefined in the
25        // case of a fail.
26        hay.skip(L::LITERAL.len());
27
28        success
29    }
30
31    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);
32    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);
33}
34
35impl<L: Literal> Debug for LiteralMatcher<L> {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        f.write_fmt(format_args!("{0:?}", L::LITERAL))write!(f, "{:?}", L::LITERAL)
38    }
39}