String module

scan

procedure · 1 overloadSource

Reads values from text as format describes. Returns whether the whole text matched, and the values read, as Anys.

scan :: (format: string, text: string) -> (bool, [] Any)

Bytes of format must appear in the text as they are. %d and %i read a whole number (an s64), %f a float, %b true or false, and %s a string in double quotes, returned without the quotes. A %% matches and consumes a literal % and produces no value.

On a mismatch the result is false and the values read so far are returned. The values and the array are allocated in temporary storage, so they are only valid until it is reset. Nothing is freed by the caller.

Parameters

format: string
text: string

Returns

bool
[] Any
ExampleOpen in playground ↗
ok, values := scan("%d %s", "-8 \"yes\"");
assert(ok && values.count == 2);
assert((cast(*s64) values[0].value_pointer).* == -8);
assert((cast(*string) values[1].value_pointer).* == "yes");

percent_ok, percent_values := scan("%d%%", "50%");
assert(percent_ok && percent_values.count == 1);

See also