From 7b764f4a5c4f9354ca2040faa6f45647a0133c9f Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Wed, 4 Nov 2026 11:44:53 +0000 Subject: [PATCH 1/5] sed: treat a BRE * with nothing to repeat as a literal --- src/sed/delimited_parser.rs | 25 +++++++++++++++++++++++++ tests/by-util/test_sed.rs | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/sed/delimited_parser.rs b/src/sed/delimited_parser.rs index d4fff8ff..4d15d234 100644 --- a/src/sed/delimited_parser.rs +++ b/src/sed/delimited_parser.rs @@ -420,9 +420,13 @@ pub fn parse_regex_for_mode( // In an ERE, a quantifier must follow something it can repeat. GNU sed // rejects one at the start of the regex or right after `(`, `|`, `^` or // `$`, and reports it once the whole regex has been read. + // In a BRE, a `*` with nothing to repeat stands for itself, so it is + // emitted as `\*`. That is at the start of the regex, right after `\(` + // or `\|`, or right after a `^` anchor in one of those places. let is_ere = matches!(regex_mode, RegexMode::Extended); let mut can_quantify = false; let mut quantifier_without_atom = false; + let mut bre_at_start = true; while !line.eol() { let c = line.current(); if is_ere && c != delimiter { @@ -432,6 +436,27 @@ pub fn parse_regex_for_mode( } can_quantify = !matches!(c, '(' | '|' | '^' | '$'); } + if !is_ere && c != delimiter { + if c == '*' && !can_quantify { + result.extend_from_slice(b"\\*"); + can_quantify = true; + bre_at_start = false; + line.advance(); + continue; + } + let at_start = bre_at_start; + bre_at_start = false; + can_quantify = !(c == '^' && at_start); + let next = line + .get_line() + .get(line.get_pos() + 1) + .map(|&b| char::from(b)); + let opens = matches!(next, Some('(')) || (matches!(next, Some('|')) && !posix); + if c == '\\' && opens && next != Some(delimiter) { + can_quantify = false; + bre_at_start = true; + } + } match c { '[' if delimiter != '[' => { let cc = parse_character_class(lines, line, character_mode, posix)?; diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 99cc9897..c9f721fa 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -4393,3 +4393,27 @@ fn test_lone_lead_byte_delimiter() { .succeeds() .stdout_only("y\n"); } + +#[test] +fn test_bre_star_without_preceding_atom() { + for (script, expected) in [ + ("s/*/x/", "xa*b\n"), + ("s/^*/x/", "xa*b\n"), + ("s/^**/x/g", "xa*b\n"), + ("s/\\(*\\)/x/g", "xaxb\n"), + ("s/b\\|*/x/g", "xaxx\n"), + ("/^*a/s/b/x/", "*a*x\n"), + ("s|\\|*|x|g", "x*xax*xbx\n"), + ] { + // Read the script from a file, see test_ere_quantifier_after_atom. + let mut file = NamedTempFile::new().expect("create temporary sed script"); + writeln!(file, "{script}").expect("write temporary sed script"); + let path = file.path().to_str().expect("temporary path is UTF-8"); + new_ucmd!() + .args(&["-f", path]) + .pipe_in("*a*b\n") + .succeeds() + .stdout_is(expected); + } +} + From 85921aa28c16383807eaf52d6f58125d8611a2a6 Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Thu, 5 Nov 2026 09:40:10 +0000 Subject: [PATCH 2/5] sed: only look ahead after a backslash, test \| with --posix --- src/sed/delimited_parser.rs | 21 +++++++++++---------- tests/by-util/test_sed.rs | 9 +++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/sed/delimited_parser.rs b/src/sed/delimited_parser.rs index 4d15d234..1ed250e3 100644 --- a/src/sed/delimited_parser.rs +++ b/src/sed/delimited_parser.rs @@ -435,8 +435,7 @@ pub fn parse_regex_for_mode( quantifier_without_atom = true; } can_quantify = !matches!(c, '(' | '|' | '^' | '$'); - } - if !is_ere && c != delimiter { + } else if c != delimiter { if c == '*' && !can_quantify { result.extend_from_slice(b"\\*"); can_quantify = true; @@ -447,14 +446,16 @@ pub fn parse_regex_for_mode( let at_start = bre_at_start; bre_at_start = false; can_quantify = !(c == '^' && at_start); - let next = line - .get_line() - .get(line.get_pos() + 1) - .map(|&b| char::from(b)); - let opens = matches!(next, Some('(')) || (matches!(next, Some('|')) && !posix); - if c == '\\' && opens && next != Some(delimiter) { - can_quantify = false; - bre_at_start = true; + if c == '\\' { + let next = line + .get_line() + .get(line.get_pos() + 1) + .map(|&b| char::from(b)); + let opens = matches!(next, Some('(')) || (matches!(next, Some('|')) && !posix); + if opens && next != Some(delimiter) { + can_quantify = false; + bre_at_start = true; + } } } match c { diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index c9f721fa..92984f00 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -4415,5 +4415,14 @@ fn test_bre_star_without_preceding_atom() { .succeeds() .stdout_is(expected); } + // Under --posix `\|` is a literal `|`, so the `*` repeats it. + let mut file = NamedTempFile::new().expect("create temporary sed script"); + writeln!(file, "s/b\\|*/x/g").expect("write temporary sed script"); + let path = file.path().to_str().expect("temporary path is UTF-8"); + new_ucmd!() + .args(&["--posix", "-f", path]) + .pipe_in("*a*b\nab||*\n") + .succeeds() + .stdout_is("*a*x\nax*\n"); } From 42dfc0b2cd3b4fe1554e070641f3095aa2eb1477 Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:01:59 +0000 Subject: [PATCH 3/5] sed: simplify BRE atom tracking to satisfy clippy --- src/sed/delimited_parser.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/sed/delimited_parser.rs b/src/sed/delimited_parser.rs index 1ed250e3..c5ce054f 100644 --- a/src/sed/delimited_parser.rs +++ b/src/sed/delimited_parser.rs @@ -452,10 +452,8 @@ pub fn parse_regex_for_mode( .get(line.get_pos() + 1) .map(|&b| char::from(b)); let opens = matches!(next, Some('(')) || (matches!(next, Some('|')) && !posix); - if opens && next != Some(delimiter) { - can_quantify = false; - bre_at_start = true; - } + bre_at_start = opens && next != Some(delimiter); + can_quantify = !bre_at_start; } } match c { From 319e9173ab2492e4a62fe5382d303a38332a8712 Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:25:20 +0000 Subject: [PATCH 4/5] sed: format rebased leading-star test --- tests/by-util/test_sed.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 92984f00..ea1df969 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -4425,4 +4425,3 @@ fn test_bre_star_without_preceding_atom() { .succeeds() .stdout_is("*a*x\nax*\n"); } - From 2514894fcfbc57be60a6d243379f3fce006e5abc Mon Sep 17 00:00:00 2001 From: qodelicious <162776175+qodelicious@users.noreply.github.com> Date: Mon, 30 Nov 2026 04:44:12 +0000 Subject: [PATCH 5/5] sed: use script_file and rstest cases for BRE leading-star test --- tests/by-util/test_sed.rs | 49 +++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index ea1df969..10f66268 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -4394,34 +4394,27 @@ fn test_lone_lead_byte_delimiter() { .stdout_only("y\n"); } -#[test] -fn test_bre_star_without_preceding_atom() { - for (script, expected) in [ - ("s/*/x/", "xa*b\n"), - ("s/^*/x/", "xa*b\n"), - ("s/^**/x/g", "xa*b\n"), - ("s/\\(*\\)/x/g", "xaxb\n"), - ("s/b\\|*/x/g", "xaxx\n"), - ("/^*a/s/b/x/", "*a*x\n"), - ("s|\\|*|x|g", "x*xax*xbx\n"), - ] { - // Read the script from a file, see test_ere_quantifier_after_atom. - let mut file = NamedTempFile::new().expect("create temporary sed script"); - writeln!(file, "{script}").expect("write temporary sed script"); - let path = file.path().to_str().expect("temporary path is UTF-8"); - new_ucmd!() - .args(&["-f", path]) - .pipe_in("*a*b\n") - .succeeds() - .stdout_is(expected); - } - // Under --posix `\|` is a literal `|`, so the `*` repeats it. - let mut file = NamedTempFile::new().expect("create temporary sed script"); - writeln!(file, "s/b\\|*/x/g").expect("write temporary sed script"); - let path = file.path().to_str().expect("temporary path is UTF-8"); +#[rstest] +#[case::star_first(&[], "s/*/x/", "*a*b\n", "xa*b\n")] +#[case::star_after_caret(&[], "s/^*/x/", "*a*b\n", "xa*b\n")] +#[case::double_star_after_caret(&[], "s/^**/x/g", "*a*b\n", "xa*b\n")] +#[case::star_after_group_open(&[], "s/\\(*\\)/x/g", "*a*b\n", "xaxb\n")] +#[case::star_after_alternation(&[], "s/b\\|*/x/g", "*a*b\n", "xaxx\n")] +#[case::address_star_after_caret(&[], "/^*a/s/b/x/", "*a*b\n", "*a*x\n")] +#[case::star_after_alternation_pipe_delim(&[], "s|\\|*|x|g", "*a*b\n", "x*xax*xbx\n")] +// Under --posix `\|` is a literal `|`, so the `*` repeats it. +#[case::posix_pipe_before_star(&["--posix"], "s/b\\|*/x/g", "*a*b\nab||*\n", "*a*x\nax*\n")] +fn test_bre_star_without_preceding_atom( + #[case] extra_args: &[&str], + #[case] script: &str, + #[case] input: &str, + #[case] expected: &str, +) { + let file = script_file(script); new_ucmd!() - .args(&["--posix", "-f", path]) - .pipe_in("*a*b\nab||*\n") + .args(extra_args) + .args(&["-f", script_path(&file)]) + .pipe_in(input) .succeeds() - .stdout_is("*a*x\nax*\n"); + .stdout_is(expected); }