cpp/utility/format/formatter

The enabled specializations of define formatting rules for a given type. Enabled specializations meet the requirements, and, unless otherwise specified, also meet the  requirements.

For all types and  for which no specialization  is enabled, that specialization is a complete type and is disabled.

Disabled specializations do not meet the requirements, and the following are all :
 * std
 * std
 * std
 * std
 * std.

Basic standard specializations
In the following list, is either  or,  is any cv-unqualified arithmetic type other than , , , , or :

Formatters for other pointers and pointers to members are disabled.

Specializations such as and  that would require encoding conversions are disabled.

Standard format specification
For basic types and string types, the format specification is based on the format specification in Python.

The syntax of format specifications is:

The, and  options are only valid when an integer or floating-point presentation type is used.

In most of the cases the syntax is similar to the old -formatting, with the addition of the and with  used instead of. For example, can be translated to.

Fill and align
is an optional fill character (which can be any character other than {{ttb|{}} or {{ttb|} }}), followed by one of the align options, ,.

If no fill character is specified, it defaults to the space character. For a format specification in a Unicode encoding, the fill character must correspond to a single Unicode scalar value.

The meaning of align options is as follows:


 * : Forces the formatted argument to be aligned to the start of the available space by inserting $n$ fill characters after the formatted argument. This is the default when a non-integer non-floating-point presentation type is used.
 * : Forces the formatted argument to be aligned to the end of the available space by inserting $n$ fill characters before the formatted argument. This is the default when an integer or floating-point presentation type is used.
 * : Forces the formatted argument to be centered within the available space by inserting ⌊$$⌋ characters before and ⌈$$⌉ characters after the formatted argument.

In each case, $n$ is the difference of the minimum field width and the estimated width of the formatted argument, or 0 if the difference is less than 0.

Sign, #, and 0
The option can be one of following:
 * : Indicates that a sign should be used for both non-negative and negative numbers. The sign is inserted before the output value for non-negative numbers.
 * : Indicates that a sign should be used for negative numbers only (this is the default behavior).
 * space: Indicates that a leading space should be used for non-negative numbers, and a minus sign for negative numbers.

Negative zero is treated as a negative number.

The option applies to floating-point infinity and NaN.

The option causes the alternate form to be used for the conversion.
 * For integral types, when binary, octal, or hexadecimal presentation type is used, the alternate form inserts the prefix (,, or ) into the output value after the sign character (possibly space) if there is one, or add it before the output value otherwise.
 * For floating-point types, the alternate form causes the result of the conversion of finite values to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for and  conversions, trailing zeros are not removed from the result.

The option pads the field with leading zeros (following any indication of sign or base) to the field width, except when applied to an infinity or NaN. If the character and an align option both appear, the  character is ignored.

Width and precision
is either a positive decimal number, or a nested replacement field ( or {{ttb|{}}n}). If present, it specifies the minimum field width.

is a dot followed by either a non-negative decimal number or a nested replacement field. This field indicates the precision or maximum field size. It can only be used with floating-point and string types.
 * For floating-point types, this field specifies the formatting precision.
 * For string types, it provides an upper bound for the estimated width (see below) of the prefix of the string to be copied to the output. For a string in a Unicode encoding, the text to be copied to the output is the longest prefix of whole extended grapheme clusters whose estimated width is no greater than the precision.

If a nested replacement field is used for or, and the corresponding argument is not of , or is negative, an exception of type std is thrown.

For string types, the width is defined as the estimated number of column positions appropriate for displaying it in a terminal.

For the purpose of width computation, a string is assumed to be in an implementation-defined encoding. The method of width computation is unspecified, but for a string in a Unicode encoding, implementation should estimate the width of the string as the sum of estimated widths of the first code points in its extended grapheme clusters. The estimated width is 2 for the following code points, and is 1 otherwise:


 * Any code point whose Unicode property has value Fullwidth  or Wide
 * U+4DC0 - U+4DFF (Yijing Hexagram Symbols)
 * U+1F300 – U+1F5FF (Miscellaneous Symbols and Pictographs)
 * U+1F900 – U+1F9FF (Supplemental Symbols and Pictographs)

