Skip to main content

parse_decimal_u64

Function parse_decimal_u64 

Source
pub fn parse_decimal_u64(bytes: &[u8]) -> Option<u64>
Expand description

SWAR decimal parser: parse an unsigned 64-bit integer from a byte slice of ASCII digits. Returns None if the slice is empty, contains a non-digit byte, or overflows u64.

Uses SWAR (SIMD Within A Register) when the input is at least 8 bytes long: 8 ASCII digits are parsed in a single u64 multiply-shift pipeline, eliminating the per-byte branch the stdlib <u64 as FromStr>::from_str walks. For inputs shorter than 8 bytes the scalar fallback runs — a tight * 10 + d loop that LLVM auto-vectorises on most targets.

Pure-safe Rust — preserves the workspace #![forbid(unsafe_code)] invariant. Validates every byte is in b'0'..=b'9' before computing the result, so malformed input never produces a garbage answer.

§Examples

use noyalib::simd::parse_decimal_u64;
assert_eq!(parse_decimal_u64(b"12345"), Some(12_345));
assert_eq!(parse_decimal_u64(b"9223372036854775807"),
           Some(9_223_372_036_854_775_807));
assert_eq!(parse_decimal_u64(b""), None);
assert_eq!(parse_decimal_u64(b"12a4"), None);
assert_eq!(parse_decimal_u64(b"99999999999999999999"), None); // overflow