File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -311,6 +311,12 @@ impl Hir {
311
311
312
312
/// Creates a repetition HIR expression.
313
313
pub fn repetition ( rep : Repetition ) -> Hir {
314
+ // The regex 'a{0}' is always equivalent to the empty regex. This is
315
+ // true even when 'a' is an expression that never matches anything
316
+ // (like '\P{any}').
317
+ if rep. min == 0 && rep. max == Some ( 0 ) {
318
+ return Hir :: empty ( ) ;
319
+ }
314
320
let mut info = HirInfo :: new ( ) ;
315
321
info. set_always_utf8 ( rep. hir . is_always_utf8 ( ) ) ;
316
322
info. set_all_assertions ( rep. hir . is_all_assertions ( ) ) ;
Original file line number Diff line number Diff line change @@ -372,6 +372,17 @@ mod tests {
372
372
roundtrip ( "(?U)a{2}" , "a{2}" ) ;
373
373
roundtrip ( "(?U)a{1,}" , "a+?" ) ;
374
374
roundtrip ( "(?U)a{1,5}" , "a{1,5}?" ) ;
375
+
376
+ // Test that various zero-length repetitions always translate to an
377
+ // empty regex. This is more a property of HIR's smart constructors
378
+ // than the printer though.
379
+ roundtrip ( "a{0}" , "" ) ;
380
+ roundtrip ( "(?:ab){0}" , "" ) ;
381
+ #[ cfg( feature = "unicode-gencat" ) ]
382
+ {
383
+ roundtrip ( r"\p{any}{0}" , "" ) ;
384
+ roundtrip ( r"\P{any}{0}" , "" ) ;
385
+ }
375
386
}
376
387
377
388
#[ test]
You can’t perform that action at this time.
0 commit comments