Document Number: | D2572R0 Draft |
---|---|
Date: | 2022-05-24 |
Audience: | SG16 |
Reply-to: | Tom Honermann <tom@honermann.net> |
Presented is a proposed resolution for the following LWG issues concerning the specification of fill characters in std::format().
This proposal follows prior discussion as recorded in the:
The current wording in [format.string.std]p1 restricts fill characters to "any character other than { or }". Depending on how "character" is interpreted, this may permit characters with a negative display width, characters with no display width, characters with a display width greater than one, chraracters with a varying display width, characters with an actual diplay width that differs from their estimated width, combining characters (with or without a non-combining lead character), decomposed characters, characters with right-to-left directionality, control characters, formatting characters, and emoji. The following table presents some examples of such characters.
Glyph | Estimated width | Code point(s) | Character name |
---|---|---|---|
>< | 1 | U+0007 | BELL |
>< | 1 | U+0008 | BACKSPACE |
>< | 1 | U+007F | DELETE |
> < | 1 | U+0009 | CHARACTER TABULATION |
>< | 1 | U+200B | ZERO WIDTH SPACE |
>́< | 1 | U+0301 | COMBINING ACCUTE ACCENT |
>é< | 1 | U+00E9 | LATIN SMALL LETTER E WITH ACUTE |
>é< | 1 | U+0065 U+0301 |
LATIN SMALL LETTER E COMBINING ACCUTE ACCENT |
>è́̂̃̄< | 1 | U+0065 U+0300 U+0301 U+0302 U+0303 U+0304 |
LATIN SMALL LETTER E COMBINING GRAVE ACCENT COMBINING ACUTE ACCENT COMBINING CIRCUMFLEX ACCENT COMBINING TILDE COMBINING MACRON |
>e< | 2 | U+FF45 | FULLWIDTH LATIN SMALL LETTER E |
>ェ< | 2 | U+30A7 | KATAKANA LETTER SMALL E |
>ェ< | 1 | U+FF6A | HALFWIDTH KATAKANA LETTER SMALL E |
> < | 2 | U+3000 | IDIOGRAPHIC SPACE |
>ת< | 1 | U+05EA | HEBREW LETTER TAV (a right-to-left character) |
>🤡< | 2 | U+1F921 | CLOWN FACE |
>﷽< | 1 | U+FDFD |
It is likely that the displayed character differs from the estimated width for at least some cases above; most likely the last case. Unfortunately, there is no specification currently available that governs character display width; actual width may vary based on font selection.
Use of a fill character with a display width other than one potentially prevents a std::format() implementation from properly aligning fields. Consider a format specification for a field of width four and a field argument with an estimated field width of one. The implementation is expected to insert fill characters to consume an estimated field width of three, but that is not possible if the fill character has an estimated field width of two. Portable behavior requires that the standard clarify the intended behavior for such characters.
A std::format() implementation must store or reference a fill character in some way. Fill character allowances may impose dynamic memory management requirements or increase the complexity of parsing standard format specifiers depending on implementation choices. Implementation choices may also cause fill character restrictions to be reflected in the ABI thus making it difficult to relax restrictions later. Portable behavior requires that the standard specify whether fill characters are restricted to those that are encoded as, for example, a single code unit, a single UCS scalar value, a stream-safe extended grapheme cluster [UAX#15], or an extended grapheme cluster of unbounded length.
Fill character allowances pose a performance and overhead tradeoff. Consider the following four options for fill character support.
The first option (any EGC) would require implementations to support EGCs that consist of an unbounded number of code points. This option implies dynamic memory management and would require implementations to identify EGC boundaries in the format string; a requirement that otherwise does not exist at present (implementations are currently required to identify EGC boundaries in formatted field arguments for the purpose of computing the estimated width, but not in the format string itself).
The second option (any stream-safe EGC) would require implementations to support EGCs that consist of up to 32 code points. This option allows an implementation to trade off dynamic memory allocation in favor of larger data structures, but still requires EGC boundary analysis of format strings.
The third option (any single UCS scalar value) avoids dynamic memory requirements and significant increases to sizes of data structures; the fill character could be stored in a single char32_t object.
The fourth option (any single code unit) reduces fill character storage requirements to a single code unit (char or wchar_t), but has the unfortunate side effect of making the permissible set of fill characters dependent on encoding. For example, U+00E9 (LATIN SMALL LETTER E WITH ACUTE) would be rejected in a UTF-8 encoded format string, but would be accepted in a UTF-16 encoded one. Similarly, U+1F921 (CLOWN FACE) would be rejected in a UTF-16 encoded format string, but accepted in a UTF-32 encoded one.
The following behaviors represent possible options for formatting fields when the fill character has an estimated width other than one.
The following table illustrates the above options for std::format(">{:🤡^4}<\n", 'X'). Font selection will determine to what degree the results shown deviate from the reference alignment.
Behavioral choice | Result |
---|---|
(reference alignment) | >-X--< |
Use an estimated width of one | >🤡X🤡🤡< |
Overfill | >🤡X🤡< |
Underfill | >X🤡< |
Pad with a different fill character (space) | > X🤡< |
Undefined, unspecified, or implementation-defined behavior | ??? |
Error (unconditionally or due to inability to align) | N/A |
The following table illustrates existing behavior for several std::format() implementations when the example characters from the introduction are used as the fill character with a directionally neutral field argument of '#' (the directionality affects the behavior of the U+05EA example). The first row illustrates a reference alignment.
Code point(s) | Format string | Clang 15 trunk with libc++ |
Gcc 12.1 with fmt 8.1.1 |
MSVC 19.31 |
---|---|---|---|---|
U+002D HYPHEN-MINUS | ">{:-^4}<" | >-#--< | >-#--< | >-#--< |
U+0007 BELL | ">{:^4}<" | >#< | >#< | >#< |
U+0008 BACKSPACE | ">{:^4}<" | >#< | >#< | >#< |
U+007F DELETE | ">{:^4}<" | >#< | >#< | >#< |
U+0009 CHARACTER TABULATION | ">{: ^4}<" | > # < | > # < | > # < |
U+200B ZERO WIDTH SPACE | ">{:^4}<" | Error1 | >#< | >#< |
U+0301 COMBINING ACCUTE ACCENT | ">{:́^4}<" | Error1 | >́#́́< | >́#́́< |
U+00E9 LATIN SMALL LETTER E WITH ACUTE | ">{:é^4}<" | Error1 | >é#éé< | >é#éé< |
U+0065 LATIN SMALL LETTER E U+0301 COMBINING ACCUTE ACCENT |
">{:é^4}<" | Error1 | Error2 | Error3 |
U+0065 LATIN SMALL LETTER E U+0300 COMBINING GRAVE ACCENT U+0301 COMBINING ACUTE ACCENT U+0302 COMBINING CIRCUMFLEX ACCENT U+0303 COMBINING TILDE U+0304 COMBINING MACRON |
">{:è́̂̃̄^4}<" | Error1 | Error2 | Error3 |
U+FF45 FULLWIDTH LATIN SMALL LETTER E | ">{:e^4}<" | Error1 | >e#ee< | >e#ee< |
U+30A7 KATAKANA LETTER SMALL E | ">{:ェ^4}<" | Error1 | >ェ#ェェ< | >ェ#ェェ< |
U+FF6A HALFWIDTH KATAKANA LETTER SMALL E | ">{:ェ^4}<" | Error1 | >ェ#ェェ< | >ェ#ェェ< |
U+3000 IDIOGRAPHIC SPACE | ">{: ^4}<" | Error1 | > # < | > # < |
U+05EA HEBREW LETTER TAV | ">{:ת^4}<" | Error1 | >ת#תת<4 >תXתת<4 |
>ת#תת<4 >תXתת<4 |
U+1F921 CLOWN FACE | ">{:🤡^4}<" | Error1 | >🤡#🤡🤡< | >🤡#🤡🤡< |
U+FDFD ARABIC LIGATURE BISMILLAH AR-RAHMAN AR-RAHEEM | ">{:﷽^4}<" | Error1 | >﷽#﷽﷽< | >﷽#﷽﷽< |
1) Clang with libc++ restricts fill characters to characters that are encoded as a single code unit; an exception is thrown that, if not caught, aborts the process with the following error message.
2) Gcc with fmt restricts fill characters to characters that are encoded as a single code point. Compilation fails with the following error message.
3) MSVC restricts fill characters to characters that are encoded as a single code point. Compilation is successful, but program execution terminates with an exit code of 3221226505 (0xC0000409: STATUS_STACK_BUFFER_OVERRUN). The buffer overflow has been corrected for the next MSVC release and these cases are now rejected with the following error message.
4) Use of a fill character with right-to-left directionality potentially causes the formatted field to be rendered right to left depending on the formatted field argument. Two examples are provided, one in which the directionally neutral character '#' is used as the formatted field argument and one in which the left-to-right character 'X' is used. U+200E LEFT-TO-RIGHT MARK characters have been inserted by the paper author to negate the right-to-left effect on surrounding text. In practice, the right-to-left directionality may affect how surrounding text from the format string or other format fields are presented.
All surveyed implementations assume an estimated width of 1 for fill characters regardless of the estimated width values specified in [format.string.std]p11.
Standardize the behavior exhibited by gcc with fmt and MSVC:
Programmers may find use cases where it is necessary for the number of inserted fill characters to depend on the estimated width of the fill character. Some of those use cases may warrant support in the standard. If such motivation arises, there are at least two methods by which support could be added.
Motivation may arise in the future to permit the use of an EGC that consists of multiple code points as a fill character. Implementations that store a char32_t or short sequence of code units in their formatter class specializations ([format.formatter.spec]) may be susceptible to ABI breaks. Implementations are encouraged to instead store a view (an iterator pair, start and end index, or start index and length) into the std-format-spec ([format.string.std]p1) string so that code unit sequences of arbitrary length can be referenced.
It appears that MSVC is currently susceptible to such ABI breaks based on the implementation of their _Basic_format_specs class template. Specializations of _Basic_format_specs form the base class of their _Dynamic_format_specs class template for which a specialization is stored in their _Formatter_base class template that forms the base class of their std::formatter specializations.
The author has not researched the ABI break susceptibility of other implementations.
This proposal standardizes the behavior exhibited by both gcc with fmt and MSVC and therefore reflects existing practice. However the ABI mitigations described in the prior section are not known to have been implemented.
Some implementations, libc++ for example, will require changes to allow any single UCS scalar value to be specified as a fill character. This may impose new encoding awareness requirements on format string parsers so that fill characters encoded with more than one code unit are correctly decoded.
Thank you to Victor Zverovich, Corentin Jabot, and Peter Brett for their insights; their commentary shaped much of this proposal.
[N4910] |
"Working Draft, Standard for Programming Language C++", N4910, 2022. https://wg21.link/n4910 |
[UAX#15] |
Ken Whistler, "Unicode Standard Annex #15 - Unicode Normalization Forms", Revision 51, Unicode 14.0.0, 2021. https://www.unicode.org/reports/tr15/tr15-51.html |
These changes are relative to N4910 [N4910].
Hide inserted textChange in 22.14.2.2 [format.string.std] paragraph 1:
[…]
The syntax of format specifications is as follows:
[…]
fill:any character other than[…]{U+007B LEFT CURLY BRACKET or}U+007D RIGHT CURLY BRACKET
Change in 22.14.2.2 [format.string.std] paragraph 2:
The fill character is the character denoted by the fill specifier or, if the fill specifier is absent, U+0020 SPACE.
[Note 2:The fill character can be any character other than { or }.The presence of afill characterfill specifier is signaled by the character following it, which must be one of the alignment options. If the second character of std-format-spec is not a valid alignment option, then it is assumed thatboth the fill character and the alignment option arethe fill-and-align specifier is absent. — end note]
Change in
22.14.2.2 [format.string.std] paragraph 3:
Drafting note: The notes that follow Note X and
Note Y will require renumbering.
The align specifier applies to all argument types. The meaning of the various alignment options is as specified in Table 64.
[ Example 1:— end example ]char c = 120; string s0 = format("{:6}", 42); // value of s0 is " 42" string s1 = format("{:6}", 'x'); // value of s1 is "x " string s2 = format("{:*<6}", 'x'); // value of s2 is "x*****" string s3 = format("{:*>6}", 'x'); // value of s3 is "*****x" string s4 = format("{:*^6}", 'x'); // value of s4 is "**x***" string s5 = format("{:6d}", c); // value of s5 is " 120" string s6 = format("{:6}", true); // value of s6 is "true " string s7 = format("{:*>6}", "12345678"); // value of s7 is "12345678"
[ Note 3: Unless a minimum field width is defined, the field width is determined by the size of the content and the alignment option has no effect. — end note ]
[ Note X: If the width of the formatting argument value exceeds the field width, then the alignment and width options have no effect. — end note ]
Table 64: Meaning of align options [tab:format.align]
Option Meaning < Forces the field to be aligned to the start of the available space by inserting n fill characters after the formatting argument value where n is the field width minus the width of the formatting argument or, if that value is negative, 0. This is the default for non-arithmetic non-pointer types, charT, and bool, unless an integer presentation type is specified. > Forces the field to be aligned to the end of the available space by inserting n fill characters before the formatting argument value where n is the field width minus the width of the formatting argument or, if that value is negative, 0. This is the default for arithmetic types other than charT and bool, pointer types, or when an integer presentation type is specified. ^ Forces the field to be centered within the available space by inserting ⌊n/2⌋ fill characters before and ⌈n/2⌉ fill characters after the formatting argument value, where n is the total number of fill characters to insertthe field width minus the width of the formatting argument or, if that value is negative, 0.
[ Note Y: The estimated width of the fill character is ignored when determining the number of fill characters to insert. — end note ]
Change in 22.14.2.2 [format.string.std] paragraph 11:
For a string in a Unicode encoding, implementations should estimate the width of a string as the sum of estimated widths of the firstcode pointsUCS scalar values in its extended grapheme clusters. The extended grapheme clusters of a string are defined by UAX #29. The estimated width of the followingcode pointsUCS scalar values is 2:
[ … ]
The estimated width of othercode pointsUCS scalar values is 1.