@@ -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
151153impl 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,20 @@ impl Prefilter {
411420 }
412421 }
413422
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+
414437 /// Implementations might return true here if they believe themselves to
415438 /// be "fast." The concept of "fast" is deliberately left vague, but in
416439 /// practice this usually corresponds to whether it's believed that SIMD
@@ -429,7 +452,7 @@ impl Prefilter {
429452 /// *know* a prefilter will be fast without actually trying the prefilter.
430453 /// (Which of course we cannot afford to do.)
431454 #[ inline]
432- pub ( crate ) fn is_fast ( & self ) -> bool {
455+ pub fn is_fast ( & self ) -> bool {
433456 #[ cfg( not( feature = "alloc" ) ) ]
434457 {
435458 unreachable ! ( )
0 commit comments