Crates.io | bstr_parse |
lib.rs | bstr_parse |
version | 0.1.0 |
source | src |
created_at | 2020-12-04 20:36:12.623814 |
updated_at | 2020-12-04 20:36:12.623814 |
description | Adds the ability to parse numbers out of `&[u8]`s. |
homepage | |
repository | https://github.com/smmalis37/bstr_parse |
max_upload_size | |
id | 319722 |
size | 27,606 |
Adds the ability to parse numbers out of &[u8]
s. Like so:
use bstr_parse::*;
let text: &[u8] = b"1234";
let num: u32 = text.parse().unwrap();
assert_eq!(num, 1234);
When dealing with text that is guaranteed to be pure ASCII, writing code that operates over a &[u8]
can often be faster than operating over a &str
, as this avoids all Unicode-related overhead.
str::parse
?Nope! The code in this crate has been copied straight out of std, with bits minimally modified. It turns out the parsing algorithm in std already operates over &[u8]
s, there's just no way to call it without getting an &str
first.
Parts were copied from:
Because often when parsing text you're doing more than just parsing out numbers. Many other common parsing operations will be faster over &[u8]
s than over &str
. However if you need to parse numbers in addition to these other things, without this crate you'd either have to operate entirely over &str
s or construct them as needed, paying (potentially) unnescessary Unicode overhead at every step.
Probably not. In the vast majority of use cases the overhead of proper Unicode handling will be so minimal as to not matter. I made this crate specifically for benchmark games, where doing absurd things to eek out every microsecond of performance is worthwhile.