1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +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:
Yury Krivopalov 2017-08-23 22:12:03 +03:00
parent ea43608ab8
commit c59e375c7a

View file

@ -71,7 +71,7 @@ fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) {
let mut locked_stdin = stdin.lock();
let mut buffered_stdout = BufWriter::new(stdout());
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 = '_';
for i in set1 {
@ -85,13 +85,12 @@ fn tr<'a>(set1: ExpandSet<'a>, mut set2: ExpandSet<'a>) {
{ // isolation to make borrow checker happy
let output_stream = buf.chars().map(|c| *map.get(&(c as usize)).unwrap_or(&c));
for c in output_stream {
let char_as_bytes = c.encode_utf8(&mut char_output_buffer);
buffered_stdout.write_all(char_as_bytes.as_bytes()).unwrap();
}
output_buf.extend(output_stream);
buffered_stdout.write_all(output_buf.as_bytes()).unwrap();
}
buf.clear();
output_buf.clear();
}
}