Program Rust
parse_partial_number
This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.
158 ///159 /// This creates a representation of the float as the160 /// significant digits and the decimal exponent.161 fn parse_partial_number(mut s: &[u8]) -> Option<(Decimal, usize)> {162 debug_assert!(!s.is_empty());163 164 // parse initial digits before dot165 let mut mantissa = 0_u64;166 let start = s;167 let tmp = try_parse_digits(s, mantissa);168 s = tmp.0;169 mantissa = tmp.1;170 let mut n_digits = s.offset_from(start);171 172 // handle dot with the following digits173 let mut n_after_dot = 0;174 let mut exponent = 0_i64;175 let int_end = s;176 177 if let Some((&b'.', s_next)) = s.split_first() {178 s = s_next;179 let before = s;180 let tmp = try_parse_digits(s, mantissa);181 s = tmp.0;182 mantissa = tmp.1;183 n_after_dot = s.offset_from(before);184 exponent = -n_after_dot as i64;185 }186 187 n_digits += n_after_dot;188 if n_digits == 0 {189 return None;190 }191 192 // handle scientific format193 let mut exp_number = 0_i64;194 if let Some((&c, s_next)) = s.split_first() {195 if c == b'e' || c == b'E' {196 s = s_next;197 // If None, we have no trailing digits after exponent, or an invalid float.198 exp_number = parse_scientific(&mut s)?;199 exponent += exp_number;200 }201 }202 203 let len = s.offset_from(start) as _;204 205 // handle uncommon case with many digits206 if n_digits <= 19 {207 return Some((Decimal { exponent, mantissa, negative: false, many_digits: false }, len));208 }209 210 n_digits -= 19;211 let mut many_digits = false;212 // The indexed loop is equivalent to the source `while let split_first`:213 // it tests nonemptiness before the same byte read and consumes one byte214 // only on the two source continuation cases. This avoids Aeneas's loop215 // context mismatch without changing checked subtraction or slice bounds.216 n_digits = adjust_many_digit_count(start, n_digits);217 if n_digits > 0 {218 // at this point we have more than 19 significant digits, let's try again219 many_digits = true;220 let retried = parse_many_digits(start, int_end, exp_number);221 mantissa = retried.0;222 exponent = retried.1;223 }224 225 Some((Decimal { exponent, mantissa, negative: false, many_digits }, len))226 }227 228 /// Try to parse a non-special floating point number,229 /// 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 | 161:0–226:1 |
| Program version | Original program |