← core

core/parse.kur

//! Parsing integers from byte views.
// core::parse -- integers from text. Each reads a byte view through
// whatever capability permits it, and gives nothing rather than a wrong
// number: for an empty view, any byte that is not part of the number, or a
// value that does not fit.
use package as core;

use core::option::Option;
use core::slice::{ReadBytes, byte_or_zero};
use core::ascii::is_digit;

/// The `u64` that `bytes` spells in decimal digits.
forall C, S where ReadBytes[C, S], Copy[S].
fn parse_u64(&cx: C, bytes: S) -> Option[u64] {
    let n = ReadBytes[C, S]::len(&cx, bytes);
    if n == 0usize {
        return Option::None;
    }
    let max = 18446744073709551615u64;
    var value = 0u64;
    var i = 0usize;
    var ok = true;
    while ok && i < n {
        let b = byte_or_zero(&cx, bytes, i);
        if !is_digit(b) {
            ok = false;
        } else {
            let digit = ((b - b'0') as u64);
            if value >(max - digit) / 10u64 {
                ok = false;
            } else {
                value = value * 10u64 + digit;
            }
        }
        i = i + 1usize;
    }
    let parsed = if ok {
        Option::Some.(value)
    } else {
        Option::None
    };
    parsed
}

/// The `usize` that `bytes` spells in decimal digits: `parse_u64`'s number,
/// or nothing when it is past `usize::MAX`, which is the target's (`usize`
/// is pointer-sized).
forall C, S where ReadBytes[C, S], Copy[S].
fn parse_usize(&cx: C, bytes: S) -> Option[usize] {
    match parse_u64(&cx, bytes) {
        Some(v) => if v <= (usize::MAX as u64) { Option::Some.(v as usize) } else { Option::None },
        None => Option::None,
    }
}

/// The `i64` that `bytes` spells in decimal digits after an optional '-'.
forall C, S where ReadBytes[C, S], Copy[S].
fn parse_i64(&cx: C, bytes: S) -> Option[i64] {
    let n = ReadBytes[C, S]::len(&cx, bytes);
    let negative = n > 0usize && byte_or_zero(&cx, bytes, 0usize) == b'-';
    let skip = if negative { 1usize } else { 0usize };
    if n == skip {
        return Option::None;
    }
    var magnitude = 0u64;
    var i = skip;
    var ok = true;
    // 2^63: the magnitude of the most negative value, one past the most
    // positive one.
    let limit = 9223372036854775808u64;
    while ok && i < n {
        let b = byte_or_zero(&cx, bytes, i);
        if !is_digit(b) {
            ok = false;
        } else {
            let digit = ((b - b'0') as u64);
            if magnitude >(limit - digit) / 10u64 {
                ok = false;
            } else {
                magnitude = magnitude * 10u64 + digit;
            }
        }
        i = i + 1usize;
    }
    let fits = ok && (negative || magnitude < limit);
    // Two's complement: negating the magnitude in `u64` and reinterpreting
    // gives every negative value, including the most negative one.
    let value = if negative {
        ((0u64 - magnitude) as i64)
    } else {
        (magnitude as i64)
    };
    let parsed = if fits {
        Option::Some.(value)
    } else {
        Option::None
    };
    parsed
}