L (locale-specific formatting)
The option causes the locale-specific form to be used. This option is only valid for arithmetic types.


 * For integral types, the locale-specific form inserts the appropriate digit group separator characters according to the context's locale.
 * For floating-point types, the locale-specific form inserts the appropriate digit group and radix separator characters according to the context's locale.
 * For the textual representation of, the locale-specific form uses the appropriate string as if obtained with std or std.

Type
The option determines how the data should be presented.

The available string presentation types are:
 * none, : Copies the string to the output.

The available integer presentation types for integral types other than, , and are:
 * : Binary format. Produces the output as if by calling . The base prefix is.
 * : same as, except that the base prefix is.
 * : Copies the character to the output, where  is the character type of the format string. Throws std if value is not in the range of representable values for.
 * : Decimal format. Produces the output as if by calling.
 * : Octal format. Produces the output as if by calling . The base prefix is if the corresponding argument value is non-zero and is empty otherwise.
 * : Hex format. Produces the output as if by calling . The base prefix is.
 * : same as, except that it uses uppercase letters for digits above 9 and the base prefix is.
 * none: same as.

The available and  presentation types are:
 * none, : Copies the character to the output.
 * ,, , , , : Uses integer presentation types.

The available presentation types are:
 * none, : Copies textual representation ( or, or the locale-specific form) to the output.
 * ,, , , , : Uses integer presentation types with the value.

The available floating-point presentation types are:
 * : If precision is specified, produces the output as if by calling where  is the specified precision; otherwise, the output is produced as if by calling.
 * : same as, except that it uses uppercase letters for digits above 9 and uses to indicate the exponent.
 * : Produces the output as if by calling where  is the specified precision, or 6 if precision is not specified.
 * : same as, except that it uses to indicate the exponent.
 * , : Produces the output as if by calling where  is the specified precision, or 6 if precision is not specified.
 * : Produces the output as if by calling where  is the specified precision, or 6 if precision is not specified.
 * : same as, except that it uses to indicate the exponent.
 * none: If precision is specified, produces the output as if by calling where  is the specified precision; otherwise, the output is produced as if by calling.

For lower-case presentation types, infinity and NaN are formatted as and, respectively. For upper-case presentation types, infinity and NaN are formatted as and, respectively.

The available pointer presentation types (also used for std) are:
 * none, : If std is defined, produces the output as if by calling with the prefix  added to the output; otherwise, the output is implementation-defined.

Formatting escaped characters and strings
A character or string can be formatted as escaped to make it more suitable for debugging or for logging.

Escaping is done as follows:


 * For each well-formed code unit sequence that encodes a character C:
 * If C is one of the characters in the following table, the corresponding escape sequence is used.


 * Otherwise, if C is not the space character (byte 0x20 in ASCII encoding), and either
 * the associated character encoding is a Unicode encoding and
 * C corresponds to a Unicode scalar value whose Unicode property has a value in the groups   or , or
 * C is not immediately preceded by a non-escaped character, and C corresponds to a Unicode scalar value which has the Unicode property, or
 * the associated character encoding is not a Unicode encoding and C is one of an implementation-defined set of separator or non-printable characters
 * the escape sequence is, where is the shortest hexadecimal representation of C using lower-case hexadecimal digits.
 * Otherwise, C is copied as is.


 * A code unit sequence that is a shift sequence has unspecified effect on the output and further decoding of the string.
 * Other code units (i.e. those in ill-formed code unit sequences) are each replaced with, where is the shortest hexadecimal representation of the code unit using lower-case hexadecimal digits.

The escaped string representation of a string is constructed by escaping the code unit sequences in the string, as described above, and quoting the result with double quotes.

The escaped representation of a character is constructed by escaping it as described above, and quoting the result with single quotes.