diff options
author | Omer Ben-Amram <omerbenamram@gmail.com> | 2019-06-03 16:27:53 +0300 |
---|---|---|
committer | Omer Ben-Amram <omerbenamram@gmail.com> | 2019-06-03 16:27:53 +0300 |
commit | 816005abbac3ca1e307dfa0049eb65c43bc16ef8 (patch) | |
tree | 593e5f351faa16a8c623af828c6a80b98346a9c1 | |
parent | 24e48139e30a78e7675ae9bea3c1520346c25442 (diff) |
optimise execution time by 25%
-rw-r--r-- | src/utils.rs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/utils.rs b/src/utils.rs index fb9b812..25470e7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,19 @@ use crate::ReadSeek; use byteorder::ReadBytesExt; use std::char::decode_utf16; +use std::fmt::Write; use std::io; pub fn to_hex_string(bytes: &[u8]) -> String { - let strs: Vec<String> = bytes.iter().map(|b| format!("{:02X}", b)).collect(); - strs.join("") + let len = bytes.len(); + // Each byte is represented by 2 ascii bytes. + let mut s = String::with_capacity(len * 2); + + for byte in bytes { + write!(s, "{:02X}", byte).expect("Writing to an allocated string cannot fail"); + } + + s } /// Reads a utf16 string from the given stream. |