Skip to content

Commit d894c63

Browse files
committed
Fix a bug in literal extraction.
When doing literal extraction, a non-empty concatenation should always be cut when a `^` (for prefixes) or a `$` (for suffixes) is seen. If a counted repetition is used, e.g., `${2}`, then the cut detection fails. We add in a special case to handle it. Fixes rust-lang#321
1 parent 7dfa895 commit d894c63

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

regex-syntax/src/literals.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ fn repeat_range_literals<F: FnMut(&Expr, &mut Literals)>(
819819
let n = cmp::min(lits.limit_size, min as usize);
820820
let es = iter::repeat(e.clone()).take(n).collect();
821821
f(&Concat(es), lits);
822-
if n < min as usize {
822+
if n < min as usize || lits.contains_empty() {
823823
lits.cut();
824824
}
825825
}
@@ -1156,8 +1156,9 @@ mod tests {
11561156

11571157
// Test regexes with empty assertions.
11581158
test_lit!(pfx_empty1, prefixes, "^a", M("a"));
1159-
test_lit!(pfx_empty2, prefixes, "^abc", M("abc"));
1160-
test_lit!(pfx_empty3, prefixes, "(?:^abc)|(?:^z)", M("abc"), M("z"));
1159+
test_lit!(pfx_empty2, prefixes, "a${2}", C("a"));
1160+
test_lit!(pfx_empty3, prefixes, "^abc", M("abc"));
1161+
test_lit!(pfx_empty4, prefixes, "(?:^abc)|(?:^z)", M("abc"), M("z"));
11611162

11621163
// Make sure some curious regexes have no prefixes.
11631164
test_lit!(pfx_nothing1, prefixes, ".");
@@ -1306,6 +1307,7 @@ mod tests {
13061307

13071308
// Test regexes with empty assertions.
13081309
test_lit!(sfx_empty1, suffixes, "a$", M("a"));
1310+
test_lit!(sfx_empty2, suffixes, "${2}a", C("a"));
13091311

13101312
// Make sure some curious regexes have no suffixes.
13111313
test_lit!(sfx_nothing1, suffixes, ".");

tests/regression.rs

+4
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,7 @@ mat!(endl_or_wb, r"(?m:$)|(?-u:\b)", "\u{6084e}", Some((4, 4)));
8282
mat!(zero_or_end, r"(?i-u:\x00)|$", "\u{e682f}", Some((4, 4)));
8383
mat!(y_or_endl, r"(?i-u:y)|(?m:$)", "\u{b4331}", Some((4, 4)));
8484
mat!(wb_start_x, r"(?u:\b)^(?-u:X)", "X", Some((0, 1)));
85+
86+
// See: https://github.com/rust-lang/regex/issues/321
87+
ismatch!(strange_anchor_non_complete_prefix, r"a^{2}", "", false);
88+
ismatch!(strange_anchor_non_complete_suffix, r"${2}a", "", false);

0 commit comments

Comments
 (0)