@@ -611,7 +611,8 @@ impl Regex {
611
611
& ' r self ,
612
612
input : I ,
613
613
) -> FindMatches < ' r , ' h > {
614
- let cache = self . pool . get ( ) ;
614
+ let mut cache = self . pool . get ( ) ;
615
+ cache. keep_lookaround_state ( true ) ;
615
616
let it = iter:: Searcher :: new ( input. into ( ) ) ;
616
617
FindMatches { re : self , cache, it }
617
618
}
@@ -652,7 +653,8 @@ impl Regex {
652
653
& ' r self ,
653
654
input : I ,
654
655
) -> CapturesMatches < ' r , ' h > {
655
- let cache = self . pool . get ( ) ;
656
+ let mut cache = self . pool . get ( ) ;
657
+ cache. keep_lookaround_state ( true ) ;
656
658
let caps = self . create_captures ( ) ;
657
659
let it = iter:: Searcher :: new ( input. into ( ) ) ;
658
660
CapturesMatches { re : self , cache, caps, it }
@@ -2076,7 +2078,11 @@ impl<'r, 'h> Iterator for FindMatches<'r, 'h> {
2076
2078
#[ inline]
2077
2079
fn next ( & mut self ) -> Option < Match > {
2078
2080
let FindMatches { re, ref mut cache, ref mut it } = * self ;
2079
- it. advance ( |input| Ok ( re. search_with ( cache, input) ) )
2081
+ let result = it. advance ( |input| Ok ( re. search_with ( cache, input) ) ) ;
2082
+ if result. is_none ( ) {
2083
+ cache. keep_lookaround_state ( false ) ;
2084
+ }
2085
+ result
2080
2086
}
2081
2087
2082
2088
#[ inline]
@@ -2149,6 +2155,7 @@ impl<'r, 'h> Iterator for CapturesMatches<'r, 'h> {
2149
2155
if caps. is_match ( ) {
2150
2156
Some ( caps. clone ( ) )
2151
2157
} else {
2158
+ cache. keep_lookaround_state ( false ) ;
2152
2159
None
2153
2160
}
2154
2161
}
@@ -2385,6 +2392,19 @@ impl Cache {
2385
2392
re. imp . strat . reset_cache ( self )
2386
2393
}
2387
2394
2395
+ /// Set this cache to keep the state of look-behind assertions upon a
2396
+ /// match being found.
2397
+ ///
2398
+ /// This must only be called with a value of `true` when a new search is
2399
+ /// started at the end of a previously found match, otherwise the result
2400
+ /// of any search after this call will most likely be wrong.
2401
+ ///
2402
+ /// Calling this function with a value of `false` will clear any previously
2403
+ /// stored look-behind state.
2404
+ pub fn keep_lookaround_state ( & mut self , keep : bool ) {
2405
+ self . pikevm . keep_lookaround_state ( keep) ;
2406
+ }
2407
+
2388
2408
/// Returns the heap memory usage, in bytes, of this cache.
2389
2409
///
2390
2410
/// This does **not** include the stack size used up by this cache. To
0 commit comments