some suggestions
Created by: miquels
Hello,
in my code I need to convert a 128 bit number (a UUID) into a string. I thought of base85 and found your crate. I ended up not using it (fixed 128 bits -> base85 string is trivial), but while looking at the code I do have some suggestions. Mainly, that it's probably faster to use a match
than lazy_static, (which costs an atomic lookup) plus a hashtable lookup. Also, the BASE85_CHARS does not have to be in a lazy static at all, and it does not have to be statically allocated. I came up with the example code below. Which eventually I did not need, but at least the excercise kept me from doom-scrolling all night ... :].
Another suggestion: u32::from_be_bytes(indata[data_index .. data_index + 4].try_into().unwrap()]
.
I hope this is useful.
#[inline]
fn x85_to_char(x85: u8) -> u8 {
static B85_TO_CHAR: &'static[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
B85_TO_CHAR[x85 as usize]
}
#[inline]
fn char_to_x85(c: u8) -> u8 {
match c {
b'0' ..= b'9' => c - b'0',
b'A' ..= b'Z' => c - b'A' + 10,
b'a' ..= b'z' => c - b'a' + 36,
b'!' => 62,
b'#' => 63,
b'$' => 64,
b'%' => 65,
b'&' => 66,
b'(' => 67,
b')' => 68,
b'*' => 69,
b'+' => 70,
b'-' => 71,
b';' => 72,
b'<' => 73,
b'=' => 74,
b'>' => 75,
b'?' => 76,
b'@' => 77,
b'^' => 78,
b'_' => 79,
b'`' => 80,
b'{' => 81,
b'|' => 82,
b'}' => 83,
b'~' => 84,
_ => unreachable!(),
}
}