Skip to content

Commit 76e52ca

Browse files
committed
Merge pull request #155 from defuz/remove-last-match
Remove last_match fields from FindCaptures and FindMatches
2 parents fb7d7e8 + 89c7207 commit 76e52ca

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

regex_macros/tests/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ fn empty_regex_nonempty_match() {
4545
assert_eq!(ms, vec![(0, 0), (1, 1), (2, 2), (3, 3)]);
4646
}
4747

48+
#[test]
49+
fn one_zero_length_match() {
50+
let re = regex!(r"\d*");
51+
let ms = re.find_iter("a1b2").collect::<Vec<_>>();
52+
assert_eq!(ms, vec![(0, 0), (1, 2), (3, 4)]);
53+
}
54+
55+
#[test]
56+
fn many_zero_length_match() {
57+
let re = regex!(r"\d*");
58+
let ms = re.find_iter("a1bbb2").collect::<Vec<_>>();
59+
assert_eq!(ms, vec![(0, 0), (1, 2), (3, 3), (4, 4), (5, 6)]);
60+
}
61+
62+
#[test]
63+
fn many_sequential_zero_length_match() {
64+
let re = regex!(r"\d?");
65+
let ms = re.find_iter("a12b3c").collect::<Vec<_>>();
66+
assert_eq!(ms, vec![(0, 0), (1, 2), (2, 3), (4, 5), (6, 6)]);
67+
}
68+
4869
#[test]
4970
fn quoted_bracket_set() {
5071
let re = regex!(r"([\x{5b}\x{5d}])");

src/re.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl Regex {
350350
re: self,
351351
search: text,
352352
last_end: 0,
353-
last_match: None,
353+
skip_next_empty: false
354354
}
355355
}
356356

@@ -454,8 +454,8 @@ impl Regex {
454454
FindCaptures {
455455
re: self,
456456
search: text,
457-
last_match: None,
458457
last_end: 0,
458+
skip_next_empty: false
459459
}
460460
}
461461

@@ -1059,8 +1059,8 @@ impl<'t> Iterator for SubCapturesNamed<'t> {
10591059
pub struct FindCaptures<'r, 't> {
10601060
re: &'r Regex,
10611061
search: &'t str,
1062-
last_match: Option<usize>,
10631062
last_end: usize,
1063+
skip_next_empty: bool
10641064
}
10651065

10661066
impl<'r, 't> Iterator for FindCaptures<'r, 't> {
@@ -1079,16 +1079,17 @@ impl<'r, 't> Iterator for FindCaptures<'r, 't> {
10791079

10801080
// Don't accept empty matches immediately following a match.
10811081
// i.e., no infinite loops please.
1082-
if e == s && Some(self.last_end) == self.last_match {
1083-
if self.last_end >= self.search.len() {
1084-
return None;
1085-
}
1082+
if e == s {
10861083
self.last_end += self.search[self.last_end..].chars()
1087-
.next().unwrap().len_utf8();
1088-
return self.next()
1084+
.next().map(|c| c.len_utf8()).unwrap_or(1);
1085+
if self.skip_next_empty {
1086+
self.skip_next_empty = false;
1087+
return self.next();
1088+
}
1089+
} else {
1090+
self.last_end = e;
1091+
self.skip_next_empty = true;
10891092
}
1090-
self.last_end = e;
1091-
self.last_match = Some(self.last_end);
10921093
Some(Captures::new(self.re, self.search, caps))
10931094
}
10941095
}
@@ -1104,8 +1105,8 @@ impl<'r, 't> Iterator for FindCaptures<'r, 't> {
11041105
pub struct FindMatches<'r, 't> {
11051106
re: &'r Regex,
11061107
search: &'t str,
1107-
last_match: Option<usize>,
11081108
last_end: usize,
1109+
skip_next_empty: bool
11091110
}
11101111

11111112
impl<'r, 't> Iterator for FindMatches<'r, 't> {
@@ -1124,16 +1125,17 @@ impl<'r, 't> Iterator for FindMatches<'r, 't> {
11241125

11251126
// Don't accept empty matches immediately following a match.
11261127
// i.e., no infinite loops please.
1127-
if e == s && Some(self.last_end) == self.last_match {
1128-
if self.last_end >= self.search.len() {
1129-
return None;
1130-
}
1128+
if e == s {
11311129
self.last_end += self.search[self.last_end..].chars()
1132-
.next().unwrap().len_utf8();
1133-
return self.next()
1130+
.next().map(|c| c.len_utf8()).unwrap_or(1);
1131+
if self.skip_next_empty {
1132+
self.skip_next_empty = false;
1133+
return self.next();
1134+
}
1135+
} else {
1136+
self.last_end = e;
1137+
self.skip_next_empty = true;
11341138
}
1135-
self.last_end = e;
1136-
self.last_match = Some(self.last_end);
11371139
Some((s, e))
11381140
}
11391141
}

0 commit comments

Comments
 (0)