| s | c | d, u, b, o, x, X | e, E, f, g, G | r | compound | |
|---|---|---|---|---|---|---|
| bool | yes | yes | ||||
| null | yes | |||||
| integer | yes | yes | yes | |||
| floating point | yes | yes | yes | |||
| character | yes | yes | yes | |||
| string | yes | yes | ||||
| array | yes | yes | ||||
| associative array | yes | yes |
Sourcestd/format/read.d
import std.format.spec : singleSpec;auto str ="false";auto spec = singleSpec("%s");writeln(str.unformatValue!bool(spec));// falsestr ="1";spec = singleSpec("%d");writeln(str.unformatValue!bool(spec));// true
import std.format.spec : singleSpec;auto str ="null";auto spec = singleSpec("%s");writeln(str.unformatValue!(typeof(null))(spec));// null
import std.format.spec : singleSpec;// signed decimal valuesauto str ="123";auto spec = singleSpec("%s");writeln(str.unformatValue!int(spec));// 123// hexadecimal valuesstr ="ABC";spec = singleSpec("%X");writeln(str.unformatValue!int(spec));// 2748// octal valuesstr ="11610";spec = singleSpec("%o");writeln(str.unformatValue!int(spec));// 5000// raw read, depends on endianessstr ="\x75\x01";spec = singleSpec("%r");auto result = str.unformatValue!short(spec);assert(result == 373/* little endian */ || result == 29953/* big endian */ );
import std.format.spec : singleSpec;import std.math.operations : isClose;// natural notationauto str ="123.456";auto spec = singleSpec("%s");assert(str.unformatValue!double(spec).isClose(123.456));// scientific notationstr ="1e17";spec = singleSpec("%e");assert(str.unformatValue!double(spec).isClose(1e17));// raw read, depends on endianessstr ="\x40\x00\x00\xBF";spec = singleSpec("%r");auto result = str.unformatValue!float(spec);assert(isClose(result, -0.5)/* little endian */ || isClose(result, 2.0)/* big endian */ );
import std.format.spec : singleSpec;// only the first character is readauto str ="abc";auto spec = singleSpec("%s");writeln(str.unformatValue!char(spec));// 'a'// using a numerical format character treats the read number as unicode code pointstr ="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));// '✓'
import std.format.spec : singleSpec;// string valuestring str ="aaa";auto spec = singleSpec("%s");writeln(str.unformatValue!(dchar[])(spec));// "aaa"d// fixed size array with charactersstr ="aaa";spec = singleSpec("%s");dchar[3] ret = ['a', 'a', 'a'];writeln(str.unformatValue!(dchar[3])(spec));// ret// dynamic arraystr ="[1, 2, 3, 4]";spec = singleSpec("%s");writeln(str.unformatValue!(int[])(spec));// [1, 2, 3, 4]// fixed size array with integersstr ="[1, 2, 3, 4]";spec = singleSpec("%s");int[4] ret2 = [1, 2, 3, 4];writeln(str.unformatValue!(int[4])(spec));// ret2// compound specifiers can be used for more controlstr ="1,2,3";spec = singleSpec("%(%s,%)");writeln(str.unformatValue!(int[])(spec));// [1, 2, 3]str ="cool";spec = singleSpec("%(%c%)");writeln(str.unformatValue!(char[])(spec));// ['c', 'o', 'o', 'l']
import std.format.spec : singleSpec;// as single valueauto str =`["one": 1, "two": 2]`;auto spec = singleSpec("%s");writeln(str.unformatValue!(int[string])(spec));// ["one":1, "two":2]// with compound specifier for more controlstr ="1/1, 2/4, 3/9";spec = singleSpec("%(%d/%d%|, %)");writeln(str.unformatValue!(int[int])(spec));// [1:1, 2:4, 3:9]
formattedRead(Range, Char, Args...)(auto ref Ranger, const(Char)[]fmt, auto ref Argsargs);formattedRead(alias fmt, Range, Args...)(auto ref Ranger, auto ref Argsargs)formattedRead takes the format string astemplate argument. In this case, it is checked for consistency atcompile-time.Ranger | aninput range, where the formatted input is read from |
const(Char)[]fmt | aformat string |
Argsargs | a variadic list of arguments where the read values are stored |
| Range | the type of the input ranger |
| Char | the character type used forfmt |
| Args | a variadic list of types of the arguments |
r ends early, this number will be less than the number of variables provided.NoteFor backward compatibility the argumentsargs can be given as pointers to that variable, but it is not recommended to do so, because this option might be removed in the future.
string object;char cmp;int value;writeln(formattedRead("angle < 36","%s %c %d", object, cmp, value));// 3writeln(object);// "angle"writeln(cmp);// '<'writeln(value);// 36// reading may end early:writeln(formattedRead("length >","%s %c %d", object, cmp, value));// 2writeln(object);// "length"writeln(cmp);// '>'// value is not changed:writeln(value);// 36
string a;int b;double c;writeln("hello!124:34.5".formattedRead!"%s!%s:%s"(a, b, c));// 3writeln(a);// "hello"writeln(b);// 124writeln(c);// 34.5
string item;double amount;writeln("orange: (12%) 15.25".formattedRead("%s: (%*d%%) %f", item, amount));// 2writeln(item);// "orange"writeln(amount);// 15.25// can also be used with tuplesimport std.typecons : Tuple;Tuple!(int,float) t;char[] line ="1 7643 2.125".dup;formattedRead(line,"%s %*u %s", t);assert(t[0] == 1 && t[1] == 2.125);
formattedRead(Args...) if (Args.length && allSatisfy!(isType, Args))formattedRead(alias fmt, Args...) if (!isType!fmt && isSomeString!(typeof(fmt)) && Args.length && allSatisfy!(isType, Args))formattedRead takes the format string astemplate argument. In this case, it is checked for consistency atcompile-time.| Args | a variadic list of types of the arguments |
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");writeln(complete);// tuple("hello", 34.5, 124)// reading ends earlyassertThrown!FormatException("hello!34.5:".formattedRead!(string,double,int)("%s!%s:%s"));
import std.format : FormatException;import std.typecons : tuple;auto result ="orange: (12%) 15.25".formattedRead!(string,double)("%s: (%*d%%) %f");writeln(result);// tuple("orange", 15.25)
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);writeln(result);// expectedassertThrown!FormatException("hello!34.5:".formattedRead!("%s!%s:%s", string,double,int));
import std.format : FormatException;import std.typecons : tuple;staticassert(!__traits(compiles,"orange: (12%) 15.25".formattedRead!("%s: (%*d%%) %f", string,double)));
formattedRead(Range, Char)(auto ref Ranger, const(Char)[]fmt);Ranger | aninput range, where the formatted input is read from |
const(Char)[]fmt | aformat string |
| Range | the type of the input ranger |
| Char | the character type used forfmt |
unformatValue(T, Range, Char)(ref Rangeinput, ref scope const FormatSpec!Charspec);Rangeinput | theinput range, to read from |
FormatSpec!Charspec | aformat string |
| T | type to return |
| Range | the type of the input rangeinput |
| Char | the character type used forspec |
input of typeT.import std.format.spec : singleSpec;string s ="42";autospec = singleSpec("%s");writeln(unformatValue!int(s,spec));// 42