@@ -146,6 +146,8 @@ pub struct Prefilter {
146
146
pre : Arc < dyn PrefilterI > ,
147
147
#[ cfg( feature = "alloc" ) ]
148
148
is_fast : bool ,
149
+ #[ cfg( feature = "alloc" ) ]
150
+ max_needle_len : usize ,
149
151
}
150
152
151
153
impl Prefilter {
@@ -202,12 +204,19 @@ impl Prefilter {
202
204
kind : MatchKind ,
203
205
needles : & [ B ] ,
204
206
) -> 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
+ } )
206
212
}
207
213
208
214
/// This turns a prefilter selection into a `Prefilter`. That is, in turns
209
215
/// 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 > {
211
220
#[ cfg( not( feature = "alloc" ) ) ]
212
221
{
213
222
None
@@ -224,7 +233,7 @@ impl Prefilter {
224
233
Choice :: AhoCorasick ( p) => Arc :: new ( p) ,
225
234
} ;
226
235
let is_fast = pre. is_fast ( ) ;
227
- Some ( Prefilter { pre, is_fast } )
236
+ Some ( Prefilter { pre, is_fast, max_needle_len } )
228
237
}
229
238
}
230
239
@@ -411,6 +420,20 @@ impl Prefilter {
411
420
}
412
421
}
413
422
423
+ /// Return the length of the longest needle
424
+ /// in this Prefilter
425
+ #[ inline]
426
+ pub fn max_needle_len ( & self ) -> usize {
427
+ #[ cfg( not( feature = "alloc" ) ) ]
428
+ {
429
+ unreachable ! ( )
430
+ }
431
+ #[ cfg( feature = "alloc" ) ]
432
+ {
433
+ self . max_needle_len
434
+ }
435
+ }
436
+
414
437
/// Implementations might return true here if they believe themselves to
415
438
/// be "fast." The concept of "fast" is deliberately left vague, but in
416
439
/// practice this usually corresponds to whether it's believed that SIMD
@@ -429,7 +452,7 @@ impl Prefilter {
429
452
/// *know* a prefilter will be fast without actually trying the prefilter.
430
453
/// (Which of course we cannot afford to do.)
431
454
#[ inline]
432
- pub ( crate ) fn is_fast ( & self ) -> bool {
455
+ pub fn is_fast ( & self ) -> bool {
433
456
#[ cfg( not( feature = "alloc" ) ) ]
434
457
{
435
458
unreachable ! ( )
0 commit comments