#integer #conversion #convert

no-std atoi

Parse integers directly from [u8] slices in safe code

13 releases (2 stable)

2.0.0 Oct 11, 2022
1.0.0 Jan 22, 2022
0.4.0 Nov 20, 2020
0.3.2 Nov 5, 2019
0.2.2 Jul 5, 2017

#6 in #conversion

Download history 192844/week @ 2023-11-18 216477/week @ 2023-11-25 242042/week @ 2023-12-02 221502/week @ 2023-12-09 212209/week @ 2023-12-16 108265/week @ 2023-12-23 181421/week @ 2023-12-30 232332/week @ 2024-01-06 226504/week @ 2024-01-13 233570/week @ 2024-01-20 235090/week @ 2024-01-27 238819/week @ 2024-02-03 240735/week @ 2024-02-10 229280/week @ 2024-02-17 255652/week @ 2024-02-24 232681/week @ 2024-03-02

999,346 downloads per month
Used in 1,057 crates (74 directly)

MIT license

29KB
367 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 has more to offer! Check out the full documentation at docs.rs.

Dependencies

~155KB