@@ -342,6 +342,9 @@ pub struct Builder {
342342 start_pattern : Vec < StateID > ,
343343 /// The starting states for each individual look-behind sub-expression.
344344 start_look_behind : Vec < StateID > ,
345+ /// The length (in bytes) of the longest string matched by any
346+ /// look-behind sub-expression. If `None`, the length is unbounded.
347+ maximum_look_behind_len : Option < usize > ,
345348 /// A map from pattern ID to capture group index to name. (If no name
346349 /// exists, then a None entry is present. Thus, all capturing groups are
347350 /// present in this mapping.)
@@ -374,7 +377,7 @@ pub struct Builder {
374377impl Builder {
375378 /// Create a new builder for hand-assembling NFAs.
376379 pub fn new ( ) -> Builder {
377- Builder :: default ( )
380+ Builder { maximum_look_behind_len : Some ( 0 ) , .. Builder :: default ( ) }
378381 }
379382
380383 /// Clear this builder.
@@ -453,6 +456,7 @@ impl Builder {
453456
454457 nfa. set_starts ( start_anchored, start_unanchored, & self . start_pattern ) ;
455458 nfa. set_look_behind_starts ( self . start_look_behind . as_slice ( ) ) ;
459+ nfa. set_maximum_look_behind_len ( self . maximum_look_behind_len ) ;
456460 nfa. set_captures ( & self . captures ) . map_err ( BuildError :: captures) ?;
457461 // The idea here is to convert our intermediate states to their final
458462 // form. The only real complexity here is the process of converting
@@ -711,9 +715,21 @@ impl Builder {
711715 }
712716
713717 /// Adds the `start_id` to the set of starting states that is used when
714- /// running look-behind expressions.
715- pub fn start_look_behind ( & mut self , start_id : StateID ) {
718+ /// running look-behind expressions. Additionally registers the maximum
719+ /// length (in bytes) that the sub-expression of the look-behind can match.
720+ pub fn start_look_behind (
721+ & mut self ,
722+ start_id : StateID ,
723+ maximum_len : Option < usize > ,
724+ ) {
716725 self . start_look_behind . push ( start_id) ;
726+
727+ self . maximum_look_behind_len =
728+ match ( self . maximum_look_behind_len , maximum_len) {
729+ ( Some ( l1) , Some ( l2) ) => Some ( usize:: max ( l1, l2) ) ,
730+ // A None subsumes the entire result.
731+ ( None , _) | ( _, None ) => None ,
732+ } ;
717733 }
718734
719735 /// Add an "empty" NFA state.
0 commit comments