Program Rust
parse_partial_number
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
161 ///162 /// This creates a representation of the float as the163 /// significant digits and the decimal exponent.164 fn parse_partial_number(mut s: &[u8]) -> Option<(Decimal, usize)> {165 debug_assert!(!s.is_empty());166 167 // parse initial digits before dot168 let mut mantissa = 0_u64;169 let start = s;170 let tmp = try_parse_digits(s, mantissa);171 s = tmp.0;172 mantissa = tmp.1;173 let mut n_digits = s.offset_from(start);174 175 // handle dot with the following digits176 let mut n_after_dot = 0;177 let mut exponent = 0_i64;178 let int_end = s;179 180 if let Some((&b'.', s_next)) = s.split_first() {181 s = s_next;182 let before = s;183 let tmp = try_parse_digits(s, mantissa);184 s = tmp.0;185 mantissa = tmp.1;186 n_after_dot = s.offset_from(before);187 exponent = -n_after_dot as i64;188 }189 190 n_digits += n_after_dot;191 if n_digits == 0 {192 return None;193 }194 195 // handle scientific format196 let mut exp_number = 0_i64;197 if let Some((&c, s_next)) = s.split_first() {198 if c == b'e' || c == b'E' {199 s = s_next;200 // If None, we have no trailing digits after exponent, or an invalid float.201 exp_number = parse_scientific(&mut s)?;202 exponent += exp_number;203 }204 }205 206 let len = s.offset_from(start) as _;207 208 // handle uncommon case with many digits209 if n_digits <= 19 {210 return Some((Decimal { exponent, mantissa, negative: false, many_digits: false }, len));211 }212 213 n_digits -= 19;214 let mut many_digits = false;215 // The indexed loop is equivalent to the source `while let split_first`:216 // it tests nonemptiness before the same byte read and consumes one byte217 // only on the two source continuation cases. This avoids Aeneas's loop218 // context mismatch without changing checked subtraction or slice bounds.219 n_digits = adjust_many_digit_count(start, n_digits);220 if n_digits > 0 {221 // at this point we have more than 19 significant digits, let's try again222 many_digits = true;223 let retried = parse_many_digits(start, int_end, exp_number);224 mantissa = retried.0;225 exponent = retried.1;226 }227 228 Some((Decimal { exponent, mantissa, negative: false, many_digits }, len))229 }230 231 /// Try to parse a non-special floating point number,232 /// as well as two slices with integer and fractional partsAeneas declaration
The Lean below was mechanically extracted by Aeneas from Rust.
def num.imp.dec2flt.parse.parse_partial_number
(s : Slice Std.U8) :
Result (Option (num.imp.dec2flt.decimal.Decimal × Std.Usize))
:= do
let b ← core.slice.Slice.is_empty s
massert (¬ b)
let (s1, mantissa) ← num.imp.dec2flt.parse.try_parse_digits s 0#u64
let n_digits ←
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from s1 s
let o ← core.slice.Slice.split_first s1
let (s2, mantissa1, n_after_dot, exponent) ←
match o with
| none => ok (s1, mantissa, 0#isize, 0#i64)
| some p =>
let (i, s_next) := p
match i with
| 46#uscalar =>
do
let (s3, mantissa2) ←
num.imp.dec2flt.parse.try_parse_digits s_next mantissa
let n_after_dot1 ←
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
s3 s_next
let i1 ← -. n_after_dot1
let exponent1 ← lift (IScalar.cast .I64 i1)
ok (s3, mantissa2, n_after_dot1, exponent1)
| _ => ok (s1, mantissa, 0#isize, 0#i64)
let n_digits1 ← n_digits + n_after_dot
if n_digits1 = 0#isize
then ok none
else
let o1 ← core.slice.Slice.split_first s2
match o1 with
| none =>
let i ←
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from s2
s
let len ← lift (IScalar.hcast .Usize i)
if n_digits1 <= 19#isize
then
ok (some
({
exponent,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
else
let n_digits2 ← n_digits1 - 19#isize
let n_digits3 ←
num.imp.dec2flt.parse.adjust_many_digit_count s n_digits2
if n_digits3 > 0#isize
then
let (mantissa2, exponent1) ←
num.imp.dec2flt.parse.parse_many_digits s s1 0#i64
ok (some
({
exponent := exponent1,
mantissa := mantissa2,
negative := false,
many_digits := true
}, len))
else
ok (some
({
exponent,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
| some p =>
let (c, s_next) := p
if c = 101#u8
then
let (o2, s_next1) ← num.imp.dec2flt.parse.parse_scientific s_next
let cf ← core.option.Option.Insts.CoreOpsTry_traitTry.branch o2
match cf with
| core.ops.control_flow.ControlFlow.Continue val =>
let exponent1 ← exponent + val
let i ←
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
s_next1 s
let len ← lift (IScalar.hcast .Usize i)
if n_digits1 <= 19#isize
then
ok (some
({
exponent := exponent1,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
else
let n_digits2 ← n_digits1 - 19#isize
let n_digits3 ←
num.imp.dec2flt.parse.adjust_many_digit_count s n_digits2
if n_digits3 > 0#isize
then
let (mantissa2, exponent2) ←
num.imp.dec2flt.parse.parse_many_digits s s1 val
ok (some
({
exponent := exponent2,
mantissa := mantissa2,
negative := false,
many_digits := true
}, len))
else
ok (some
({
exponent := exponent1,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
| core.ops.control_flow.ControlFlow.Break residual =>
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual
(num.imp.dec2flt.decimal.Decimal × Std.Usize) residual
else
if c = 69#u8
then
let (o2, s_next1) ← num.imp.dec2flt.parse.parse_scientific s_next
let cf ← core.option.Option.Insts.CoreOpsTry_traitTry.branch o2
match cf with
| core.ops.control_flow.ControlFlow.Continue val =>
let exponent1 ← exponent + val
let i ←
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
s_next1 s
let len ← lift (IScalar.hcast .Usize i)
if n_digits1 <= 19#isize
then
ok (some
({
exponent := exponent1,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
else
let n_digits2 ← n_digits1 - 19#isize
let n_digits3 ←
num.imp.dec2flt.parse.adjust_many_digit_count s n_digits2
if n_digits3 > 0#isize
then
let (mantissa2, exponent2) ←
num.imp.dec2flt.parse.parse_many_digits s s1 val
ok (some
({
exponent := exponent2,
mantissa := mantissa2,
negative := false,
many_digits := true
}, len))
else
ok (some
({
exponent := exponent1,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
| core.ops.control_flow.ControlFlow.Break residual =>
core.option.Option.Insts.CoreOpsTry_traitFromResidualOptionInfallible.from_residual
(num.imp.dec2flt.decimal.Decimal × Std.Usize) residual
else
let i ←
SliceU8.Insts.Dec2flt_modelNumImpDec2fltCommonByteSlice.offset_from
s2 s
let len ← lift (IScalar.hcast .Usize i)
if n_digits1 <= 19#isize
then
ok (some
({
exponent,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))
else
let n_digits2 ← n_digits1 - 19#isize
let n_digits3 ←
num.imp.dec2flt.parse.adjust_many_digit_count s n_digits2
if n_digits3 > 0#isize
then
let (mantissa2, exponent1) ←
num.imp.dec2flt.parse.parse_many_digits s s1 0#i64
ok (some
({
exponent := exponent1,
mantissa := mantissa2,
negative := false,
many_digits := true
}, len))
else
ok (some
({
exponent,
mantissa := mantissa1,
negative := false,
many_digits := false
}, len))Operations used here
parse_scientific · Program Rust
parse_many_digits · Program Rust
Source details
| Declaration | num.imp.dec2flt.parse.parse_partial_number |
|---|---|
| File | num/imp/../../dec2flt/parse.rs |
| Lines | 164:0–229:1 |
| Program version | Revised program |