Portable decimal byte-to-integer parsing with SIMD, prefix parsing, and 128-bit results, ported from atoi_simd
///|
test "signed limits and prefix byte counts" {
let minimum : Int64 = @atoi.parse(b"-9223372036854775808")
assert_eq(minimum, -9223372036854775808L)
let prefix : (UInt64, Int32) = @atoi.parse_prefix(
b"+000000000000000000001234tail",
skip_zeroes=true,
skip_plus=true,
)
assert_eq(prefix, (1234UL, 25))
}///|
test "exact 128-bit decimal output" {
let maximum : @atoi.UInt128 = @atoi.parse(
b"340282366920938463463374607431768211455",
)
assert_eq(maximum.to_words(), (0xFFFFFFFFFFFFFFFFUL, 0xFFFFFFFFFFFFFFFFUL))
let minimum : @atoi.Int128 = @atoi.parse(
b"-170141183460469231731687303715884105728",
)
assert_eq(minimum.to_string(), "-170141183460469231731687303715884105728")
}pub(open) trait Parse {
fn parse_decimal(BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Self, Int) raise ParseError
}fn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Byte, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Int, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Int16, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Int64, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (UInt, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (UInt16, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (UInt64, Int) raise ParseErrorpub(open) trait ParseNeg {
fn parse_negative_decimal(BytesView, prefix~ : Bool, skip_zeroes~ : Bool, simd~ : Bool) -> (Self, Int) raise ParseError
}fn parse_negative_decimal(data : BytesView, prefix~ : Bool, skip_zeroes~ : Bool, simd~ : Bool) -> (Int, Int) raise ParseErrorfn parse_negative_decimal(data : BytesView, prefix~ : Bool, skip_zeroes~ : Bool, simd~ : Bool) -> (Int16, Int) raise ParseErrorfn parse_negative_decimal(data : BytesView, prefix~ : Bool, skip_zeroes~ : Bool, simd~ : Bool) -> (Int64, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Int128, Int) raise ParseErrorfn parse_negative_decimal(data : BytesView, prefix~ : Bool, skip_zeroes~ : Bool, simd~ : Bool) -> (Int128, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (Int8, Int) raise ParseErrorfn parse_negative_decimal(data : BytesView, prefix~ : Bool, skip_zeroes~ : Bool, simd~ : Bool) -> (Int8, Int) raise ParseErrorfn parse_decimal(data : BytesView, positive_only~ : Bool, prefix~ : Bool, skip_zeroes~ : Bool, skip_plus~ : Bool, simd~ : Bool) -> (UInt128, Int) raise ParseErrorfn[T : Parse] parse(data : BytesView, skip_zeroes? : Bool, skip_plus? : Bool, simd? : Bool) -> T raise ParseErrortest {
let value : Int64 = @atoi.parse(b"-9223372036854775808")
assert_eq(value, -9223372036854775808L)
}fn[T : ParseNeg] parse_neg(data : BytesView, skip_zeroes? : Bool, simd? : Bool) -> T raise ParseErrorfn[T : Parse] parse_prefix(data : BytesView, skip_zeroes? : Bool, skip_plus? : Bool, simd? : Bool) -> (T, Int) raise ParseErrortest {
let result : (UInt64, Int32) = @atoi.parse_prefix(b"+123rest", skip_plus=true)
assert_eq(result, (123UL, 4))
}fn[T : ParseNeg] parse_prefix_neg(data : BytesView, skip_zeroes? : Bool, simd? : Bool) -> (T, Int) raise ParseErrorfn[T : Parse] parse_prefix_pos(data : BytesView, skip_zeroes? : Bool, simd? : Bool) -> (T, Int) raise ParseErrorInstall
Download zipPortable decimal byte-to-integer parsing with SIMD, prefix parsing, and 128-bit results, ported from atoi_simd