Source context

Program Rust

parse_scientific

This Rust is the input prepared for Aeneas. Modeling choices explains the adaptations from the production source.

118 }119 120 /// Parse the scientific notation component of a float.121 pub(super) fn parse_scientific(s_ref: &mut &[u8]) -> Option<i64> {122     let mut exponent = 0i64;123     let mut negative = false;124 125     let mut s = *s_ref;126 127     if let Some((&c, s_next)) = s.split_first() {128         negative = c == b'-';129         if c == b'-' || c == b'+' {130             s = s_next;131         }132     }133 134     // `matches!` with a guarded slice pattern triggers an Aeneas internal135     // error.  This short-circuit test reads the same first byte exactly when136     // it exists and therefore preserves both acceptance and evaluation order.137     if !s.is_empty() && s[0].is_ascii_digit() {138         // Specialized form of the revised source callback, shared with the139         // sibling slow parser. The guard tests the pre-update accumulator:140         // one digit can overshoot 2^59, up to 10 * 2^59 - 1, still below141         // i64::MAX. This is not a clamp. Subsequent digits are still consumed142         // and the first nondigit remains in the returned suffix, so the caller143         // performs the same whole-input validation even after updates stop.144         let mut count = 0;145         while count < s.len() && s[count].wrapping_sub(b'0') < 10 {146             let digit = s[count].wrapping_sub(b'0');147             if exponent < (1_i64 << 59) {148                 exponent = 10 * exponent + digit as i64;149             }150             count += 1;151         }152         *s_ref = &s[count..];153         if negative { Some(-exponent) } else { Some(exponent) }154     } else {155         *s_ref = s;156         None157     }158 }159 160 /// Parse a partial, non-special floating point number.161 ///

Aeneas declaration

The Lean below was mechanically extracted by Aeneas from Rust.

def num.imp.dec2flt.parse.parse_scientific
  (s_ref : Slice Std.U8) : Result ((Option Std.I64) × (Slice Std.U8)) := do
  let o ← core.slice.Slice.split_first s_ref
  let (negative, s) ←
    match o with
    | none => ok (false, s_ref)
    | some p =>
      do
      let (c, s_next) := p
      let s1 ←
        if c = 45#u8
        then ok s_next
        else if c = 43#u8
             then ok s_next
             else ok s_ref
      ok (c = 45#u8, s1)
  let b ← core.slice.Slice.is_empty s
  if b
  then ok (none, s)
  else
    let i ← Slice.index_usize s 0#usize
    let b1 ← core.num.U8.is_ascii_digit i
    if b1
    then
      let (exponent, count) ←
        num.imp.dec2flt.parse.parse_scientific_loop 0#i64 s 0#usize
      let s_ref1 ←
        core.slice.index.Slice.index
          (core.slice.index.SliceIndexRangeFromUsizeSlice Std.U8) s
          { start := count }
      if negative
      then let i1 ← -. exponent
           ok (some i1, s_ref1)
      else ok (some exponent, s_ref1)
    else ok (none, s)
Operations used here

is_ascii_digit · Rust library

split_first · Rust library

parse_scientific_loop · Program Rust

Source details
Declarationnum.imp.dec2flt.parse.parse_scientific
Filenum/imp/../../dec2flt/parse.rs
Lines121:0–158:1
Program versionRevised program