diff --git a/src/sed/command.rs b/src/sed/command.rs index e652e97e..b95b41be 100644 --- a/src/sed/command.rs +++ b/src/sed/command.rs @@ -109,12 +109,13 @@ pub struct ByteSpace { #[derive(Debug)] /// Types of address specifications that precede commands pub enum Address { - Re(Option>), // Line that matches (optional) regex - Line(usize), // Specific line - RelLine(usize), // Relative line - Last, // Last line - StepMatch(usize), // Lines matching specified step from first - StepEnd(usize), // Range ending at specified step from first + Re(Option>), // Line that matches (optional) regex + 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, ... + StepEnd(usize), // Range ending at specified step from first } #[derive(Debug)] diff --git a/src/sed/compiler.rs b/src/sed/compiler.rs index c4534606..342db035 100644 --- a/src/sed/compiler.rs +++ b/src/sed/compiler.rs @@ -358,50 +358,27 @@ fn compile_address_range( line.eat_spaces(); if n_addr == 1 && !line.eol() && matches!(line.current(), ',' | '~') { - let is_step_match = line.current() == '~'; // E.g. 0~2: Pick even-numbered lines - line.advance(); - line.eat_spaces(); - let is_step_end = if !line.eol() && line.current() == '~' { - // E.g. /foo/,~10: Start at foo, include all lines until multiple of 10 is reached. - line.advance(); - line.eat_spaces(); - true - } else { - false - }; + let (mut is_step_match, mut is_step_end, mut addr2, mut step_n) = + compile_second_address(lines, line, context)?; - if (is_step_match || is_step_end) && context.posix { - // ~ steps are a GNU extension. - return compilation_error(lines, line, "~step is invalid in POSIX mode"); - } - - // Look for second address; a missing ~step counts as 0. - // addr1,+N is a GNU extension. - let has_addr2 = !line.eol() - && (is_address_char(line.current()) || (line.current() == '+' && !context.posix)); - if !(has_addr2 || is_step_match || is_step_end) { - return compilation_error(lines, line, "unexpected `,'"); + // first~step,addr2: the lines first~step selects start the range. + line.eat_spaces(); + 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. + cmd.addr1 = Some(if step_n == 0 { + Address::Line(first) + } else { + Address::StepStart(first, step_n) + }); + is_line0 = false; + (is_step_match, is_step_end, addr2, step_n) = + compile_second_address(lines, line, context)?; } - let addr2 = if has_addr2 { - compile_address(lines, line, context)? - } else { - Address::Line(0) - }; - // Set step_n to the number specified in the (required numeric) address. - let step_n = if is_step_match || is_step_end { - match addr2 { - Address::Line(n) => n, - _ => { - return compilation_error( - lines, - line, - "~step can only be specified through numeric values", - ); - } - } - } else { - 0 // dummy, not used - }; // Address 0 needs a regex end or a positive ~step (0~0 selects no line). if is_line0 && !matches!(addr2, Address::Re(_)) && !(is_step_match && step_n > 0) { @@ -432,6 +409,61 @@ fn compile_address_range( Ok(n_addr) } +/// Read the `,addr2`, `,~N` or `~N` that follows the first address. +/// Return whether it was `~N` (step match) or `,~N` (step end), the second +/// address, and N for either step form. +fn compile_second_address( + lines: &ScriptLineProvider, + line: &mut ScriptCharProvider, + context: &ProcessingContext, +) -> UResult<(bool, bool, Address, usize)> { + let is_step_match = line.current() == '~'; // E.g. 0~2: Pick even-numbered lines + line.advance(); + line.eat_spaces(); + let is_step_end = if !line.eol() && line.current() == '~' { + // E.g. /foo/,~10: Start at foo, include all lines until multiple of 10 is reached. + line.advance(); + line.eat_spaces(); + true + } else { + false + }; + + if (is_step_match || is_step_end) && context.posix { + // ~ steps are a GNU extension. + return compilation_error(lines, line, "~step is invalid in POSIX mode"); + } + + // Look for second address; a missing ~step counts as 0. + // addr1,+N is a GNU extension. + let has_addr2 = !line.eol() + && (is_address_char(line.current()) || (line.current() == '+' && !context.posix)); + if !(has_addr2 || is_step_match || is_step_end) { + return compilation_error(lines, line, "unexpected `,'"); + } + let addr2 = if has_addr2 { + compile_address(lines, line, context)? + } else { + Address::Line(0) + }; + // Set step_n to the number specified in the (required numeric) address. + let step_n = if is_step_match || is_step_end { + match addr2 { + Address::Line(n) => n, + _ => { + return compilation_error( + lines, + line, + "~step can only be specified through numeric values", + ); + } + } + } else { + 0 // dummy, not used + }; + Ok((is_step_match, is_step_end, addr2, step_n)) +} + /// Read the line's remaining characters as a file path and return it. // TODO Move to delimited_parser in separate commit. fn read_file_path(lines: &ScriptLineProvider, line: &mut ScriptCharProvider) -> UResult { diff --git a/src/sed/processor.rs b/src/sed/processor.rs index b6831169..4dde35f3 100644 --- a/src/sed/processor.rs +++ b/src/sed/processor.rs @@ -59,6 +59,11 @@ fn match_address( Address::Line(lineno) => Ok(context.line_number == *lineno), + Address::StepStart(first, step) => { + let n = context.line_number; + Ok(n >= *first && (n - first).is_multiple_of(*step)) + } + // Recognize "$" as the last line of last file. This is consistent // with the original 7th Research Edition implementation: // https://github.com/dspinellis/unix-history-repo/blob/Research-V7/usr/src/cmd/sed/sed1.c#L665 diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index a4e7450f..7c408df2 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -3789,6 +3789,43 @@ fn test_step_without_number_is_zero(#[values("3~=", "3,~=", "3,~0=")] script: &s .stdout_is("3\n"); } +#[test] +fn test_step_match_starts_range() { + // `first~step,addr2` starts the range on every line first~step selects. + let input = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n"; + for (script, expected) in [ + ("1~3,+1p", "1\n2\n4\n5\n7\n8\n10\n11\n"), + ("1~3,5p", "1\n2\n3\n4\n5\n7\n10\n"), + ("0~4,~3p", "4\n5\n6\n8\n9\n12\n"), + ("2~3,/7/p", "2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n"), + ("1~0,3p", "1\n2\n3\n"), + ("3~2,1p", "3\n5\n7\n9\n11\n"), + ("1d;1~0,3p", "2\n3\n"), + ] { + new_ucmd!() + .args(&["-n", script]) + .pipe_in(input) + .succeeds() + .stdout_is(expected); + } + // Unlike `0,/x/`, `0~3,/x/` starts on line 3, so a match on line 1 + // does not end it. + new_ucmd!() + .args(&["-n", "0~3,/x/p"]) + .pipe_in("x\n2\n3\n4\nx\n6\n7\n") + .succeeds() + .stdout_is("3\n4\nx\n6\n7\n"); +} + +#[test] +fn test_step_match_zero_zero_range_start() { + new_ucmd!() + .args(&["-n", "0~0,3p"]) + .fails() + .code_is(1) + .stderr_is("sed: