@@ -220,20 +220,26 @@ pub enum Faction {
220
220
NonCapture ,
221
221
}
222
222
223
- trait FindSet {
223
+ trait SetMatch {
224
224
fn find_set ( & self , & Set , & Membership ) -> Option < usize > ;
225
+ fn starts_with_set ( & self , & Set , & Membership ) -> bool ;
225
226
}
226
227
227
- impl FindSet for str {
228
+ impl SetMatch for str {
228
229
fn find_set ( & self , set : & Set , membership : & Membership ) -> Option < usize > {
229
- for ( index, c) in self . chars ( ) . enumerate ( ) {
230
- match * membership {
231
- Inclusive => if set. contains ( c) { return Some ( index) } ,
232
- Exclusive => if !set. contains ( c) { return Some ( index) } ,
233
- }
234
- }
235
-
236
- None
230
+ self . chars ( )
231
+ . position ( |c| match * membership {
232
+ Inclusive => set. contains ( c) ,
233
+ Exclusive => !set. contains ( c) ,
234
+ } )
235
+ }
236
+ fn starts_with_set ( & self , set : & Set , membership : & Membership ) -> bool {
237
+ self . chars ( )
238
+ . take ( 1 )
239
+ . any ( |c| match * membership {
240
+ Inclusive => set. contains ( c) ,
241
+ Exclusive => !set. contains ( c) ,
242
+ } )
237
243
}
238
244
}
239
245
@@ -267,20 +273,15 @@ impl Ast {
267
273
}
268
274
// If the str matches, return the remainer of the str after the 1st match
269
275
// has been trimmed off.
270
- pub fn matches < ' a > ( & self , txt : & ' a str ) -> Option < & ' a str > {
271
- let vec: Vec < & str > = match self {
272
- & Ast :: Char ( c) => {
273
- if txt. starts_with ( c) { txt. splitn ( 2 , c) . collect ( ) }
274
- else { return None }
275
- } ,
276
- & Ast :: Literal ( ref s) => {
277
- if txt. starts_with ( s) { txt. splitn ( 2 , s) . collect ( ) }
278
- else { return None }
276
+ pub fn trim_left_match < ' a > ( & self , txt : & ' a str ) -> Option < & ' a str > {
277
+ match self {
278
+ & Ast :: Char ( c) => if txt. starts_with ( c) { Some ( & txt[ 1 ..] ) } else { None } ,
279
+ & Ast :: Literal ( ref s) => if txt. starts_with ( s) { Some ( & txt[ s. len ( ) ..] ) } else { None } ,
280
+ & Ast :: Set ( ref set, ref membership) => {
281
+ if txt. starts_with_set ( set, membership) { Some ( & txt[ 1 ..] ) } else { None }
279
282
} ,
280
- ast => return if let Some ( index) = ast. find ( txt) { Some ( & txt[ index + 1 ..] ) } else { None } ,
281
- } ;
282
-
283
- Some ( vec[ 1 ] )
283
+ _ => unimplemented ! ( ) ,
284
+ }
284
285
}
285
286
fn negate ( self ) -> Self {
286
287
match self {
0 commit comments