Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #47 from robinst/fix-empty-alt-with-newer-regex
Browse files Browse the repository at this point in the history
Fix alternations with empty sub-expressions with regex >= 0.2.7
  • Loading branch information
raphlinus authored May 1, 2018
2 parents f45d058 + c582def commit 2956e67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl Expr {

pub fn to_str(&self, buf: &mut String, precedence: u8) {
match *self {
Expr::Empty => buf.push_str(".{0}"),
Expr::Empty => (),
Expr::Any { newline } => buf.push_str(
if newline { "(?s:.)" } else { "." }
),
Expand Down Expand Up @@ -476,11 +476,26 @@ impl Expr {
if precedence > 0 {
buf.push_str("(?:");
}
children[0].to_str(buf, 1);
for child in &children[1..] {
buf.push('|');

let is_empty = |e| match e {
&Expr::Empty => true,
_ => false,
};
let contains_empty = children.iter().any(&is_empty);
if contains_empty {
buf.push_str("(?:");
}
for (i, child) in children.iter().filter(|&c| !is_empty(c)).enumerate() {
if i != 0 {
buf.push('|');
}
child.to_str(buf, 1);
}
if contains_empty {
// regex fails with `(a|b|)`, so transform to `((?:a|b)?)`
buf.push_str(")?");
}

if precedence > 0 {
buf.push(')');
}
Expand Down
6 changes: 6 additions & 0 deletions tests/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ fn character_class_intersection() {
fn alternation_with_empty_arm() {
assert_match(r"^(a|)$", "a");
assert_match(r"^(a|)$", "");
assert_match(r"^(|a)$", "a");
assert_match(r"^(|a)$", "");
assert_match(r"a|", "a");
assert_match(r"a|", "");
assert_match(r"|a", "a");
assert_match(r"|a", "");
assert_no_match(r"^(a|)$", "b");
}

Expand Down

0 comments on commit 2956e67

Please sign in to comment.