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 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 source digit callback.  `count` advances139         // exactly once per accepted byte and the bound keeps every checked140         // exponent operation in the same safe range as the source.141         let mut count = 0;142         while count < s.len() && s[count].wrapping_sub(b'0') < 10 {143             let digit = s[count].wrapping_sub(b'0');144             if exponent < 0x10000 {145                 exponent = 10 * exponent + digit as i64;146             }147             count += 1;148         }149         *s_ref = &s[count..];150         if negative { Some(-exponent) } else { Some(exponent) }151     } else {152         *s_ref = s;153         None154     }155 }156 157 /// Parse a partial, non-special floating point number.158 ///

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–155:1
Program versionOriginal program