View source code
Display the source code in std/format.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.format.unformatValue

Reads a value from the given input range according to spec and returns it as type T.

T unformatValue(T, Range, Char) (
  ref Range input,
  scope const ref FormatSpec!Char spec
);

Parameters

NameDescription
T the type to return
input the input range to read from
spec the FormatSpec to use when reading from input

Returns

A value from input of type T

Throws

A FormatException if spec cannot read a type T

See Also

parse and to

Example

Booleans

auto str = "false";
auto spec = singleSpec("%s");
writeln(unformatValue!bool(str, spec)); // false

str = "1";
spec = singleSpec("%d");
assert(unformatValue!bool(str, spec));

Example

Null values

auto str = "null";
auto spec = singleSpec("%s");
writeln(str.unformatValue!(typeof(null))(spec)); // null

Example

Integrals

auto str = "123";
auto spec = singleSpec("%s");
writeln(str.unformatValue!int(spec)); // 123

str = "ABC";
spec = singleSpec("%X");
writeln(str.unformatValue!int(spec)); // 2748

str = "11610";
spec = singleSpec("%o");
writeln(str.unformatValue!int(spec)); // 5000

Example

Floating point numbers

import std.math : isClose;

auto str = "123.456";
auto spec = singleSpec("%s");
assert(str.unformatValue!double(spec).isClose(123.456));

Example

Character input ranges

auto str = "aaa";
auto spec = singleSpec("%s");
writeln(str.unformatValue!char(spec)); // 'a'

// Using a numerical format spec reads a Unicode value from a string
str = "65";
spec = singleSpec("%d");
writeln(str.unformatValue!char(spec)); // 'A'

str = "41";
spec = singleSpec("%x");
writeln(str.unformatValue!char(spec)); // 'A'

str = "10003";
spec = singleSpec("%d");
writeln(str.unformatValue!dchar(spec)); // '✓'

Example

Arrays and static arrays

string str = "aaa";
auto spec = singleSpec("%s");
writeln(str.unformatValue!(dchar[])(spec)); // "aaa"d

str = "aaa";
spec = singleSpec("%s");
dchar[3] ret = ['a', 'a', 'a'];
writeln(str.unformatValue!(dchar[3])(spec)); // ret

str = "[1, 2, 3, 4]";
spec = singleSpec("%s");
writeln(str.unformatValue!(int[])(spec)); // [1, 2, 3, 4]

str = "[1, 2, 3, 4]";
spec = singleSpec("%s");
int[4] ret2 = [1, 2, 3, 4];
writeln(str.unformatValue!(int[4])(spec)); // ret2

Example

Associative arrays

auto str = `["one": 1, "two": 2]`;
auto spec = singleSpec("%s");
writeln(str.unformatValue!(int[string])(spec)); // ["one":1, "two":2]

Authors

Walter Bright, Andrei Alexandrescu, and Kenji Hara

License

Boost License 1.0.