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
8 changes: 8 additions & 0 deletions src/sed/fast_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,11 @@ impl OutputBuffer {
/// Schedule the specified output chunk for eventual output
pub fn write_chunk(&mut self, new_chunk: &IOChunk) -> io::Result<()> {
if new_chunk.is_empty() && !new_chunk.is_newline_terminated() {
// An empty last line without its newline writes no bytes, but it is
// still a line: it ends the previous output's line, and any later
// output starts on a new one.
self.flush_pending_newline()?;
self.pending_newline = true;
return Ok(());
}

Expand Down Expand Up @@ -873,6 +878,9 @@ impl OutputBuffer {
/// Schedule the specified output chunk for eventual output
pub fn write_chunk(&mut self, chunk: &IOChunk) -> io::Result<()> {
if chunk.is_empty() && !chunk.is_newline_terminated() {
// See the unix version: an empty unterminated line is still a line.
self.flush_pending_newline()?;
self.pending_newline = true;
return Ok(());
}

Expand Down
8 changes: 7 additions & 1 deletion src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ fn process_file(
commands.clone()
};

// Set by `c`, `d` and `D`, which end the cycle without printing.
let mut deleted = false;

// Loop over script commands.
while let Some(command_rc) = current.take() {
let mut command = command_rc.borrow_mut();
Expand Down Expand Up @@ -744,11 +747,13 @@ fn process_file(
let text = extract_variant!(command, Text);
output.write_bytes(text.as_ref())?;
}
deleted = true;
break;
}
'd' => {
// Delete the pattern space and start the next cycle.
pattern.clear();
deleted = true;
break;
}
'D' => {
Expand All @@ -761,6 +766,7 @@ fn process_file(
}
// Same as d
pattern.clear();
deleted = true;
break;
}
'e' => match &command.data {
Expand Down Expand Up @@ -982,7 +988,7 @@ fn process_file(
current.clone_from(&command.next);
}

if !context.quiet {
if !context.quiet && !deleted {
write_chunk(output, context, &pattern)?;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4403,3 +4403,31 @@ fn test_lone_lead_byte_delimiter() {
.succeeds()
.stdout_only("y\n");
}

#[test]
fn test_empty_last_line_without_newline_is_still_a_line() {
// The last line lacks its newline and becomes empty. Printing it writes
// nothing, but the output around it is still separated into lines.
for (script, input, expected) in [
("s/ab//p", "ab", "\n"),
("p;s/x//", "x", "x\n"),
("s/x//;p", "a\nx", "a\na\n\n"),
("s/x//", "x", ""),
("$!d;s/x//", "a\nx", ""),

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.

could you please add cases for q and c/D too? e.g. s/x//;q and $!N;D on an empty unterminated last line, since they go through the new path as well

// q prints the empty line too.
("s/x//;q", "x", "\n"),
("$s/x//;$q", "a\nx", "a\n\n"),
("s/x//p;q", "x", "\n\n"),
// c and D end the cycle without printing it.
("s/x//;c\\\nfoo", "x", "foo\n"),
("s/x//;$c\\\nfoo", "a\nx", "a\nfoo\n"),
("$!N;s/x$//;D", "a\nx", ""),
("p;$!N;s/x$//;D", "a\nx", "a\n"),
] {
new_ucmd!()
.arg(script)
.pipe_in(input)
.succeeds()
.stdout_is(expected);
}
}
Loading