formattedRead

Reads an input range according to a format string and returns a tuple of Args with the read values.

Format specifiers with format character 'd', 'u' and 'c' can take a '*' parameter for skipping values.

The second version of formattedRead takes the format string as template argument. In this case, it is checked for consistency at compile-time.

  1. Tuple!Args formattedRead(Range r, const(Char)[] fmt)
    template formattedRead(Args...)
    Tuple!Args
    formattedRead
    (
    Range
    Char
    )
    (
    auto ref Range r
    ,
    const(Char)[] fmt
    )
    if (
    Args.length &&
    )
  2. uint formattedRead(Range r, const(Char)[] fmt, Args args)
  3. uint formattedRead(Range r, Args args)
  4. template formattedRead(alias fmt, Args...)

Members

Functions

formattedRead
Tuple!Args formattedRead(Range r, const(Char)[] fmt)

Parameters

Args

a variadic list of types of the arguments

Examples

import std.exception : assertThrown;
import std.format : FormatException;
import std.typecons : tuple;

auto complete = "hello!34.5:124".formattedRead!(string, double, int)("%s!%s:%s");
assert(complete == tuple("hello", 34.5, 124));

// reading ends early
assertThrown!FormatException("hello!34.5:".formattedRead!(string, double, int)("%s!%s:%s"));

Skipping values

import std.format : FormatException;
import std.typecons : tuple;

auto result = "orange: (12%) 15.25".formattedRead!(string, double)("%s: (%*d%%) %f");
assert(result == tuple("orange", 15.25));

The format string can be checked at compile-time

import std.exception : assertThrown;
import std.format : FormatException;
import std.typecons : tuple;

auto expected = tuple("hello", 124, 34.5);
auto result = "hello!124:34.5".formattedRead!("%s!%s:%s", string, int, double);
assert(result == expected);

assertThrown!FormatException("hello!34.5:".formattedRead!("%s!%s:%s", string, double, int));

Compile-time consistency check

import std.format : FormatException;
import std.typecons : tuple;

static assert(!__traits(compiles, "orange: (12%) 15.25".formattedRead!("%s: (%*d%%) %f", string, double)));

Meta