Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make additional prefileter metadata public #1156

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions regex-automata/src/util/prefilter/mod.rs
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,20 @@ impl Prefilter {
}
}

/// Return the length of the longest needle
/// in this Prefilter
#[inline]
pub fn max_needle_len(&self) -> usize {
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
#[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 +452,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