Skip to content

Commit c8cb161

Browse files
committed
Simplify RegexSplitN
1 parent 76e52ca commit c8cb161

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

src/re.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ impl Regex {
511511
-> RegexSplitsN<'r, 't> {
512512
RegexSplitsN {
513513
splits: self.split(text),
514-
cur: 0,
515-
limit: limit,
514+
n: limit,
516515
}
517516
}
518517

@@ -793,24 +792,22 @@ impl<'r, 't> Iterator for RegexSplits<'r, 't> {
793792
/// of the string being split.
794793
pub struct RegexSplitsN<'r, 't> {
795794
splits: RegexSplits<'r, 't>,
796-
cur: usize,
797-
limit: usize,
795+
n: usize,
798796
}
799797

800798
impl<'r, 't> Iterator for RegexSplitsN<'r, 't> {
801799
type Item = &'t str;
802800

803801
fn next(&mut self) -> Option<&'t str> {
804-
let text = self.splits.finder.search;
805-
if self.cur >= self.limit {
806-
None
802+
if self.n == 0 {
803+
return None
804+
}
805+
self.n -= 1;
806+
if self.n == 0 {
807+
let text = self.splits.finder.search;
808+
Some(&text[self.splits.last..])
807809
} else {
808-
self.cur += 1;
809-
if self.cur >= self.limit {
810-
Some(&text[self.splits.last..])
811-
} else {
812-
self.splits.next()
813-
}
810+
self.splits.next()
814811
}
815812
}
816813
}

0 commit comments

Comments
 (0)