Skip to content

Commit

Permalink
make additional prefileter metadata public
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalkuthe committed Jan 25, 2024
1 parent 0c09903 commit 867da13
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions regex-automata/src/util/prefilter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ pub struct Prefilter {
pre: Arc<dyn PrefilterI>,
#[cfg(feature = "alloc")]
is_fast: bool,
#[cfg(feature = "alloc")]
max_needle_len: usize,
}

impl Prefilter {
Expand Down Expand Up @@ -202,12 +204,19 @@ impl Prefilter {
kind: MatchKind,
needles: &[B],
) -> Option<Prefilter> {
Choice::new(kind, needles).and_then(Prefilter::from_choice)
Choice::new(kind, needles).and_then(|choice| {
let max_needle_len =
needles.iter().map(|b| b.as_ref().len()).max().unwrap_or(0);
Prefilter::from_choice(choice, max_needle_len)
})
}

/// This turns a prefilter selection into a `Prefilter`. That is, in turns
/// the enum given into a trait object.
fn from_choice(choice: Choice) -> Option<Prefilter> {
fn from_choice(
choice: Choice,
max_needle_len: usize,
) -> Option<Prefilter> {
#[cfg(not(feature = "alloc"))]
{
None
Expand All @@ -224,7 +233,7 @@ impl Prefilter {
Choice::AhoCorasick(p) => Arc::new(p),
};
let is_fast = pre.is_fast();
Some(Prefilter { pre, is_fast })
Some(Prefilter { pre, is_fast, max_needle_len })
}
}

Expand Down Expand Up @@ -411,6 +420,19 @@ impl Prefilter {
}
}

/// Return the length of the longest needle
/// in this Prefilter
pub fn max_needle_len(&self) -> usize {
#[cfg(not(feature = "alloc"))]
{
unreachable!()
}
#[cfg(feature = "alloc")]
{
self.max_needle_len
}
}

/// Implementations might return true here if they believe themselves to
/// be "fast." The concept of "fast" is deliberately left vague, but in
/// practice this usually corresponds to whether it's believed that SIMD
Expand All @@ -429,7 +451,7 @@ impl Prefilter {
/// *know* a prefilter will be fast without actually trying the prefilter.
/// (Which of course we cannot afford to do.)
#[inline]
pub(crate) fn is_fast(&self) -> bool {
pub fn is_fast(&self) -> bool {
#[cfg(not(feature = "alloc"))]
{
unreachable!()
Expand Down

0 comments on commit 867da13

Please sign in to comment.