mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 06:42:42 +00:00
Fix line wrapping encoding performance
This commit is contained in:
parent
ed04f14ba8
commit
8a6923dcd4
1 changed files with 11 additions and 15 deletions
|
@ -362,29 +362,25 @@ mod fast_encode {
|
|||
|
||||
assert!(line_length_usize > 0_usize);
|
||||
|
||||
let number_of_lines = encoded_buffer.len() / line_length_usize;
|
||||
let make_contiguous_result = encoded_buffer.make_contiguous();
|
||||
|
||||
// How many bytes to take from the front of `encoded_buffer` and then write to stdout
|
||||
let number_of_bytes_to_drain = number_of_lines * line_length_usize;
|
||||
// (Number of whole lines times the line length)
|
||||
let number_of_bytes_to_drain =
|
||||
(make_contiguous_result.len() / line_length_usize) * line_length_usize;
|
||||
|
||||
let line_wrap_size_minus_one = line_length_usize - 1_usize;
|
||||
let chunks_exact = make_contiguous_result.chunks_exact(line_length_usize);
|
||||
|
||||
let mut i = 0_usize;
|
||||
|
||||
for ue in encoded_buffer.drain(..number_of_bytes_to_drain) {
|
||||
print_buffer.push(ue);
|
||||
|
||||
if i == line_wrap_size_minus_one {
|
||||
print_buffer.push(b'\n');
|
||||
|
||||
i = 0_usize;
|
||||
} else {
|
||||
i += 1_usize;
|
||||
}
|
||||
for sl in chunks_exact {
|
||||
print_buffer.extend_from_slice(sl);
|
||||
print_buffer.push(b'\n');
|
||||
}
|
||||
|
||||
stdout_lock.write_all(print_buffer)?;
|
||||
|
||||
// Remove the bytes that were just printed from `encoded_buffer`
|
||||
drop(encoded_buffer.drain(..number_of_bytes_to_drain));
|
||||
|
||||
if is_cleanup {
|
||||
if encoded_buffer.is_empty() {
|
||||
// Do not write a newline in this case, because two trailing newlines should never be printed
|
||||
|
|
Loading…
Reference in a new issue