String module

parse_int

procedure · 1 overloadSource

Reads the next token as a whole number of type T (int by default). Only space and tab are skipped in front of it unless you pass spaces.

parse_int :: (
    line: *string,
    $T: Type = int,
    spaces := " \t",
    delimiters := DEFAULT_DELIMITERS
) -> (value: T, success: bool)

The second result is false when the token is not entirely a number, or there is none, or the number does not fit T (including a minus sign for an unsigned T). The value is then 0.

Parameters

line: *string
$T: Type
spaces := " \t"
delimiters := DEFAULT_DELIMITERS

Returns

value: T
success: bool
ExampleOpen in playground ↗
line := " 42;7";
value, ok := parse_int(*line);
assert(ok && value == 42);

big := "257";
small, small_ok := parse_int(*big, u8);
assert(!small_ok && small == 0);

See also