Skip to content

Commit 9d9b0e8

Browse files
committed
syntax: 'a{0}' should compile to Hir::empty
No matter what 'a' is, 'a{0}' is always equivalent to an empty regex.
1 parent d3caa6e commit 9d9b0e8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

regex-syntax/src/hir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ impl Hir {
311311

312312
/// Creates a repetition HIR expression.
313313
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+
}
314320
let mut info = HirInfo::new();
315321
info.set_always_utf8(rep.hir.is_always_utf8());
316322
info.set_all_assertions(rep.hir.is_all_assertions());

regex-syntax/src/hir/print.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ mod tests {
372372
roundtrip("(?U)a{2}", "a{2}");
373373
roundtrip("(?U)a{1,}", "a+?");
374374
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+
}
375386
}
376387

377388
#[test]

0 commit comments

Comments
 (0)