Skip to content

Commit 6cffa53

Browse files
committed
make additional prefileter metadata public
1 parent 0c09903 commit 6cffa53

File tree

1 file changed

+19
-4
lines changed
  • regex-automata/src/util/prefilter

1 file changed

+19
-4
lines changed

regex-automata/src/util/prefilter/mod.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ pub struct Prefilter {
146146
pre: Arc<dyn PrefilterI>,
147147
#[cfg(feature = "alloc")]
148148
is_fast: bool,
149+
#[cfg(feature = "alloc")]
150+
max_needle_len: usize,
149151
}
150152

151153
impl Prefilter {
@@ -202,12 +204,19 @@ impl Prefilter {
202204
kind: MatchKind,
203205
needles: &[B],
204206
) -> Option<Prefilter> {
205-
Choice::new(kind, needles).and_then(Prefilter::from_choice)
207+
Choice::new(kind, needles).and_then(|choice| {
208+
let max_needle_len =
209+
needles.iter().map(|b| b.as_ref().len()).max().unwrap_or(0);
210+
Prefilter::from_choice(choice, max_needle_len)
211+
})
206212
}
207213

208214
/// This turns a prefilter selection into a `Prefilter`. That is, in turns
209215
/// the enum given into a trait object.
210-
fn from_choice(choice: Choice) -> Option<Prefilter> {
216+
fn from_choice(
217+
choice: Choice,
218+
max_needle_len: usize,
219+
) -> Option<Prefilter> {
211220
#[cfg(not(feature = "alloc"))]
212221
{
213222
None
@@ -224,7 +233,7 @@ impl Prefilter {
224233
Choice::AhoCorasick(p) => Arc::new(p),
225234
};
226235
let is_fast = pre.is_fast();
227-
Some(Prefilter { pre, is_fast })
236+
Some(Prefilter { pre, is_fast, max_needle_len })
228237
}
229238
}
230239

@@ -411,6 +420,12 @@ impl Prefilter {
411420
}
412421
}
413422

423+
/// Return the length of the longest needle
424+
/// in this Prefilter
425+
pub fn max_needle_len(&self) -> usize {
426+
self.max_needle_len
427+
}
428+
414429
/// Implementations might return true here if they believe themselves to
415430
/// be "fast." The concept of "fast" is deliberately left vague, but in
416431
/// practice this usually corresponds to whether it's believed that SIMD
@@ -429,7 +444,7 @@ impl Prefilter {
429444
/// *know* a prefilter will be fast without actually trying the prefilter.
430445
/// (Which of course we cannot afford to do.)
431446
#[inline]
432-
pub(crate) fn is_fast(&self) -> bool {
447+
pub fn is_fast(&self) -> bool {
433448
#[cfg(not(feature = "alloc"))]
434449
{
435450
unreachable!()

0 commit comments

Comments
 (0)