diff --git a/src/sed/fast_io.rs b/src/sed/fast_io.rs index ddecc835..051a09e7 100644 --- a/src/sed/fast_io.rs +++ b/src/sed/fast_io.rs @@ -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(()); } @@ -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(()); } diff --git a/src/sed/processor.rs b/src/sed/processor.rs index b6831169..f9dccda5 100644 --- a/src/sed/processor.rs +++ b/src/sed/processor.rs @@ -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(); @@ -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' => { @@ -761,6 +766,7 @@ fn process_file( } // Same as d pattern.clear(); + deleted = true; break; } 'e' => match &command.data { @@ -982,7 +988,7 @@ fn process_file( current.clone_from(&command.next); } - if !context.quiet { + if !context.quiet && !deleted { write_chunk(output, context, &pattern)?; } diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index e7ad2ef0..ab08cd3a 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -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", ""), + // 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); + } +}