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
16 changes: 13 additions & 3 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ fn applies(
context: &mut ProcessingContext,
) -> UResult<bool> {
let linenum = context.line_number;
// Set below when this line ends a range; read by `c`.
context.last_address = false;

let result = if command.addr1.is_none() && command.addr2.is_none() {
// No address
Expand All @@ -99,6 +101,10 @@ fn applies(
command.start_line = None;
Ok(false)
} else {
if linenum - start == *n {
command.start_line = None;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also changes the restart for +N, e.g. printf 'a\na\na\na\n' | sed '/a/,+1d'
please add a test for it too

context.last_address = true;
}
Ok(true)
}
}
Expand All @@ -108,6 +114,10 @@ fn applies(
command.start_line = None;
Ok(false)
} else {
if linenum == *n {
command.start_line = None;
context.last_address = true;
}
Ok(true)
}
}
Expand Down Expand Up @@ -742,10 +752,10 @@ fn process_file(
break;
}
'c' => {
// At range end replace pattern space with text and
// start the next cycle.
// Print the text at the end of a range, or for every line
// when the range is negated, then start the next cycle.
pattern.clear();
if command.addr2.is_none() || context.last_address || reader.last_line()? {
if command.addr2.is_none() || command.non_select || context.last_address {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropping reader.last_line() changes the output for a range still open at EOF, please run the GNU testsuite to make sure nothing regresses:
git clone https://github.com/mirror/sed.git ../gnu.sed
./util/run-gnu-testsuite.sh

let text = extract_variant!(command, Text);
output.write_bytes(text.as_ref())?;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2221,6 +2221,44 @@ fn test_change_command_prevents_automatic_printing() {
.stdout_is("line1\nreplaced\nline3");
}

#[test]
fn test_change_command_prints_text_at_range_end() {
for (script, expected) in [
("2,4c\\X", "1\nX\n5\n6\n"),
("2,+2c\\X", "1\nX\n5\n6\n"),
("2,~4c\\X", "1\nX\n5\n6\n"),
("4,~4c\\X", "1\n2\n3\n"),
("2,4!c\\X", "X\n2\n3\n4\nX\nX\n"),
("5,8c\\X", "1\n2\n3\n4\n"),
("/2/,/3/c\\X\n6,8c\\Y", "1\nX\n4\n5\n"),
] {
new_ucmd!()
.arg(script)
.pipe_in("1\n2\n3\n4\n5\n6\n")
.succeeds()
.stdout_is(expected);
}
}

#[test]
fn test_range_with_line_end_restarts_on_next_line() {
new_ucmd!()
.arg("/a/,3d")
.pipe_in("a\na\na\na\nb\n")
.succeeds()
.stdout_is("b\n");
new_ucmd!()
.arg("/a/,+1d")
.pipe_in("a\na\na\na\nb\n")
.succeeds()
.stdout_is("b\n");
new_ucmd!()
.arg("/a/,+1c\\X")
.pipe_in("a\na\na\na\nb\n")
.succeeds()
.stdout_is("X\nX\nb\n");
}

#[test]
fn test_uppercase_delete_prevents_automatic_printing() {
// Test 'D' command - delete up to newline and restart
Expand Down
Loading