Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/sed/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
18 changes: 11 additions & 7 deletions src/sed/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down Expand Up @@ -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]
Expand Down
29 changes: 29 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Loading