mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
tr: use as_bytes on whole output string
On my environment on 1MiB file with this fix tr takes 30ms, instead of 44ms without fix.
This commit is contained in:
parent
ea43608ab8
commit
c59e375c7a
1 changed files with 4 additions and 5 deletions
|
@ -71,7 +71,7 @@ fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) {
|
||||||
let mut locked_stdin = stdin.lock();
|
let mut locked_stdin = stdin.lock();
|
||||||
let mut buffered_stdout = BufWriter::new(stdout());
|
let mut buffered_stdout = BufWriter::new(stdout());
|
||||||
let mut buf = String::with_capacity(BUFFER_LEN + 4);
|
let mut buf = String::with_capacity(BUFFER_LEN + 4);
|
||||||
let mut char_output_buffer: [u8; 4] = [0;4];
|
let mut output_buf = String::with_capacity(BUFFER_LEN + 4);
|
||||||
|
|
||||||
let mut s2_prev = '_';
|
let mut s2_prev = '_';
|
||||||
for i in set1 {
|
for i in set1 {
|
||||||
|
@ -85,13 +85,12 @@ fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) {
|
||||||
|
|
||||||
{ // isolation to make borrow checker happy
|
{ // isolation to make borrow checker happy
|
||||||
let output_stream = buf.chars().map(|c| *map.get(&(c as usize)).unwrap_or(&c));
|
let output_stream = buf.chars().map(|c| *map.get(&(c as usize)).unwrap_or(&c));
|
||||||
for c in output_stream {
|
output_buf.extend(output_stream);
|
||||||
let char_as_bytes = c.encode_utf8(&mut char_output_buffer);
|
buffered_stdout.write_all(output_buf.as_bytes()).unwrap();
|
||||||
buffered_stdout.write_all(char_as_bytes.as_bytes()).unwrap();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.clear();
|
buf.clear();
|
||||||
|
output_buf.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue