[go: nahoru, domu]

Skip to content

Parse integers from strings, with support for base prefixes

License

Notifications You must be signed in to change notification settings

jwodder/strtoint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI Status codecov.io Minimum Supported Rust Version 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.

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());