blob: f731d13d36f420a01d7de6009132a65ee0f14a47 [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Layer-0 `#![no_std]` integer and decimal floating-point parsers
//! (`vx_core::num`).
//!
//! Provides panic-free, `unsafe`-free (`#![forbid(unsafe_code)]`),
//! zero-`SRET(x8)` replacements for `core`'s out-of-line numeric parsers
//! (`u64::from_str`, `u64::from_str_radix`, `i64::from_str`,
//! `i64::from_str_radix`, `f64::from_str`) organized under typed submodules
//! ([`u64`], [`i64`], [`f64`]).
//!
//! Inline/zero-cost types from `core::num` (such as `NonZeroU32` and
//! `IntErrorKind`) are not re-exported here; use `core::num` directly.
#![forbid(unsafe_code)]
mod flt;
mod int;
use ::core::num::IntErrorKind;
/// Error returned when parsing an integer via [`u64`] or [`i64`] fails.
///
/// Matches `core::num::ParseIntError::kind`. Compact 1-byte representation so
/// `Result<u64, ParseIntError>` and `Result<i64, ParseIntError>` fit in
/// registers `x0, x1` (`16 B`, zero `SRET(x8)`).
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct ParseIntError {
kind: IntErrorKind,
}
const _: () = assert!(::core::mem::size_of::<ParseIntError>() == 1);
const _: () =
assert!(::core::mem::size_of::<Result<::core::primitive::u64, ParseIntError>>() == 16);
const _: () =
assert!(::core::mem::size_of::<Result<::core::primitive::i64, ParseIntError>>() == 16);
impl ParseIntError {
#[inline(always)]
pub(crate) const fn new(kind: IntErrorKind) -> Self {
Self { kind }
}
/// Return the detailed cause of the parse failure, matching
/// `core::num::ParseIntError::kind`.
#[inline(always)]
pub const fn kind(&self) -> &IntErrorKind {
&self.kind
}
}
// Implemented manually with `#[inline(always)]` rather than `#[derive(Debug)]`
// so `vx_core.o` does not emit an out-of-line `Debug::fmt` referencing
// unlinked `core::fmt::Formatter::write_str`.
impl ::core::fmt::Debug for ParseIntError {
#[inline(always)]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self.kind {
IntErrorKind::Empty => f.write_str("ParseIntError { kind: Empty }"),
IntErrorKind::InvalidDigit => f.write_str("ParseIntError { kind: InvalidDigit }"),
IntErrorKind::PosOverflow => f.write_str("ParseIntError { kind: PosOverflow }"),
IntErrorKind::NegOverflow => f.write_str("ParseIntError { kind: NegOverflow }"),
IntErrorKind::Zero => f.write_str("ParseIntError { kind: Zero }"),
_ => f.write_str("ParseIntError"),
}
}
}
/// Error returned when parsing an `f64` via [`f64::from_str`] fails.
///
/// Zero-sized so `Result<f64, ParseFloatError>` fits in registers `x0, x1`
/// (`16 B`, zero `SRET(x8)`).
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct ParseFloatError;
const _: () = assert!(::core::mem::size_of::<ParseFloatError>() == 0);
const _: () =
assert!(::core::mem::size_of::<Result<::core::primitive::f64, ParseFloatError>>() == 16);
// Implemented manually with `#[inline(always)]` rather than `#[derive(Debug)]`
// so `vx_core.o` does not emit an out-of-line `Debug::fmt` referencing
// unlinked `core::fmt::Formatter::write_str`.
impl ::core::fmt::Debug for ParseFloatError {
#[inline(always)]
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str("ParseFloatError")
}
}
/// Verified panic-free `u64` ASCII string parsers (`from_str`, `from_str_radix`).
pub mod u64 {
pub use super::int::{u64_from_str as from_str, u64_from_str_radix as from_str_radix};
}
/// Verified panic-free `i64` ASCII string parsers (`from_str`, `from_str_radix`).
pub mod i64 {
pub use super::int::{i64_from_str as from_str, i64_from_str_radix as from_str_radix};
}
/// Verified panic-free `f64` ASCII decimal string parser (`from_str`).
pub mod f64 {
pub use super::flt::f64_from_str as from_str;
}