From 0ea2552a129d234851a90b08c5f56ccb1c115e98 Mon Sep 17 00:00:00 2001 From: Nadir Ali <36660597+Xnadir@users.noreply.github.com> Date: Sat, 28 Nov 2026 23:16:42 +0500 Subject: [PATCH] sed: compile first~step into a single address first~step depends on the line number alone, so compile it to the single StepStart address that #619 introduced for range starts, instead of addr1 Line plus addr2 StepMatch. It no longer starts late when line first never reaches the command (deleted, or joined by N), and c prints its text on every selected line. StepMatch stays for a regex first address. --- src/sed/command.rs | 4 ++-- src/sed/compiler.rs | 18 +++++++++++------- tests/by-util/test_sed.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/sed/command.rs b/src/sed/command.rs index b95b41be..732eae04 100644 --- a/src/sed/command.rs +++ b/src/sed/command.rs @@ -113,8 +113,8 @@ pub enum Address { Line(usize), // Specific line RelLine(usize), // Relative line Last, // Last line - StepMatch(usize), // Lines matching specified step from first - StepStart(usize, usize), // first~step starting a range: first, first+step, ... + StepMatch(usize), // Lines matching specified step from a regex first address + StepStart(usize, usize), // first~step: line first and every step-th line after it StepEnd(usize), // Range ending at specified step from first } diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index 342db035..13a93ae5 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -361,21 +361,24 @@ fn compile_address_range( let (mut is_step_match, mut is_step_end, mut addr2, mut step_n) = compile_second_address(lines, line, context)?; - // first~step,addr2: the lines first~step selects start the range. line.eat_spaces(); + let starts_range = !line.eol() && line.current() == ','; if is_step_match - && !line.eol() - && line.current() == ',' && let Some(Address::Line(first)) = cmd.addr1 && (first, step_n) != (0, 0) { - // A step of 0 selects line first alone, like a plain line number. + // first~step depends on the line number alone, so it is a single + // address: a step of 0 selects line first, like a plain line number. cmd.addr1 = Some(if step_n == 0 { Address::Line(first) } else { Address::StepStart(first, step_n) }); is_line0 = false; + if !starts_range { + return Ok(n_addr); + } + // first~step,addr2: the lines first~step selects start the range. (is_step_match, is_step_end, addr2, step_n) = compile_second_address(lines, line, context)?; } @@ -2329,9 +2332,10 @@ mod tests { let mut cmd = Rc::new(RefCell::new(Command::default())); let n_addr = compile_address_range(&lines, &mut chars, &mut cmd, &ctx()).unwrap(); - assert_eq!(n_addr, 2); - assert!(matches!(cmd.borrow().addr1, Some(Address::Line(0)))); - assert!(matches!(cmd.borrow().addr2, Some(Address::StepMatch(2)))); + // first~step is a single address. + assert_eq!(n_addr, 1); + assert!(matches!(cmd.borrow().addr1, Some(Address::StepStart(0, 2)))); + assert!(cmd.borrow().addr2.is_none()); } #[test] diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 7c408df2..bf4d51fe 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -4479,3 +4479,32 @@ fn test_lone_lead_byte_delimiter() { .succeeds() .stdout_only("y\n"); } + +#[test] +fn test_step_address_counts_from_its_first_line() { + // Line 1 never reaches `1~2` (deleted, or joined by N), yet the step + // still counts from line 1: lines 3, 5, ... are selected, not line 2. + for (script, expected) in [ + ("1d;1~2s/^/>/", "c\n>d\ne\n"), + ("/b/d;1~2s/^/>/", "c\n>d\ne\n"), + ("N;1~2s/^/>/", "b\nc\nd\ne\n"), + ("1~2s/^/>/", ">b\nc\n>d\ne\n"), + ("0~2s/^/>/", "b\n>c\nd\n>e\n"), + ("3~0s/^/>/", "b\nc\n>d\ne\n"), + ] { + new_ucmd!() + .arg(script) + .pipe_in("b\nc\nd\ne\n") + .succeeds() + .stdout_is(expected); + } +} + +#[test] +fn test_step_address_change_prints_text_for_each_line() { + new_ucmd!() + .arg("1~2c\\\nX") + .pipe_in("a\nb\nc\nd\ne\n") + .succeeds() + .stdout_is("X\nb\nX\nd\nX\n"); +}