@@ -83,14 +83,18 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize
83
83
None
84
84
}
85
85
}
86
- ReadUntil :: Any ( ref any) => {
87
- for read_until in any {
88
- if let Some ( pos_tuple) = find ( read_until, buffer, eof) {
89
- return Some ( pos_tuple) ;
86
+ ReadUntil :: Any ( ref anys) => anys
87
+ . iter ( )
88
+ // Filter matching needles
89
+ . filter_map ( |any| find ( any, buffer, eof) )
90
+ // Return the left-most match
91
+ . min_by ( |( start1, end1) , ( start2, end2) | {
92
+ if start1 == start2 {
93
+ end1. cmp ( end2)
94
+ } else {
95
+ start1. cmp ( start2)
90
96
}
91
- }
92
- None
93
- }
97
+ } ) ,
94
98
}
95
99
}
96
100
@@ -304,8 +308,11 @@ mod tests {
304
308
let f = io:: Cursor :: new ( "2014-03-15" ) ;
305
309
let mut r = NBReader :: new ( f, None ) ;
306
310
let re = Regex :: new ( r"^\d{4}-\d{2}-\d{2}$" ) . unwrap ( ) ;
307
- r. read_until ( & ReadUntil :: Regex ( re) )
308
- . expect ( "regex doesn't match" ) ;
311
+ assert_eq ! (
312
+ ( "" . to_string( ) , "2014-03-15" . to_string( ) ) ,
313
+ r. read_until( & ReadUntil :: Regex ( re) )
314
+ . expect( "regex doesn't match" )
315
+ ) ;
309
316
}
310
317
311
318
#[ test]
@@ -338,6 +345,36 @@ mod tests {
338
345
) ;
339
346
}
340
347
348
+ #[ test]
349
+ fn test_any_with_multiple_possible_matches ( ) {
350
+ let f = io:: Cursor :: new ( "zero one two three four five" ) ;
351
+ let mut r = NBReader :: new ( f, None ) ;
352
+
353
+ let result = r
354
+ . read_until ( & ReadUntil :: Any ( vec ! [
355
+ ReadUntil :: String ( "two" . to_string( ) ) ,
356
+ ReadUntil :: String ( "one" . to_string( ) ) ,
357
+ ] ) )
358
+ . expect ( "finding string" ) ;
359
+
360
+ assert_eq ! ( ( "zero " . to_string( ) , "one" . to_string( ) ) , result) ;
361
+ }
362
+
363
+ #[ test]
364
+ fn test_any_with_same_start_different_length ( ) {
365
+ let f = io:: Cursor :: new ( "hi hello" ) ;
366
+ let mut r = NBReader :: new ( f, None ) ;
367
+
368
+ let result = r
369
+ . read_until ( & ReadUntil :: Any ( vec ! [
370
+ ReadUntil :: String ( "hello" . to_string( ) ) ,
371
+ ReadUntil :: String ( "hell" . to_string( ) ) ,
372
+ ] ) )
373
+ . expect ( "finding string" ) ;
374
+
375
+ assert_eq ! ( ( "hi " . to_string( ) , "hell" . to_string( ) ) , result) ;
376
+ }
377
+
341
378
#[ test]
342
379
fn test_eof ( ) {
343
380
let f = io:: Cursor :: new ( "lorem ipsum dolor sit amet" ) ;
0 commit comments