This is a submodule of std.format.
This is a submodule of std.format.
This is a submodule of std.format.
Signals an issue encountered while formatting.
Converts its arguments according to a format string into a string.
Converts its arguments according to a format string into a buffer. The buffer has to be large enough to hold the formatted string.
Simple use:
// Easiest way is to use `%s` everywhere: assert(format("I got %s %s for %s euros.", 30, "eggs", 5.27) == "I got 30 eggs for 5.27 euros."); // Other format characters provide more control: assert(format("I got %b %(%X%) for %f euros.", 30, "eggs", 5.27) == "I got 11110 65676773 for 5.270000 euros.");
Compound specifiers allow formatting arrays and other compound types:
/* The trailing end of the sub-format string following the specifier for each item is interpreted as the array delimiter, and is therefore omitted following the last array item: */ assert(format("My items are %(%s %).", [1,2,3]) == "My items are 1 2 3."); assert(format("My items are %(%s, %).", [1,2,3]) == "My items are 1, 2, 3."); /* The "%|" delimiter specifier may be used to indicate where the delimiter begins, so that the portion of the format string prior to it will be retained in the last array element: */ assert(format("My items are %(-%s-%|, %).", [1,2,3]) == "My items are -1-, -2-, -3-."); /* These compound format specifiers may be nested in the case of a nested array argument: */ auto mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; assert(format("%(%(%d %) - %)", mat), "1 2 3 - 4 5 6 - 7 8 9"); assert(format("[%(%(%d %) - %)]", mat), "[1 2 3 - 4 5 6 - 7 8 9]"); assert(format("[%([%(%d %)]%| - %)]", mat), "[1 2 3] - [4 5 6] - [7 8 9]"); /* Strings and characters are escaped automatically inside compound format specifiers. To avoid this behavior, use "%-(" instead of "%(": */ assert(format("My friends are %s.", ["John", "Nancy"]) == `My friends are ["John", "Nancy"].`); assert(format("My friends are %(%s, %).", ["John", "Nancy"]) == `My friends are "John", "Nancy".`); assert(format("My friends are %-(%s, %).", ["John", "Nancy"]) == `My friends are John, Nancy.`);
Using parameters:
// Flags can be used to influence to outcome: assert(format("%g != %+#g", 3.14, 3.14) == "3.14 != +3.14000"); // Width and precision help to arrange the formatted result: assert(format(">%10.2f<", 1234.56789) == "> 1234.57<"); // Numbers can be grouped: assert(format("%,4d", int.max) == "21,4748,3647"); // It's possible to specify the position of an argument: assert(format("%3$s %1$s", 3, 17, 5) == "5 3");
Providing parameters as arguments:
// Width as argument assert(format(">%*s<", 10, "abc") == "> abc<"); // Precision as argument assert(format(">%.*f<", 5, 123.2) == ">123.20000<"); // Grouping as argument assert(format("%,*d", 1, int.max) == "2,1,4,7,4,8,3,6,4,7"); // Grouping separator as argument assert(format("%,3?d", '_', int.max) == "2_147_483_647"); // All at once assert(format("%*.*,*?d", 20, 15, 6, '/', int.max) == " 000/002147/483647");
Copyright The D Language Foundation 2000-2021.
This package provides string formatting functionality using printf style format strings.
Limitation: This package does not support localization, but adheres to the rounding mode of the floating point unit, if available.
$(LNAME2 format-strings, Format Strings)
The functions contained in this package use format strings. A format string describes the layout of another string for reading or writing purposes. A format string is composed of normal text interspersed with format specifiers. A format specifier starts with a percentage sign '%', optionally followed by one or more parameters and ends with a format indicator. A format indicator may be a simple format character or a compound indicator.
Format strings are composed according to the following grammar:
Note: FormatCharacter is unspecified. It can be any character that has no other purpose in this grammar, but it is recommended to assign (lower- and uppercase) letters.
Note: The Parameters of a CompoundIndicator are currently limited to a '-' flag.
$(LNAME2 format-indicator, Format Indicator)
The format indicator can either be a single character or an expression surrounded by %\() and %\. It specifies the basic manner in which a value will be formatted and is the minimum requirement to format a value.
The following characters can be used as format characters:
The compound indicator can be used to describe compound types like arrays or structs in more detail. A compound type is enclosed within '%\(') and '%\'. The enclosed sub-format string is applied to individual elements. The trailing portion of the sub-format string following the specifier for the element is interpreted as the delimiter, and is therefore omitted following the last 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 element.
The format string inside of the compound indicator should contain exactly one format specifier (two in case of associative arrays), which specifies the formatting mode of the elements of the compound type. This format specifier can be a compound indicator itself.
Note: Inside a compound indicator, strings and characters are escaped automatically. To avoid this behavior, use "%-$(LPAREN)" instead of "%$(LPAREN)".
$(LNAME2 flags, Flags)
There are several flags that affect the outcome of the formatting.
There are two exceptions where the '-' flag has a different meaning: (1) with 'r' it denotes to use little endian and (2) in case of a compound indicator it means that no special handling of the members is applied.
In case of 'r', a big endian format is used.
$(LNAME2 width-precision-separator, Width, Precision and Separator)
The width parameter specifies the minimum width of the result.
The meaning of precision depends on the format indicator. For integers it denotes the minimum number of digits printed, for real numbers it denotes the number of fractional digits and for strings and compound types it denotes the maximum number of elements that are included in the output.
A separator is used for formatting numbers. If it is specified, the output is divided into chunks of three digits, separated by a ','. The number of digits in a chunk can be given explicitly by providing a number or a '*' after the ','.
In all three cases the number of digits can be replaced by a '*'. In this scenario, the next argument is used as the number of digits. If the argument is a negative number, the precision and separator parameters are considered unspecified. For width, the absolute value is used and the '-' flag is set.
The separator can also be followed by a '?'. In that case, an additional argument is used to specify the symbol that should be used to separate the chunks.
$(LNAME2 position, Position)
By default, the arguments are processed in the provided order. With the position parameter it is possible to address arguments directly. It is also possible to denote a series of arguments with two numbers separated by ':', that are all processed in the same way. The second number can be omitted. In that case the series ends with the last argument.
It's also possible to use positional arguments for width, precision and separator by adding a number and a '$' after the '*'.
$(LNAME2 types, Types)
This section describes the result of combining types with format characters. It is organized in 2 subsections: a list of general information regarding the formatting of types in the presence of format characters and a table that contains details for every available combination of type and format character.
When formatting types, the following rules apply:
This table contains descriptions for every possible combination of type and format character:
Please note, that 'o' and 'x' with '#' flag might produce unexpected results due to special handling of the value 0.
In case of 'o' and 'x', the '#' flag denotes that the number must be preceded by 0 and 0x, with the exception of the value 0, where this does not apply. For 'b' and 'u' the '#' flag has no effect.
Default precision is large enough to add all digits of the integral value.
In case of 'a' and 'A', the integral digit can be any hexadecimal digit.
When there are no fractional digits and the '#' flag is not present, the dot is omitted.
When there are no fractional digits and the '#' flag is not present, the dot is omitted.
Please note: the difference between 'f' and 'F' is only visible for NaN and Infinity.
In both cases precision denotes the count of all digits, including the integral digits. Trailing zeros (including a trailing dot) are removed.
If '#' flag is present, trailing zeros are not removed.
When there are no fractional digits and the '#' flag is not present, the dot is omitted.
Inside of a compound indicator 's' is treated differently: The character is surrounded by single quotes and non printable characters are escaped. This can be avoided by preceding the compound indicator with a '-' flag (e.g. "%-$(LPAREN)%s%$(RPAREN)").
Inside of a compound indicator the string is surrounded by double quotes and non printable characters are escaped. This can be avoided by preceding the compound indicator with a '-' flag (e.g. "%-$(LPAREN)%s%$(RPAREN)").
Please note: The implementation is currently buggy and its use is discouraged.