Module std.format.write
This is a submodule of std.
It provides two functions for writing formatted output: formatValue and formattedWrite. The former writes a single
value. The latter writes several values at once, interspersed with
unformatted text.
The following combinations of format characters and types are
available
| s | c | d, u, b, o | x, X | e, E, f, F, g, G, a, A | r | compound | |
|---|---|---|---|---|---|---|---|
| bool | yes | yes | yes | yes | |||
| null | yes | ||||||
| integer | yes | yes | yes | yes | yes | ||
| floating point | yes | yes | yes | ||||
| character | yes | yes | yes | yes | yes | ||
| string | yes | yes | yes | ||||
| array | yes | yes | yes | ||||
| associative array | yes | yes | |||||
| pointer | yes | yes | |||||
| SIMD vectors | yes | yes | yes | ||||
| delegates | yes | yes | yes | 
Enums can be used with all format characters of the base type.
Structs, Unions, Classes, and Interfaces
Unions, Classes, and InterfacesAggregate types can define various toString functions. If this
function takes a FormatSpec or a format string as argument, the function decides
which format characters are accepted. If no toString is defined and
the aggregate is an input range, it is treated like a range, that is 's', 'r' and a compound specifier are accepted. In all other cases
aggregate types only accept 's'.
toString should have one of the following signatures:
void toString(Writer, Char)(ref Writer w, const ref FormatSpec!Char fmt)
void toString(Writer)(ref Writer w)
string toString();Where Writer is an output range which accepts characters (of type
Char in the first version). The template type does not have
to be called Writer.
Sometimes it's not possible to use a template, for example when
toString overrides Object. In this case, the following
(slower and less flexible) functions can be used:
void toString(void delegate(const(char)[]) sink, const ref FormatSpec!char fmt);
void toString(void delegate(const(char)[]) sink, string fmt);
void toString(void delegate(const(char)[]) sink);When several of the above toString versions are available, the
versions with Writer take precedence over the versions with a
sink. string toString() has the lowest priority.
If none of the above mentioned toString versions are available, the
aggregates will be formatted by other means, in the following
order
If an aggregate is an input range, it is formatted like an input range.
If an aggregate is a builtin type (using alias this), it is formatted
like the builtin type.
If all else fails, structs are formatted like Type(field1, field2, ...),
classes and interfaces are formatted with their fully qualified name
and unions with their base name.
Example
bools are formatted as "true" or "false" with %s and like the
bytes 1 and 0 with all other format characters.
import stdExample
The null literal is formatted as "null".
import stdExample
Integrals are formatted in (signed) every day notation with %s and
%d and as an (unsigned) image of the underlying bit representation
with %b (binary), %u (decimal), %o (octal), and %x (hexadecimal).
import stdExample
Floating-point values are formatted in natural notation with %f, in
scientific notation with %e, in short notation with %g, and in
hexadecimal scientific notation with %a. If a rounding mode is
available, they are rounded according to this rounding mode, otherwise
they are rounded to the nearest value, ties to even.
import stdExample
Individual characters (char, wchar, or dchar) are formatted as
Unicode characters with %s and %c and as integers (ubyte,
ushort, uint) with all other format characters. With
compound specifiers characters are
treated differently.
import stdExample
Strings are formatted as a sequence of characters with %s.
Non-printable characters are not escaped. With a compound specifier
the string is treated like a range of characters. With compound specifiers strings are treated differently.
import stdExample
Static arrays are formatted as dynamic arrays.
import stdExample
Dynamic arrays are formatted as input ranges.
import stdExample
Associative arrays are formatted by using ':' and ", " as
separators, enclosed by '[' and ']' when used with %s. It's
also possible to use a compound specifier for better control.
Please note, that the order of the elements is not defined, therefore the result of this function might differ.
import stdExample
enums are formatted as their name when used with %s and like
their base value else.
import stdExample
structs, unions, classes and interfaces can be formatted in
several different ways. The following example highlights struct
formatting, however, it applies to other aggregates as well.
import stdExample
Pointers are formatted as hexadecimal integers.
import stdExample
SIMD vectors are formatted as arrays.
import coreFunctions
| Name | Description | 
|---|---|
| 
									formattedWrite(w, fmt, args)
								 | Converts its arguments according to a format string and writes the result to an output range. | 
| 
									formatValue(w, val, f)
								 | Formats a value of any type according to a format specifier and writes the result to an output range. | 
Authors
Walter Bright, Andrei Alexandrescu, and Kenji Hara