Skip to content

Commit 0ff2270

Browse files
committed
core: add core::pattern::loop_next helper function
Negative delta FTW. Issue: rust-lang#49802
1 parent 13fbed4 commit 0ff2270

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

library/core/src/pattern.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,7 @@ pub unsafe trait Searcher<H: Haystack> {
237237
/// `(start_match, end_match)`, where start_match is the index of where
238238
/// the match begins, and end_match is the index after the end of the match.
239239
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())
247241
}
248242

249243
/// Finds the next [`Reject`][SearchStep::Reject] result. See [`next()`][Searcher::next]
@@ -252,13 +246,7 @@ pub unsafe trait Searcher<H: Haystack> {
252246
/// Unlike [`next()`][Searcher::next], there is no guarantee that the returned ranges
253247
/// of this and [`next_match`][Searcher::next_match] will overlap.
254248
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())
262250
}
263251
}
264252

@@ -303,25 +291,13 @@ pub unsafe trait ReverseSearcher<H: Haystack>: Searcher<H> {
303291
/// Finds the next [`Match`][SearchStep::Match] result.
304292
/// See [`next_back()`][ReverseSearcher::next_back].
305293
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())
313295
}
314296

315297
/// Finds the next [`Reject`][SearchStep::Reject] result.
316298
/// See [`next_back()`][ReverseSearcher::next_back].
317299
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())
325301
}
326302
}
327303

@@ -347,3 +323,19 @@ pub unsafe trait ReverseSearcher<H: Haystack>: Searcher<H> {
347323
/// the pattern `"aa"` in the haystack `"aaa"` matches as either
348324
/// `"[aa]a"` or `"a[aa]"`, depending from which side it is searched.
349325
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

Comments
 (0)