NumberParser
Proves invariant text parsing into one exact numeric representation. Built-in integer/IEEE types bind their existing exact source parser. Custom types may provide the same static member. Parsing returns owned values and retains no text backing; syntax/range policies are explicit in ParseError. This concept grants no lifecycle permission, allocation or runtime witness.
Module std.text
import std.text;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
NumberParser
pub concept NumberParser<Self>Proves invariant text parsing into one exact numeric representation. Built-in integer/IEEE types bind their existing exact source parser. Custom types may provide the same static member. Parsing returns owned values and retains no text backing; syntax/range policies are explicit in ParseError. This concept grants no lifecycle permission, allocation or runtime witness. Self: Exact destination type, preserving its width and rounding policy.
Example
module example.number_parser;
from std.text import NumberParser, ParseError;
from std.prelude import Result;
fn ParseInvariant<T>(Text: str) -> Result<T, ParseError> where T: NumberParser, Defaultable {
return T.ParseNumber(Text, default(T));
}
fn Main() -> i32 {
switch ParseInvariant<i32>("42") {
case .Ok { Value = Value } => { if Value != 42 { return 1; } }
case .Err { Error = _ } => return 2;
}
switch ParseInvariant<i32>("42x") {
case .Ok { Value = _ } => return 3;
case .Err { Error = Error } => { if Error != ParseError.InvalidSyntax { return 4; } }
}
switch ParseInvariant<i8>("128") {
case .Ok { Value = _ } => return 5;
case .Err { Error = Error } => { if Error != ParseError.OutOfRange { return 6; } }
}
return 0;
}Example
module example.floating_parsing;
from std.text import TryParse, ParseError;
fn Main() -> i32 {
switch TryParse<f16>("0.1") {
case .Ok { Value = Value } => {
if bit_cast<u16>(Value) != 0x2E66_u16 { return 1; }
}
case .Err { Error = _ } => return 2;
}
switch TryParse<f64>("-0") {
case .Ok { Value = Value } => {
if bit_cast<u64>(Value) != 0x8000000000000000_u64 { return 3; }
}
case .Err { Error = _ } => return 4;
}
switch TryParse<f32>("1.0 trailing") {
case .Ok { Value = _ } => return 5;
case .Err { Error = Error } => {
if Error != ParseError.InvalidSyntax { return 6; }
}
}
return 0;
}NumberParser.ParseNumber
requires static fn ParseNumber(Text: str, Witness: Self) -> Result<Self, ParseError>;Parses one complete invariant numeric token without retaining input. Text: UTF8 input borrowed through the call. Witness: Exact type witness. Return: Owned value, InvalidSyntax or OutOfRange.