Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.111.0

overview

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 usinga local clone.

std.format.read

This is a submodule ofstd.format.
It provides two functions for reading formatted input:unformatValue andformattedRead. The former reads a singlevalue. The latter reads several values at once and matches thecharacters found between format specifiers.
Parameters are ignored, except for the ones consisting of a single'*'. SeeformattedRead for more information.
A space outside of a format specifier has a special meaning: itmatches any sequence of whitespace characters, not just a singlespace.
The following combinations of format characters and types areavailable:
scd, u, b, o, x, Xe, E, f, g, Grcompound
boolyes — yes —  —  — 
nullyes —  —  —  —  — 
integeryes — yes — yes — 
floating pointyes —  — yesyes — 
characteryesyesyes —  —  — 
stringyes —  —  —  — yes
arrayyes —  —  —  — yes
associative arrayyes —  —  —  — yes
Below are highlighted examples on how these combinations are usedwithunformatValue, however, they apply forformattedRead also
License:
Boost License 1.0.
Authors:
Walter Bright,Andrei Alexandrescu, and Kenji Hara

Sourcestd/format/read.d

Examples:
Booleans
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
Examples:
Null values
import std.format.spec : singleSpec;auto str ="null";auto spec = singleSpec("%s");writeln(str.unformatValue!(typeof(null))(spec));// null
Examples:
Integrals
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 */ );
Examples:
Floating point numbers
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 */ );
Examples:
Characters
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));// '✓'
Examples:
Arrays
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']
Examples:
Associative arrays
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]
uintformattedRead(Range, Char, Args...)(auto ref Ranger, const(Char)[]fmt, auto ref Argsargs);

uintformattedRead(alias fmt, Range, Args...)(auto ref Ranger, auto ref Argsargs)
if (!isType!fmt && isSomeString!(typeof(fmt)));
Reads an input range according to a format string and stores the readvalues into its arguments.
Format specifiers with format character'd','u' and'c' can take a'*' parameter for skipping values.
The second version offormattedRead takes the format string astemplate argument. In this case, it is checked for consistency atcompile-time.
Parameters:
Rangeraninput range, where the formatted input is read from
const(Char)[]fmtaformat string
Argsargsa variadic list of arguments where the read values are stored
Rangethe type of the input ranger
Charthe character type used forfmt
Argsa variadic list of types of the arguments
Returns:
The number of variables filled. If the input ranger ends early, this number will be less than the number of variables provided.
Throws:
AFormatException if reading did not succeed.

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.

Examples:
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
Examples:
The format string can be checked at compile-time:
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
Examples:
Skipping values
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);
templateformattedRead(Args...) if (Args.length && allSatisfy!(isType, Args))

templateformattedRead(alias fmt, Args...) if (!isType!fmt && isSomeString!(typeof(fmt)) && Args.length && allSatisfy!(isType, Args))
Reads an input range according to a format string and returns a tuple of Argswith the read values.
Format specifiers with format character'd','u' and'c' can take a'*' parameter for skipping values.
The second version offormattedRead takes the format string astemplate argument. In this case, it is checked for consistency atcompile-time.
Parameters:
Argsa 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");writeln(complete);// tuple("hello", 34.5, 124)// reading ends earlyassertThrown!FormatException("hello!34.5:".formattedRead!(string,double,int)("%s!%s:%s"));
Examples:
Skipping values
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)
Examples:
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);writeln(result);// expectedassertThrown!FormatException("hello!34.5:".formattedRead!("%s!%s:%s", string,double,int));
Examples:
Compile-time consistency check
import std.format : FormatException;import std.typecons : tuple;staticassert(!__traits(compiles,"orange: (12%) 15.25".formattedRead!("%s: (%*d%%) %f", string,double)));
Tuple!ArgsformattedRead(Range, Char)(auto ref Ranger, const(Char)[]fmt);
Parameters:
Rangeraninput range, where the formatted input is read from
const(Char)[]fmtaformat string
Rangethe type of the input ranger
Charthe character type used forfmt
Returns:
A Tuple!Args with the elements filled.
Throws:
AFormatException if reading did not succeed.
TunformatValue(T, Range, Char)(ref Rangeinput, ref scope const FormatSpec!Charspec);
Reads a value from the given input range and converts it according to aformat specifier.
Parameters:
Rangeinputtheinput range, to read from
FormatSpec!Charspecaformat string
Ttype to return
Rangethe type of the input rangeinput
Charthe character type used forspec
Returns:
A value frominput of typeT.
Throws:
AFormatException if reading did not succeed.
See Also:
Examples:
import std.format.spec : singleSpec;string s ="42";autospec = singleSpec("%s");writeln(unformatValue!int(s,spec));// 42
Copyright © 1999-2025 by theD Language Foundation | Page generated byDdoc on Fri Oct 10 22:10:42 2025

[8]ページ先頭

©2009-2025 Movatter.jp