Merge pull request #3277 from jfinkels/split-line-bytes-no-trailing-newline

split: handle no final newline with --line-bytes
This commit is contained in:
Sylvestre Ledru 2022-03-20 09:09:58 +01:00 committed by GitHub
commit 07b0bc6bb9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View file

@ -886,7 +886,7 @@ impl<'a> Write for LineBytesChunkWriter<'a> {
// then move on to the next chunk if necessary.
None => {
let end = self.num_bytes_remaining_in_current_chunk;
let num_bytes_written = self.inner.write(&buf[..end])?;
let num_bytes_written = self.inner.write(&buf[..end.min(buf.len())])?;
self.num_bytes_remaining_in_current_chunk -= num_bytes_written;
total_bytes_written += num_bytes_written;
buf = &buf[num_bytes_written..];

View file

@ -618,3 +618,19 @@ fn test_line_bytes() {
assert_eq!(at.read("xac"), "cccc\ndd\n");
assert_eq!(at.read("xad"), "ee\n");
}
#[test]
fn test_line_bytes_no_final_newline() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["-C", "2"])
.pipe_in("1\n2222\n3\n4")
.succeeds()
.no_stdout()
.no_stderr();
assert_eq!(at.read("xaa"), "1\n");
assert_eq!(at.read("xab"), "22");
assert_eq!(at.read("xac"), "22");
assert_eq!(at.read("xad"), "\n");
assert_eq!(at.read("xae"), "3\n");
assert_eq!(at.read("xaf"), "4");
}