#atoi #conversion #integer

atoi

Parse integers directly from [u8] slices in safe code

11 releases

new 0.4.0 Nov 20, 2020
0.3.2 Nov 5, 2019
0.3.1 Dec 16, 2018
0.2.3 Mar 5, 2018
0.2.2 Jul 5, 2017

#15 in Parser tooling

Download history 6637/week @ 2020-08-04 7979/week @ 2020-08-11 5847/week @ 2020-08-18 6578/week @ 2020-08-25 6252/week @ 2020-09-01 6596/week @ 2020-09-08 9307/week @ 2020-09-15 9978/week @ 2020-09-22 10366/week @ 2020-09-29 8805/week @ 2020-10-06 10179/week @ 2020-10-13 10703/week @ 2020-10-20 10551/week @ 2020-10-27 12027/week @ 2020-11-03 13703/week @ 2020-11-10 12226/week @ 2020-11-17

41,909 downloads per month
Used in 74 crates (14 directly)

MIT license

28KB
366 lines

atoi-rs

Parse integers directly from [u8] slices in safe code

Reasons to use this crate

Starting from a binary or ascii format you can parse an integer around three times as fast as with the more idiomatic detour over utf8. The crate comes with benchmarks so you can see for yourself.

The FromRadix10Checked trait also provides a way to parse integers very fast and safe, as its implementation only performs checked arithmetics for the one digit that may actually overflow.

Example

Parsing from a slice

use atoi::atoi;
assert_eq!(Some(42), atoi::<u32>(b"42"));

Note that if you want to know how much of the input has been used, you can use the FromRadix10 trait, for example:

use atoi::FromRadix10;

/// Return the parsed integer and remaining slice if successful.
fn atoi_with_rest<I: FromRadix10>(text: &[u8]) -> Option<(&[u8], I)> {
    match I::from_radix_10(text) {
        (_, 0) => None,
        (n, used) => Some((&[used..], n)),
    }
}

This crate as more to offer! Check out the full documentation at docs.rs.

Dependencies

~145KB