mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #3278 from jfinkels/split-line-bytes-empty-file
split: avoid writing final empty chunk with -C
This commit is contained in:
commit
b55da29c53
2 changed files with 26 additions and 6 deletions
|
@ -858,6 +858,11 @@ impl<'a> Write for LineBytesChunkWriter<'a> {
|
||||||
// Loop until we have written all bytes in the input buffer
|
// Loop until we have written all bytes in the input buffer
|
||||||
// (or an IO error occurs).
|
// (or an IO error occurs).
|
||||||
loop {
|
loop {
|
||||||
|
// If the buffer is empty, then we are done writing.
|
||||||
|
if buf.is_empty() {
|
||||||
|
return Ok(total_bytes_written);
|
||||||
|
}
|
||||||
|
|
||||||
// If we have filled the current chunk with bytes, then
|
// If we have filled the current chunk with bytes, then
|
||||||
// start a new chunk and initialize its corresponding
|
// start a new chunk and initialize its corresponding
|
||||||
// writer.
|
// writer.
|
||||||
|
@ -875,12 +880,6 @@ impl<'a> Write for LineBytesChunkWriter<'a> {
|
||||||
|
|
||||||
// Find the first newline character in the buffer.
|
// Find the first newline character in the buffer.
|
||||||
match memchr::memchr(b'\n', buf) {
|
match memchr::memchr(b'\n', buf) {
|
||||||
// If there is no newline character and the buffer is
|
|
||||||
// empty, then we are done writing.
|
|
||||||
None if buf.is_empty() => {
|
|
||||||
return Ok(total_bytes_written);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there is no newline character and the buffer is
|
// If there is no newline character and the buffer is
|
||||||
// not empty, then write as many bytes as we can and
|
// not empty, then write as many bytes as we can and
|
||||||
// then move on to the next chunk if necessary.
|
// then move on to the next chunk if necessary.
|
||||||
|
|
|
@ -634,3 +634,24 @@ fn test_line_bytes_no_final_newline() {
|
||||||
assert_eq!(at.read("xae"), "3\n");
|
assert_eq!(at.read("xae"), "3\n");
|
||||||
assert_eq!(at.read("xaf"), "4");
|
assert_eq!(at.read("xaf"), "4");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_line_bytes_no_empty_file() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
ucmd.args(&["-C", "1"])
|
||||||
|
.pipe_in("1\n2222\n3\n4")
|
||||||
|
.succeeds()
|
||||||
|
.no_stdout()
|
||||||
|
.no_stderr();
|
||||||
|
assert_eq!(at.read("xaa"), "1");
|
||||||
|
assert_eq!(at.read("xab"), "\n");
|
||||||
|
assert_eq!(at.read("xac"), "2");
|
||||||
|
assert_eq!(at.read("xad"), "2");
|
||||||
|
assert_eq!(at.read("xae"), "2");
|
||||||
|
assert_eq!(at.read("xaf"), "2");
|
||||||
|
assert_eq!(at.read("xag"), "\n");
|
||||||
|
assert_eq!(at.read("xah"), "3");
|
||||||
|
assert_eq!(at.read("xai"), "\n");
|
||||||
|
assert_eq!(at.read("xaj"), "4");
|
||||||
|
assert!(!at.plus("xak").exists());
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue