This module implements the formatting functionality for strings and I/O. It's comparable to C99's vsprintf() and uses a similar format encoding scheme.
Signals a mismatch between a format and its corresponding argument.
Interprets variadic argument list args, formats them according to fmt, and sends the resulting characters to w. The encoding of the output is the same as Char. The type Writer must satisfy std.range.isOutputRange!(Writer, Char).
The variadic arguments are normally consumed in order. POSIX-style
positional parameter syntax is also supported. Each argument is
formatted into a sequence of chars according to the format
specification, and the characters are passed to w. As many
arguments as specified in the format string are consumed and
formatted. If there are fewer arguments than format specifiers, a
FormatException is thrown. If there are more remaining arguments
than needed by the format specification, they are ignored but only
if at least one argument was formatted.
The format string supports the formatting of array and nested array elements
via the grouping format specifiers %( and %). Each
matching pair of %( and %) corresponds with a single array
argument. The enclosed sub-format string is applied to individual array
elements. The trailing portion of the sub-format string following the
conversion specifier for the array element is interpreted as the array
delimiter, and is therefore omitted following the last array element. The
%| specifier may be used to explicitly indicate the start of the
delimiter, so that the preceding portion of the string will be included
following the last array element. (See below for explicit examples.)
Writer w | Output is sent to this writer. Typical output writers include std.array.Appender!string and std.stdio.LockingTextWriter. |
Char[] fmt | Format string. |
A args | Variadic argument list. |
FormatString: FormatStringItem* FormatStringItem: '%%' '%' Position Flags Width Precision FormatChar '%(' FormatString '%)' OtherCharacterExceptPercent Position: empty Integer '$' Flags: empty '-' Flags '+' Flags '#' Flags '0' Flags ' ' Flags Width: empty Integer '*' Precision: empty '.' '.' Integer '.*' Integer: Digit Digit Integer Digit: '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' FormatChar: 's'|'c'|'b'|'d'|'o'|'x'|'X'|'e'|'E'|'f'|'F'|'g'|'G'|'a'|'A'
Flag | Types affected | Semantics |
---|---|---|
'-' | numeric | Left justify the result in the field. It overrides any 0 flag. |
'+' | numeric | Prefix positive numbers in a signed conversion with a +. It overrides any space flag. |
'#' | integral ('o') | Add to precision as necessary so that the first digit of the octal formatting is a '0', even if both the argument and the Precision are zero. |
'#' | integral ('x', 'X') | If non-zero, prefix result with 0x (0X). |
'#' | floating | Always insert the decimal point and print trailing zeros. |
'0' | numeric | Use leading zeros to pad rather than spaces (except for the floating point values nan and infinity). Ignore if there's a Precision. |
' ' | numeric | Prefix positive numbers in a signed conversion with a space. |
import std.c.stdio; import std.format; void main() { auto writer = appender!string(); formattedWrite(writer, "%s is the ultimate %s.", 42, "answer"); assert(writer.data == "42 is the ultimate answer."); // Clear the writer writer = appender!string(); formattedWrite(writer, "Date: %2$s %1$s", "October", 5); assert(writer.data == "Date: 5 October"); }
import std.stdio; void main() { writefln("My items are %(%s %).", [1,2,3]); writefln("My items are %(%s, %).", [1,2,3]); }The output is:
My items are 1 2 3. My items are 1, 2, 3.
import std.stdio; void main() { writefln("My items are %(-%s-%|, %).", [1,2,3]); }which gives the output:
My items are -1-, -2-, -3-.
import std.stdio; void main() { auto mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; writefln("%(%(%d %)\n%)", mat); writeln(); writefln("[%(%(%d %)\n %)]", mat); writeln(); writefln("[%([%(%d %)]%|\n %)]", mat); writeln(); }The output is:
1 2 3 4 5 6 7 8 9
[1 2 3 4 5 6 7 8 9]
[[1 2 3] [4 5 6] [7 8 9]]
import std.stdio; void main() { writefln("My friends are %s.", ["John", "Nancy"]); writefln("My friends are %(%s, %).", ["John", "Nancy"]); writefln("My friends are %-(%s, %).", ["John", "Nancy"]); }which gives the output:
My friends are ["John", "Nancy"]. My friends are "John", "Nancy". My friends are John, Nancy.
Reads characters from input range r, converts them according to fmt, and writes them to args.
string s = "hello!124:34.5"; string a; int b; double c; formattedRead(s, "%s!%s:%s", &a, &b, &c); assert(a == "hello" && b == 124 && c == 34.5);
A General handler for printf style format specifiers. Used for building more specific formatting functions.
auto a = appender!(string)(); auto fmt = "Number: %2.4e\nString: %s"; auto f = FormatSpec!char(fmt); f.writeUpToNextSpec(a); assert(a.data == "Number: "); assert(f.trailing == "\nString: %s"); assert(f.spec == 'e'); assert(f.width == 2); assert(f.precision == 4); f.writeUpToNextSpec(a); assert(a.data == "Number: \nString: "); assert(f.trailing == ""); assert(f.spec == 's');
Minimum width, default 0.
Precision. Its semantics depends on the argument type. For floating point numbers, precision dictates the number of decimals printed.
Special value for width and precision. DYNAMIC width or precision means that they were specified with '*' in the format string and are passed at runtime through the varargs.
Special value for precision, meaning the format specifier contained no explicit precision.
The actual format specifier, 's' by default.
Index of the argument for positional parameters, from 1 to ubyte.max. (0 means not used).
Index of the last argument for positional parameter range, from 1 to ubyte.max. (0 means not used).
In case of a compound format specifier starting with "%(" and ending with "%)", nested contains the string contained within the two separators.
In case of a compound format specifier, sep contains the string positioning after "%|".
trailing contains the rest of the format string.
Construct a new FormatSpec using the format string fmt, no processing is done until needed.
Helper function that returns a FormatSpec for a single specifier given in fmt
Returns a FormatSpec with the specifier parsed.
Enforces giving only one specifier to the function.
bools are formatted as "true" or "false" with %s and as "1" or "0" with integral-specific format specs.
null literal is formatted as "null".
Integrals are formatted like printf does.
Floating-point values are formatted like printf does.
Individual characters (char, wchar, or dchar) are formatted as Unicode characters with %s and as integers with integral-specific format specs.
Strings are formatted like printf does.
Static-size arrays are formatted as dynamic arrays.
Dynamic arrays are formatted as input ranges.
Associative arrays are formatted by using ':' and ", " as separators, and enclosed by '[' and ']'.
Aggregates (struct, union, class, and interface) are basically formatted by calling toString. toString should have one of the following signatures:
const void toString(scope void delegate(const(char)[]) sink, FormatSpec fmt); const void toString(scope void delegate(const(char)[]) sink, string fmt); const void toString(scope void delegate(const(char)[]) sink); const string toString();
enum is formatted like its base value.
Pointers are formatted as hex integers.
Delegates are formatted by 'Attributes ReturnType delegate(Parameters)'
Reads a boolean value and returns it.
Reads null literal and returns it.
Reads an integral value and returns it.
Reads a floating-point value and returns it.
Reads one character and returns it.
Reads a string and returns it.
Reads an array (except for string types) and returns it.
Reads an associative array and returns it.