@@ -5,15 +5,14 @@ use std::ffi::OsStr;
5
5
#[ cfg( feature = "std" ) ]
6
6
use std:: path:: Path ;
7
7
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} ;
10
10
11
11
use crate :: ascii;
12
12
use crate :: bstr:: BStr ;
13
13
use crate :: byteset;
14
14
#[ cfg( feature = "std" ) ]
15
15
use crate :: ext_vec:: ByteVec ;
16
- use crate :: search:: { PrefilterState , TwoWay } ;
17
16
#[ cfg( feature = "unicode" ) ]
18
17
use crate :: unicode:: {
19
18
whitespace_len_fwd, whitespace_len_rev, GraphemeIndices , Graphemes ,
@@ -2986,15 +2985,13 @@ pub trait ByteSlice: Sealed {
2986
2985
/// version which permits building a `Finder` that is not connected to the
2987
2986
/// lifetime of its needle.
2988
2987
#[ derive( Clone , Debug ) ]
2989
- pub struct Finder < ' a > {
2990
- searcher : TwoWay < ' a > ,
2991
- }
2988
+ pub struct Finder < ' a > ( memmem:: Finder < ' a > ) ;
2992
2989
2993
2990
impl < ' a > Finder < ' a > {
2994
2991
/// Create a new finder for the given needle.
2995
2992
#[ inline]
2996
2993
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 ( ) ) )
2998
2995
}
2999
2996
3000
2997
/// Convert this finder into its owned variant, such that it no longer
@@ -3007,7 +3004,7 @@ impl<'a> Finder<'a> {
3007
3004
#[ cfg( feature = "std" ) ]
3008
3005
#[ inline]
3009
3006
pub fn into_owned ( self ) -> Finder < ' static > {
3010
- Finder { searcher : self . searcher . into_owned ( ) }
3007
+ Finder ( self . 0 . into_owned ( ) )
3011
3008
}
3012
3009
3013
3010
/// Returns the needle that this finder searches for.
@@ -3018,7 +3015,7 @@ impl<'a> Finder<'a> {
3018
3015
/// needle returned must necessarily be the shorter of the two.
3019
3016
#[ inline]
3020
3017
pub fn needle ( & self ) -> & [ u8 ] {
3021
- self . searcher . needle ( )
3018
+ self . 0 . needle ( )
3022
3019
}
3023
3020
3024
3021
/// Returns the index of the first occurrence of this needle in the given
@@ -3050,7 +3047,7 @@ impl<'a> Finder<'a> {
3050
3047
/// ```
3051
3048
#[ inline]
3052
3049
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 ( ) )
3054
3051
}
3055
3052
}
3056
3053
@@ -3071,15 +3068,13 @@ impl<'a> Finder<'a> {
3071
3068
/// version which permits building a `FinderReverse` that is not connected to
3072
3069
/// the lifetime of its needle.
3073
3070
#[ derive( Clone , Debug ) ]
3074
- pub struct FinderReverse < ' a > {
3075
- searcher : TwoWay < ' a > ,
3076
- }
3071
+ pub struct FinderReverse < ' a > ( memmem:: FinderRev < ' a > ) ;
3077
3072
3078
3073
impl < ' a > FinderReverse < ' a > {
3079
3074
/// Create a new reverse finder for the given needle.
3080
3075
#[ inline]
3081
3076
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 ( ) ) )
3083
3078
}
3084
3079
3085
3080
/// Convert this finder into its owned variant, such that it no longer
@@ -3092,7 +3087,7 @@ impl<'a> FinderReverse<'a> {
3092
3087
#[ cfg( feature = "std" ) ]
3093
3088
#[ inline]
3094
3089
pub fn into_owned ( self ) -> FinderReverse < ' static > {
3095
- FinderReverse { searcher : self . searcher . into_owned ( ) }
3090
+ FinderReverse ( self . 0 . into_owned ( ) )
3096
3091
}
3097
3092
3098
3093
/// Returns the needle that this finder searches for.
@@ -3103,7 +3098,7 @@ impl<'a> FinderReverse<'a> {
3103
3098
/// the needle returned must necessarily be the shorter of the two.
3104
3099
#[ inline]
3105
3100
pub fn needle ( & self ) -> & [ u8 ] {
3106
- self . searcher . needle ( )
3101
+ self . 0 . needle ( )
3107
3102
}
3108
3103
3109
3104
/// Returns the index of the last occurrence of this needle in the given
@@ -3135,7 +3130,7 @@ impl<'a> FinderReverse<'a> {
3135
3130
/// ```
3136
3131
#[ inline]
3137
3132
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 ( ) )
3139
3134
}
3140
3135
}
3141
3136
@@ -3147,17 +3142,14 @@ impl<'a> FinderReverse<'a> {
3147
3142
/// byte string being looked for.
3148
3143
#[ derive( Debug ) ]
3149
3144
pub struct Find < ' a > {
3145
+ it : memmem:: FindIter < ' a , ' a > ,
3150
3146
haystack : & ' a [ u8 ] ,
3151
- prestate : PrefilterState ,
3152
- searcher : TwoWay < ' a > ,
3153
- pos : usize ,
3147
+ needle : & ' a [ u8 ] ,
3154
3148
}
3155
3149
3156
3150
impl < ' a > Find < ' a > {
3157
3151
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 }
3161
3153
}
3162
3154
}
3163
3155
@@ -3166,20 +3158,7 @@ impl<'a> Iterator for Find<'a> {
3166
3158
3167
3159
#[ inline]
3168
3160
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 ( )
3183
3162
}
3184
3163
}
3185
3164
@@ -3191,28 +3170,26 @@ impl<'a> Iterator for Find<'a> {
3191
3170
/// byte string being looked for.
3192
3171
#[ derive( Debug ) ]
3193
3172
pub struct FindReverse < ' a > {
3173
+ it : memmem:: FindRevIter < ' a , ' a > ,
3194
3174
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 ] ,
3200
3176
}
3201
3177
3202
3178
impl < ' a > FindReverse < ' a > {
3203
3179
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
+ }
3208
3185
}
3209
3186
3210
3187
fn haystack ( & self ) -> & ' a [ u8 ] {
3211
3188
self . haystack
3212
3189
}
3213
3190
3214
3191
fn needle ( & self ) -> & [ u8 ] {
3215
- self . searcher . needle ( )
3192
+ self . needle
3216
3193
}
3217
3194
}
3218
3195
@@ -3221,24 +3198,7 @@ impl<'a> Iterator for FindReverse<'a> {
3221
3198
3222
3199
#[ inline]
3223
3200
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 ( )
3242
3202
}
3243
3203
}
3244
3204
@@ -3398,7 +3358,7 @@ impl<'a> Iterator for Split<'a> {
3398
3358
match self . finder . next ( ) {
3399
3359
Some ( start) => {
3400
3360
let next = & haystack[ self . last ..start] ;
3401
- self . last = start + self . finder . searcher . needle ( ) . len ( ) ;
3361
+ self . last = start + self . finder . needle . len ( ) ;
3402
3362
Some ( next)
3403
3363
}
3404
3364
None => {
0 commit comments