Skip to content

Commit 2520eb6

Browse files
committed
Fix \?, \+ and \| to be treated as ERE in BRE mode
1 parent d7a713e commit 2520eb6

5 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎src/sed/compiler.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ fn parse_command_ending(
533533
}
534534

535535
/// Convert a primitive BRE pattern to a safe ERE-compatible pattern string.
536-
/// - Replaces `\(` and `\)` with `(` and `)`.
536+
/// - Replaces `\(`, `\)`, `\?`, `\+` and `\|` with `(`, `)`, `?`, `+` and `|`.
537537
/// - Puts single-digit back-references in non-capturing groups..
538538
/// - Escapes ERE-only metacharacters: `+ ? { } | ( )`.
539539
/// - Leaves all other characters as-is.
@@ -554,6 +554,18 @@ fn bre_to_ere(pattern: &str) -> String {
554554
chars.next();
555555
result.push(')'); // Group end
556556
}
557+
Some('?') => {
558+
chars.next();
559+
result.push('?'); // Quantifier 0 or 1
560+
}
561+
Some('+') => {
562+
chars.next();
563+
result.push('+'); // Quantifier 1 or more
564+
}
565+
Some('|') => {
566+
chars.next();
567+
result.push('|'); // Alternation operator
568+
}
557569
Some(v) if v.is_ascii_digit() => {
558570
// Back-reference. In sed BREs these are single-digit
559571
// (\1-\9) whereas fancy_regex supports multi-digit
@@ -2192,7 +2204,7 @@ mod tests {
21922204
// bre_to_ere
21932205
#[test]
21942206
fn test_bre_group_translation() {
2195-
assert_eq!(bre_to_ere(r"\(abc\)"), "(abc)");
2207+
assert_eq!(bre_to_ere(r"\(a\?b\+c\|\)"), "(a?b+c|)");
21962208
assert_eq!(bre_to_ere(r"a\(b\)c"), "a(b)c");
21972209
}
21982210

‎tests/by-util/test_sed.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ check_output!(
300300
LINES1
301301
]
302302
);
303+
check_output!(
304+
subst_quantifier_zero_or_one,
305+
["-e", r"s/_[0-9]\([0-9]\)\?/_x\1/g", LINES1]
306+
);
307+
check_output!(
308+
subst_quantifier_one_or_more,
309+
["-e", r"s/_[0-9]\+/_x/g", LINES1]
310+
);
311+
check_output!(subst_alternation_operator, ["-e", r"s/0\|1/x/g", LINES1]);
303312
check_output!(subst_multiline, ["-e", "s/_/u0\\\nu1\\\nu2/g", LINES1]);
304313
check_output!(subst_numbered_replacement, ["-e", r"s/./X/4", LINES1]);
305314
check_output!(subst_brace, ["-e", r"s/[123]/X/g", LINES1]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
lx_x
2+
lx_2
3+
lx_3
4+
lx_4
5+
lx_5
6+
lx_6
7+
lx_7
8+
lx_8
9+
lx_9
10+
lx_xx
11+
lx_xx
12+
lx_x2
13+
lx_x3
14+
lx_x4
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
l1_x
2+
l1_x
3+
l1_x
4+
l1_x
5+
l1_x
6+
l1_x
7+
l1_x
8+
l1_x
9+
l1_x
10+
l1_x
11+
l1_x
12+
l1_x
13+
l1_x
14+
l1_x
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
l1_x
2+
l1_x
3+
l1_x
4+
l1_x
5+
l1_x
6+
l1_x
7+
l1_x
8+
l1_x
9+
l1_x
10+
l1_x0
11+
l1_x1
12+
l1_x2
13+
l1_x3
14+
l1_x4

0 commit comments

Comments
 (0)