String module

scan2

procedure · 1 overloadSource

Reads values from text as format describes, storing them through the pointers in args. Returns whether the whole text matched.

scan2 :: (text: string, format: string, args: ..Any) -> bool

The directives are those of scan. Each directive stores into the next argument, which must be a pointer to a number, a bool or a string. A number that does not fit the target (a 256 into a u8) makes the call fail, as does a missing or null argument. On failure the targets read before the mismatch keep their new values and the others are not touched. A string result points into text.

Floats read into a float or a float64, and a literal percent sign is written %%:

Parameters

text: string
format: string
args: ..Any

Returns

bool
ExampleOpen in playground ↗
count: int;
flag: bool;
name: string;
ok := scan2("7 true \"bob\"", "%d %b %s", *count, *flag, *name);
assert(ok && count == 7 && flag && name == "bob");
ExampleOpen in playground ↗
ratio: float;
precise: float64;
assert(scan2("1.25%", "%f%%", *ratio) && ratio == 1.25);
assert(scan2("1.23456789012345", "%f", *precise) && precise == 1.23456789012345);

See also