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

T

the type to return

input Range

the _input range to read from

spec FormatSpec!Char

the FormatSpec to use when reading from input

Return Value

Type: T

A value from input of type T

Throws

A FormatException if spec cannot read a type T

Examples

Booleans

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

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

Null values

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

Integrals

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

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

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

Floating point numbers

import std.math : approxEqual;

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

Character input ranges

1 auto str = "aaa";
2 auto spec = singleSpec("%s");
3 assert(str.unformatValue!char(spec) == 'a');
4 
5 // Using a numerical format spec reads a Unicode value from a string
6 str = "65";
7 spec = singleSpec("%d");
8 assert(str.unformatValue!char(spec) == 'A');
9 
10 str = "41";
11 spec = singleSpec("%x");
12 assert(str.unformatValue!char(spec) == 'A');
13 
14 str = "10003";
15 spec = singleSpec("%d");
16 assert(str.unformatValue!dchar(spec) == '✓');

Arrays and static arrays

1 string str = "aaa";
2 auto spec = singleSpec("%s");
3 assert(str.unformatValue!(dchar[])(spec) == "aaa"d);
4 
5 str = "aaa";
6 spec = singleSpec("%s");
7 dchar[3] ret = ['a', 'a', 'a'];
8 assert(str.unformatValue!(dchar[3])(spec) == ret);
9 
10 str = "[1, 2, 3, 4]";
11 spec = singleSpec("%s");
12 assert(str.unformatValue!(int[])(spec) == [1, 2, 3, 4]);
13 
14 str = "[1, 2, 3, 4]";
15 spec = singleSpec("%s");
16 int[4] ret2 = [1, 2, 3, 4];
17 assert(str.unformatValue!(int[4])(spec) == ret2);

Associative arrays

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

See Also

Meta

Suggestion Box / Bug Report