@@ -5,15 +5,14 @@ use std::ffi::OsStr;
55#[ cfg( feature = "std" ) ]
66use std:: path:: Path ;
77
8- use core:: { cmp , iter, ops, ptr, slice, str} ;
9- use memchr:: { memchr, memrchr} ;
8+ use core:: { iter, ops, ptr, slice, str} ;
9+ use memchr:: { memchr, memmem , memrchr} ;
1010
1111use crate :: ascii;
1212use crate :: bstr:: BStr ;
1313use crate :: byteset;
1414#[ cfg( feature = "std" ) ]
1515use crate :: ext_vec:: ByteVec ;
16- use crate :: search:: { PrefilterState , TwoWay } ;
1716#[ cfg( feature = "unicode" ) ]
1817use crate :: unicode:: {
1918 whitespace_len_fwd, whitespace_len_rev, GraphemeIndices , Graphemes ,
@@ -2986,15 +2985,13 @@ pub trait ByteSlice: Sealed {
29862985/// version which permits building a `Finder` that is not connected to the
29872986/// lifetime of its needle.
29882987#[ derive( Clone , Debug ) ]
2989- pub struct Finder < ' a > {
2990- searcher : TwoWay < ' a > ,
2991- }
2988+ pub struct Finder < ' a > ( memmem:: Finder < ' a > ) ;
29922989
29932990impl < ' a > Finder < ' a > {
29942991 /// Create a new finder for the given needle.
29952992 #[ inline]
29962993 pub fn new < B : ?Sized + AsRef < [ u8 ] > > ( needle : & ' a B ) -> Finder < ' a > {
2997- Finder { searcher : TwoWay :: forward ( needle. as_ref ( ) ) }
2994+ Finder ( memmem :: Finder :: new ( needle. as_ref ( ) ) )
29982995 }
29992996
30002997 /// Convert this finder into its owned variant, such that it no longer
@@ -3007,7 +3004,7 @@ impl<'a> Finder<'a> {
30073004 #[ cfg( feature = "std" ) ]
30083005 #[ inline]
30093006 pub fn into_owned ( self ) -> Finder < ' static > {
3010- Finder { searcher : self . searcher . into_owned ( ) }
3007+ Finder ( self . 0 . into_owned ( ) )
30113008 }
30123009
30133010 /// Returns the needle that this finder searches for.
@@ -3018,7 +3015,7 @@ impl<'a> Finder<'a> {
30183015 /// needle returned must necessarily be the shorter of the two.
30193016 #[ inline]
30203017 pub fn needle ( & self ) -> & [ u8 ] {
3021- self . searcher . needle ( )
3018+ self . 0 . needle ( )
30223019 }
30233020
30243021 /// Returns the index of the first occurrence of this needle in the given
@@ -3050,7 +3047,7 @@ impl<'a> Finder<'a> {
30503047 /// ```
30513048 #[ inline]
30523049 pub fn find < B : AsRef < [ u8 ] > > ( & self , haystack : B ) -> Option < usize > {
3053- self . searcher . find ( haystack. as_ref ( ) )
3050+ self . 0 . find ( haystack. as_ref ( ) )
30543051 }
30553052}
30563053
@@ -3071,15 +3068,13 @@ impl<'a> Finder<'a> {
30713068/// version which permits building a `FinderReverse` that is not connected to
30723069/// the lifetime of its needle.
30733070#[ derive( Clone , Debug ) ]
3074- pub struct FinderReverse < ' a > {
3075- searcher : TwoWay < ' a > ,
3076- }
3071+ pub struct FinderReverse < ' a > ( memmem:: FinderRev < ' a > ) ;
30773072
30783073impl < ' a > FinderReverse < ' a > {
30793074 /// Create a new reverse finder for the given needle.
30803075 #[ inline]
30813076 pub fn new < B : ?Sized + AsRef < [ u8 ] > > ( needle : & ' a B ) -> FinderReverse < ' a > {
3082- FinderReverse { searcher : TwoWay :: reverse ( needle. as_ref ( ) ) }
3077+ FinderReverse ( memmem :: FinderRev :: new ( needle. as_ref ( ) ) )
30833078 }
30843079
30853080 /// Convert this finder into its owned variant, such that it no longer
@@ -3092,7 +3087,7 @@ impl<'a> FinderReverse<'a> {
30923087 #[ cfg( feature = "std" ) ]
30933088 #[ inline]
30943089 pub fn into_owned ( self ) -> FinderReverse < ' static > {
3095- FinderReverse { searcher : self . searcher . into_owned ( ) }
3090+ FinderReverse ( self . 0 . into_owned ( ) )
30963091 }
30973092
30983093 /// Returns the needle that this finder searches for.
@@ -3103,7 +3098,7 @@ impl<'a> FinderReverse<'a> {
31033098 /// the needle returned must necessarily be the shorter of the two.
31043099 #[ inline]
31053100 pub fn needle ( & self ) -> & [ u8 ] {
3106- self . searcher . needle ( )
3101+ self . 0 . needle ( )
31073102 }
31083103
31093104 /// Returns the index of the last occurrence of this needle in the given
@@ -3135,7 +3130,7 @@ impl<'a> FinderReverse<'a> {
31353130 /// ```
31363131 #[ inline]
31373132 pub fn rfind < B : AsRef < [ u8 ] > > ( & self , haystack : B ) -> Option < usize > {
3138- self . searcher . rfind ( haystack. as_ref ( ) )
3133+ self . 0 . rfind ( haystack. as_ref ( ) )
31393134 }
31403135}
31413136
@@ -3147,17 +3142,14 @@ impl<'a> FinderReverse<'a> {
31473142/// byte string being looked for.
31483143#[ derive( Debug ) ]
31493144pub struct Find < ' a > {
3145+ it : memmem:: FindIter < ' a , ' a > ,
31503146 haystack : & ' a [ u8 ] ,
3151- prestate : PrefilterState ,
3152- searcher : TwoWay < ' a > ,
3153- pos : usize ,
3147+ needle : & ' a [ u8 ] ,
31543148}
31553149
31563150impl < ' a > Find < ' a > {
31573151 fn new ( haystack : & ' a [ u8 ] , needle : & ' a [ u8 ] ) -> Find < ' a > {
3158- let searcher = TwoWay :: forward ( needle) ;
3159- let prestate = searcher. prefilter_state ( ) ;
3160- Find { haystack, prestate, searcher, pos : 0 }
3152+ Find { it : memmem:: find_iter ( haystack, needle) , haystack, needle }
31613153 }
31623154}
31633155
@@ -3166,20 +3158,7 @@ impl<'a> Iterator for Find<'a> {
31663158
31673159 #[ inline]
31683160 fn next ( & mut self ) -> Option < usize > {
3169- if self . pos > self . haystack . len ( ) {
3170- return None ;
3171- }
3172- let result = self
3173- . searcher
3174- . find_with ( & mut self . prestate , & self . haystack [ self . pos ..] ) ;
3175- match result {
3176- None => None ,
3177- Some ( i) => {
3178- let pos = self . pos + i;
3179- self . pos = pos + cmp:: max ( 1 , self . searcher . needle ( ) . len ( ) ) ;
3180- Some ( pos)
3181- }
3182- }
3161+ self . it . next ( )
31833162 }
31843163}
31853164
@@ -3191,28 +3170,26 @@ impl<'a> Iterator for Find<'a> {
31913170/// byte string being looked for.
31923171#[ derive( Debug ) ]
31933172pub struct FindReverse < ' a > {
3173+ it : memmem:: FindRevIter < ' a , ' a > ,
31943174 haystack : & ' a [ u8 ] ,
3195- prestate : PrefilterState ,
3196- searcher : TwoWay < ' a > ,
3197- /// When searching with an empty needle, this gets set to `None` after
3198- /// we've yielded the last element at `0`.
3199- pos : Option < usize > ,
3175+ needle : & ' a [ u8 ] ,
32003176}
32013177
32023178impl < ' a > FindReverse < ' a > {
32033179 fn new ( haystack : & ' a [ u8 ] , needle : & ' a [ u8 ] ) -> FindReverse < ' a > {
3204- let searcher = TwoWay :: reverse ( needle) ;
3205- let prestate = searcher. prefilter_state ( ) ;
3206- let pos = Some ( haystack. len ( ) ) ;
3207- FindReverse { haystack, prestate, searcher, pos }
3180+ FindReverse {
3181+ it : memmem:: rfind_iter ( haystack, needle) ,
3182+ haystack,
3183+ needle,
3184+ }
32083185 }
32093186
32103187 fn haystack ( & self ) -> & ' a [ u8 ] {
32113188 self . haystack
32123189 }
32133190
32143191 fn needle ( & self ) -> & [ u8 ] {
3215- self . searcher . needle ( )
3192+ self . needle
32163193 }
32173194}
32183195
@@ -3221,24 +3198,7 @@ impl<'a> Iterator for FindReverse<'a> {
32213198
32223199 #[ inline]
32233200 fn next ( & mut self ) -> Option < usize > {
3224- let pos = match self . pos {
3225- None => return None ,
3226- Some ( pos) => pos,
3227- } ;
3228- let result = self
3229- . searcher
3230- . rfind_with ( & mut self . prestate , & self . haystack [ ..pos] ) ;
3231- match result {
3232- None => None ,
3233- Some ( i) => {
3234- if pos == i {
3235- self . pos = pos. checked_sub ( 1 ) ;
3236- } else {
3237- self . pos = Some ( i) ;
3238- }
3239- Some ( i)
3240- }
3241- }
3201+ self . it . next ( )
32423202 }
32433203}
32443204
@@ -3398,7 +3358,7 @@ impl<'a> Iterator for Split<'a> {
33983358 match self . finder . next ( ) {
33993359 Some ( start) => {
34003360 let next = & haystack[ self . last ..start] ;
3401- self . last = start + self . finder . searcher . needle ( ) . len ( ) ;
3361+ self . last = start + self . finder . needle . len ( ) ;
34023362 Some ( next)
34033363 }
34043364 None => {
0 commit comments