darkwyrm / base85 Goto Github PK
View Code? Open in Web Editor NEWA library for Base85 encoding as described in RFC1924
License: Mozilla Public License 2.0
A library for Base85 encoding as described in RFC1924
License: Mozilla Public License 2.0
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!(),
}
}
I noticed the library panics on certain inputs containing }
:
[dependencies]
base85 = "2.0.0"
fn main() {
println!("{:?}", base85::decode("}"));
}
$ cargo run
thread 'main' panicked at 'attempt to multiply with overflow', /home/finnb/.cargo/registry/src/index.crates.io-6f17d22bba15001f/base85-2.0.0/src/lib.rs:158:27
$ cargo run --release
Err(UnexpectedEof)
As far as I can tell, it is trying to compute 83*85^4=4332651875
which overflows u32
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.