@@ -350,7 +350,7 @@ impl Regex {
350
350
re : self ,
351
351
search : text,
352
352
last_end : 0 ,
353
- last_match : None ,
353
+ skip_next_empty : false
354
354
}
355
355
}
356
356
@@ -454,8 +454,8 @@ impl Regex {
454
454
FindCaptures {
455
455
re : self ,
456
456
search : text,
457
- last_match : None ,
458
457
last_end : 0 ,
458
+ skip_next_empty : false
459
459
}
460
460
}
461
461
@@ -1059,8 +1059,8 @@ impl<'t> Iterator for SubCapturesNamed<'t> {
1059
1059
pub struct FindCaptures < ' r , ' t > {
1060
1060
re : & ' r Regex ,
1061
1061
search : & ' t str ,
1062
- last_match : Option < usize > ,
1063
1062
last_end : usize ,
1063
+ skip_next_empty : bool
1064
1064
}
1065
1065
1066
1066
impl < ' r , ' t > Iterator for FindCaptures < ' r , ' t > {
@@ -1079,16 +1079,17 @@ impl<'r, 't> Iterator for FindCaptures<'r, 't> {
1079
1079
1080
1080
// Don't accept empty matches immediately following a match.
1081
1081
// 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 {
1086
1083
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 ;
1089
1092
}
1090
- self . last_end = e;
1091
- self . last_match = Some ( self . last_end ) ;
1092
1093
Some ( Captures :: new ( self . re , self . search , caps) )
1093
1094
}
1094
1095
}
@@ -1104,8 +1105,8 @@ impl<'r, 't> Iterator for FindCaptures<'r, 't> {
1104
1105
pub struct FindMatches < ' r , ' t > {
1105
1106
re : & ' r Regex ,
1106
1107
search : & ' t str ,
1107
- last_match : Option < usize > ,
1108
1108
last_end : usize ,
1109
+ skip_next_empty : bool
1109
1110
}
1110
1111
1111
1112
impl < ' r , ' t > Iterator for FindMatches < ' r , ' t > {
@@ -1124,16 +1125,17 @@ impl<'r, 't> Iterator for FindMatches<'r, 't> {
1124
1125
1125
1126
// Don't accept empty matches immediately following a match.
1126
1127
// 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 {
1131
1129
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 ;
1134
1138
}
1135
- self . last_end = e;
1136
- self . last_match = Some ( self . last_end ) ;
1137
1139
Some ( ( s, e) )
1138
1140
}
1139
1141
}
0 commit comments