1use core::mem::size_of;
23use crate::util::wire::{self, DeserializeError, Endian, SerializeError};
45/// The kind of anchored starting configurations to support in a DFA.
6///
7/// Fully compiled DFAs need to be explicitly configured as to which anchored
8/// starting configurations to support. The reason for not just supporting
9/// everything unconditionally is that it can use more resources (such as
10/// memory and build time). The downside of this is that if you try to execute
11/// a search using an [`Anchored`](crate::Anchored) mode that is not supported
12/// by the DFA, then the search will return an error.
13#[derive(#[automatically_derived]
impl ::core::clone::Clone for StartKind {
#[inline]
fn clone(&self) -> StartKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for StartKind { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for StartKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
StartKind::Both => "Both",
StartKind::Unanchored => "Unanchored",
StartKind::Anchored => "Anchored",
})
}
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for StartKind {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for StartKind {
#[inline]
fn eq(&self, other: &StartKind) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq)]
14pub enum StartKind {
15/// Support both anchored and unanchored searches.
16Both,
17/// Support only unanchored searches. Requesting an anchored search will
18 /// panic.
19 ///
20 /// Note that even if an unanchored search is requested, the pattern itself
21 /// may still be anchored. For example, `^abc` will only match `abc` at the
22 /// start of a haystack. This will remain true, even if the regex engine
23 /// only supported unanchored searches.
24Unanchored,
25/// Support only anchored searches. Requesting an unanchored search will
26 /// panic.
27Anchored,
28}
2930impl StartKind {
31pub(crate) fn from_bytes(
32 slice: &[u8],
33 ) -> Result<(StartKind, usize), DeserializeError> {
34 wire::check_slice_len(slice, size_of::<u32>(), "start kind bytes")?;
35let (n, nr) = wire::try_read_u32(slice, "start kind integer")?;
36match n {
370 => Ok((StartKind::Both, nr)),
381 => Ok((StartKind::Unanchored, nr)),
392 => Ok((StartKind::Anchored, nr)),
40_ => Err(DeserializeError::generic("unrecognized start kind")),
41 }
42 }
4344pub(crate) fn write_to<E: Endian>(
45&self,
46 dst: &mut [u8],
47 ) -> Result<usize, SerializeError> {
48let nwrite = self.write_to_len();
49if dst.len() < nwrite {
50return Err(SerializeError::buffer_too_small("start kind"));
51 }
52let n = match *self {
53 StartKind::Both => 0,
54 StartKind::Unanchored => 1,
55 StartKind::Anchored => 2,
56 };
57 E::write_u32(n, dst);
58Ok(nwrite)
59 }
6061pub(crate) fn write_to_len(&self) -> usize {
62 size_of::<u32>()
63 }
6465#[cfg_attr(feature = "perf-inline", inline(always))]
66pub(crate) fn has_unanchored(&self) -> bool {
67#[allow(non_exhaustive_omitted_patterns)] match *self {
StartKind::Both | StartKind::Unanchored => true,
_ => false,
}matches!(*self, StartKind::Both | StartKind::Unanchored)68 }
6970#[cfg_attr(feature = "perf-inline", inline(always))]
71pub(crate) fn has_anchored(&self) -> bool {
72#[allow(non_exhaustive_omitted_patterns)] match *self {
StartKind::Both | StartKind::Anchored => true,
_ => false,
}matches!(*self, StartKind::Both | StartKind::Anchored)73 }
74}