Skip to content

Commit 89c7207

Browse files
committed
Add skip_next_empty field for FindCaptures and FindMatches
1 parent 4fd7a3f commit 89c7207

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ impl Regex {
350350
re: self,
351351
search: text,
352352
last_end: 0,
353+
skip_next_empty: false
353354
}
354355
}
355356

@@ -454,6 +455,7 @@ impl Regex {
454455
re: self,
455456
search: text,
456457
last_end: 0,
458+
skip_next_empty: false
457459
}
458460
}
459461

@@ -1058,6 +1060,7 @@ pub struct FindCaptures<'r, 't> {
10581060
re: &'r Regex,
10591061
search: &'t str,
10601062
last_end: usize,
1063+
skip_next_empty: bool
10611064
}
10621065

10631066
impl<'r, 't> Iterator for FindCaptures<'r, 't> {
@@ -1079,8 +1082,13 @@ impl<'r, 't> Iterator for FindCaptures<'r, 't> {
10791082
if e == s {
10801083
self.last_end += self.search[self.last_end..].chars()
10811084
.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+
}
10821089
} else {
10831090
self.last_end = e;
1091+
self.skip_next_empty = true;
10841092
}
10851093
Some(Captures::new(self.re, self.search, caps))
10861094
}
@@ -1098,6 +1106,7 @@ pub struct FindMatches<'r, 't> {
10981106
re: &'r Regex,
10991107
search: &'t str,
11001108
last_end: usize,
1109+
skip_next_empty: bool
11011110
}
11021111

11031112
impl<'r, 't> Iterator for FindMatches<'r, 't> {
@@ -1119,8 +1128,13 @@ impl<'r, 't> Iterator for FindMatches<'r, 't> {
11191128
if e == s {
11201129
self.last_end += self.search[self.last_end..].chars()
11211130
.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+
}
11221135
} else {
11231136
self.last_end = e;
1137+
self.skip_next_empty = true;
11241138
}
11251139
Some((s, e))
11261140
}

0 commit comments

Comments
 (0)