Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/sed/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ fn parse_command_ending(
}

/// Convert a primitive BRE pattern to a safe ERE-compatible pattern string.
/// - Replaces `\(` and `\)` with `(` and `)`.
/// - Replaces `\(`, `\)`, `\?`, `\+` and `\|` with `(`, `)`, `?`, `+` and `|`.
/// - Puts single-digit back-references in non-capturing groups..
/// - Escapes ERE-only metacharacters: `+ ? { } | ( )`.
/// - Leaves all other characters as-is.
Expand All @@ -554,6 +554,18 @@ fn bre_to_ere(pattern: &str) -> String {
chars.next();
result.push(')'); // Group end
}
Some('?') => {
chars.next();
result.push('?'); // Quantifier 0 or 1
}
Some('+') => {
chars.next();
result.push('+'); // Quantifier 1 or more
}
Some('|') => {
chars.next();
result.push('|'); // Alternation operator
}
Some(v) if v.is_ascii_digit() => {
// Back-reference. In sed BREs these are single-digit
// (\1-\9) whereas fancy_regex supports multi-digit
Expand Down Expand Up @@ -2192,7 +2204,7 @@ mod tests {
// bre_to_ere
#[test]
fn test_bre_group_translation() {
assert_eq!(bre_to_ere(r"\(abc\)"), "(abc)");
assert_eq!(bre_to_ere(r"\(a\?b\+c\|\)"), "(a?b+c|)");
assert_eq!(bre_to_ere(r"a\(b\)c"), "a(b)c");
}

Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ check_output!(
LINES1
]
);
check_output!(
subst_quantifier_zero_or_one,
["-e", r"s/_[0-9]\([0-9]\)\?/_x\1/g", LINES1]
);
check_output!(
subst_quantifier_one_or_more,
["-e", r"s/_[0-9]\+/_x/g", LINES1]
);
check_output!(subst_alternation_operator, ["-e", r"s/0\|1/x/g", LINES1]);
check_output!(subst_multiline, ["-e", "s/_/u0\\\nu1\\\nu2/g", LINES1]);
check_output!(subst_numbered_replacement, ["-e", r"s/./X/4", LINES1]);
check_output!(subst_brace, ["-e", r"s/[123]/X/g", LINES1]);
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/sed/output/subst_alternation_operator
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
lx_x
lx_2
lx_3
lx_4
lx_5
lx_6
lx_7
lx_8
lx_9
lx_xx
lx_xx
lx_x2
lx_x3
lx_x4
14 changes: 14 additions & 0 deletions tests/fixtures/sed/output/subst_quantifier_one_or_more
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
14 changes: 14 additions & 0 deletions tests/fixtures/sed/output/subst_quantifier_zero_or_one
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x
l1_x0
l1_x1
l1_x2
l1_x3
l1_x4
Loading