@@ -237,13 +237,7 @@ pub unsafe trait Searcher<H: Haystack> {
237
237
/// `(start_match, end_match)`, where start_match is the index of where
238
238
/// the match begins, and end_match is the index after the end of the match.
239
239
fn next_match ( & mut self ) -> Option < ( H :: Cursor , H :: Cursor ) > {
240
- loop {
241
- match self . next ( ) {
242
- SearchStep :: Match ( a, b) => return Some ( ( a, b) ) ,
243
- SearchStep :: Done => return None ,
244
- _ => continue ,
245
- }
246
- }
240
+ loop_next :: < true , _ > ( || self . next ( ) )
247
241
}
248
242
249
243
/// Finds the next [`Reject`][SearchStep::Reject] result. See [`next()`][Searcher::next]
@@ -252,13 +246,7 @@ pub unsafe trait Searcher<H: Haystack> {
252
246
/// Unlike [`next()`][Searcher::next], there is no guarantee that the returned ranges
253
247
/// of this and [`next_match`][Searcher::next_match] will overlap.
254
248
fn next_reject ( & mut self ) -> Option < ( H :: Cursor , H :: Cursor ) > {
255
- loop {
256
- match self . next ( ) {
257
- SearchStep :: Reject ( a, b) => return Some ( ( a, b) ) ,
258
- SearchStep :: Done => return None ,
259
- _ => continue ,
260
- }
261
- }
249
+ loop_next :: < false , _ > ( || self . next ( ) )
262
250
}
263
251
}
264
252
@@ -303,25 +291,13 @@ pub unsafe trait ReverseSearcher<H: Haystack>: Searcher<H> {
303
291
/// Finds the next [`Match`][SearchStep::Match] result.
304
292
/// See [`next_back()`][ReverseSearcher::next_back].
305
293
fn next_match_back ( & mut self ) -> Option < ( H :: Cursor , H :: Cursor ) > {
306
- loop {
307
- match self . next_back ( ) {
308
- SearchStep :: Match ( a, b) => return Some ( ( a, b) ) ,
309
- SearchStep :: Done => return None ,
310
- _ => continue ,
311
- }
312
- }
294
+ loop_next :: < true , _ > ( || self . next_back ( ) )
313
295
}
314
296
315
297
/// Finds the next [`Reject`][SearchStep::Reject] result.
316
298
/// See [`next_back()`][ReverseSearcher::next_back].
317
299
fn next_reject_back ( & mut self ) -> Option < ( H :: Cursor , H :: Cursor ) > {
318
- loop {
319
- match self . next_back ( ) {
320
- SearchStep :: Reject ( a, b) => return Some ( ( a, b) ) ,
321
- SearchStep :: Done => return None ,
322
- _ => continue ,
323
- }
324
- }
300
+ loop_next :: < false , _ > ( || self . next_back ( ) )
325
301
}
326
302
}
327
303
@@ -347,3 +323,19 @@ pub unsafe trait ReverseSearcher<H: Haystack>: Searcher<H> {
347
323
/// the pattern `"aa"` in the haystack `"aaa"` matches as either
348
324
/// `"[aa]a"` or `"a[aa]"`, depending from which side it is searched.
349
325
pub trait DoubleEndedSearcher < H : Haystack > : ReverseSearcher < H > { }
326
+
327
+
328
+ /// Calls callback until it returns `SearchStep::Done` or either `Match` or
329
+ /// `Reject` depending no `MATCH` generic argument.
330
+ pub ( super ) fn loop_next < const MATCH : bool , T > (
331
+ mut next : impl FnMut ( ) -> SearchStep < T > ,
332
+ ) -> Option < ( T , T ) > {
333
+ loop {
334
+ match next ( ) {
335
+ SearchStep :: Done => break None ,
336
+ SearchStep :: Match ( start, end) if MATCH => break Some ( ( start, end) ) ,
337
+ SearchStep :: Reject ( start, end) if !MATCH => break Some ( ( start, end) ) ,
338
+ _ => ( ) ,
339
+ }
340
+ }
341
+ }
0 commit comments