#hex #binary #octal #int #parse-int #primitive-integer

no-std strtoint

Parse integers from strings, with support for base prefixes

2 releases

0.1.0 Nov 5, 2022
0.1.0-alpha Nov 4, 2022

#202 in Parser tooling

28 downloads per month
Used in 2 crates

MIT license

13KB
138 lines

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI Status codecov.io MIT License

GitHub | crates.io | Documentation | Issues | Changelog

strtoint provides a function of the same name for parsing integer literals from strings, with support for the base prefixes 0x, 0o, and 0b for hexadecimal, octal, and binary literals, respectively.

This crate supports parsing into all primitive integer types built into Rust, along with their "NonZero" equivalents.

If the std feature (enabled by default) is disabled, this crate will be built in no-std mode. The only difference is that StrToIntError only implements the std::error::Error trait under std.

Installation

strtoint requires version 1.56 of Rust or higher. To use the strtoint library in your Cargo project, add the following to your Cargo.toml:

[dependencies]
strtoint = "0.1.0"

Examples

use core::num::NonZeroUsize;
use strtoint::strtoint;

assert_eq!(strtoint::<i32>("123").unwrap(), 123);
assert_eq!(strtoint::<u32>("0xabcd_FFFF").unwrap(), 2882404351);
assert_eq!(strtoint::<i16>("0o644").unwrap(), 420);
assert_eq!(strtoint::<i8>("-0b00101010").unwrap(), -42);
assert!(strtoint::<i64>("42.0").is_err());

assert_eq!(
    strtoint::<NonZeroUsize>("123_456").unwrap(),
    NonZeroUsize::new(123456).unwrap()
);
assert!(strtoint::<NonZeroUsize>("0").is_err());

No runtime deps