Expand description
§Fast &[u8] to integer parser
SIMD (fast) parsing is supported on x86_64 (SSE4.1, AVX2) and on Arm64 (aarch64, Neon), but this library works even if you don’t have a SIMD supported cpu (and it will be still faster than str::parse).
Supports negative values and validates the input.
Supported output types: u8, i8, u16, i16, u32, i32, u64, i64, u128, i128, usize, isize.
Has good test coverage, and can be considered safe.
To enable SIMD it needs the target-feature or target-cpu flags set, or it will fallback to non-SIMD functions.
You can copy the ./.cargo/config.toml to your project, or use one of the following environment variables:
-
RUSTFLAGS="-C target-feature=+sse2,+sse3,+sse4.1,+ssse3,+avx,+avx2"for x86_64; -
RUSTFLAGS="-C target-feature=+neon"for Arm64; -
RUSTFLAGS="-C target-cpu=native"will optimize for your current cpu.
If you have &str then use .as_bytes()
Supports no_std with --no-default-features
§Examples
// a drop-in replacement for `str::parse`
assert_eq!(atoi_simd::parse::<u64, true, true>(b"+000000000000000000001234"), Ok(1234_u64));
let val: u64 = atoi_simd::parse::<_, false, false>(b"1234").unwrap();
assert_eq!(val, 1234_u64);
assert_eq!(atoi_simd::parse::<i64, false, false>(b"-2345"), Ok(-2345_i64));
assert_eq!(atoi_simd::parse_neg::<i64, false>(b"2345"), Ok(-2345_i64));
assert_eq!(atoi_simd::parse_prefix::<u64, false, false>(b"123something_else"), Ok((123_u64, 3)));Enums§
Traits§
- Parse
- Note: all of the provided methods are
#[inline(always)] - Parse
Neg - Note: all of the provided methods are
#[inline(always)]
Functions§
- parse
- Parses a slice of digits, and checks for the first ‘-’ char for signed integers.
- parse_
any Deprecated - parse_
any_ neg Deprecated - parse_
any_ pos Deprecated - parse_
neg - Parses a negative integer. Does not check any signs, so slice must not contain them (including ‘-’ sign).
- parse_
pos - Parses a positive integer. Does not check any signs, so slice must not contain them.
- parse_
prefix - Parses a slice of digits until it reaches an invalid character, and checks for the first ‘-’ char for signed integers. Returns the parsed value and the parsed size of the slice.
- parse_
prefix_ neg - Parses a negative integer until it reaches an invalid character. Returns the parsed value and the parsed size of the slice. Does not check any signs, so slice must not contain them (including ‘-’ sign).
- parse_
prefix_ pos - Parses a positive integer until it reaches an invalid character. Returns the parsed value and the parsed size of the slice. Does not check any signs, so slice must not contain them.
- parse_
skipped Deprecated