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;
}