From Jason Turner

[format.string.std]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp__y35s_z/{from.md → to.md} +323 -0
tmp/tmp__y35s_z/{from.md → to.md} RENAMED
@@ -0,0 +1,323 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### Standard format specifiers <a id="format.string.std">[[format.string.std]]</a>
2
+
3
+ Each `formatter` specializations described in [[format.formatter.spec]]
4
+ for fundamental and string types interprets *format-spec* as a
5
+ *std-format-spec*.
6
+
7
+ [*Note 1*: The format specification can be used to specify such details
8
+ as field width, alignment, padding, and decimal precision. Some of the
9
+ formatting options are only supported for arithmetic
10
+ types. — *end note*]
11
+
12
+ The syntax of format specifications is as follows:
13
+
14
+ ``` bnf
15
+ std-format-spec
16
+ fill-and-alignₒₚₜ signₒₚₜ '#'ₒₚₜ '0'ₒₚₜ widthₒₚₜ precisionₒₚₜ 'L'ₒₚₜ typeₒₚₜ
17
+ ```
18
+
19
+ ``` bnf
20
+ fill-and-align
21
+ fillₒₚₜ align
22
+ ```
23
+
24
+ ``` bnf
25
+ fill
26
+ any character other than \{ or \}
27
+ ```
28
+
29
+ ``` bnf
30
+ align one of
31
+ '< > ^'
32
+ ```
33
+
34
+ ``` bnf
35
+ sign one of
36
+ '+ -' space
37
+ ```
38
+
39
+ ``` bnf
40
+ width
41
+ positive-integer
42
+ '{' arg-idₒₚₜ '}'
43
+ ```
44
+
45
+ ``` bnf
46
+ precision
47
+ '.' nonnegative-integer
48
+ '.' '{' arg-idₒₚₜ '}'
49
+ ```
50
+
51
+ ``` bnf
52
+ type one of
53
+ 'a A b B c d e E f F g G o p s x X'
54
+ ```
55
+
56
+ [*Note 2*: The *fill* character can be any character other than `{` or
57
+ `}`. The presence of a fill character is signaled by the character
58
+ following it, which must be one of the alignment options. If the second
59
+ character of *std-format-spec* is not a valid alignment option, then it
60
+ is assumed that both the fill character and the alignment option are
61
+ absent. — *end note*]
62
+
63
+ The *align* specifier applies to all argument types. The meaning of the
64
+ various alignment options is as specified in [[format.align]].
65
+
66
+ [*Example 1*:
67
+
68
+ ``` cpp
69
+ char c = 120;
70
+ string s0 = format("{:6}", 42); // value of s0 is "\ \ \ \ 42"
71
+ string s1 = format("{:6}", 'x'); // value of s1 is "x\ \ \ \ \ "
72
+ string s2 = format("{:*<6}", 'x'); // value of s2 is "x*****"
73
+ string s3 = format("{:*>6}", 'x'); // value of s3 is "*****x"
74
+ string s4 = format("{:*^6}", 'x'); // value of s4 is "**x***"
75
+ string s5 = format("{:6d}", c); // value of s5 is "\ \ \ 120"
76
+ string s6 = format("{:6}", true); // value of s6 is "true\ \ "
77
+ ```
78
+
79
+ — *end example*]
80
+
81
+ [*Note 3*: Unless a minimum field width is defined, the field width is
82
+ determined by the size of the content and the alignment option has no
83
+ effect. — *end note*]
84
+
85
+ **Table: Meaning of align options** <a id="format.align">[format.align]</a>
86
+
87
+ | Option | Meaning |
88
+ | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
89
+ | `<` | Forces the field to be aligned to the start of the available space. This is the default for non-arithmetic types, `charT`, and `bool`, unless an integer presentation type is specified. |
90
+ | % `>` | Forces the field to be aligned to the end of the available space. This is the default for arithmetic types other than `charT` and `bool` or when an integer presentation type is specified. |
91
+ | % `^` | Forces the field to be centered within the available space by inserting $\bigl\lfloor \frac{n}{2} \bigr\rfloor$ characters before and $\bigl\lceil \frac{n}{2} \bigr\rceil$ characters after the value, where $n$ is the total number of fill characters to insert. |
92
+
93
+
94
+ The *sign* option is only valid for arithmetic types other than `charT`
95
+ and `bool` or when an integer presentation type is specified. The
96
+ meaning of the various options is as specified in [[format.sign]].
97
+
98
+ [*Note 4*: For negative numbers and negative zero the output of
99
+ `to_chars` will already contain the sign so no additional transformation
100
+ is performed. — *end note*]
101
+
102
+ The *sign* option applies to floating-point infinity and NaN.
103
+
104
+ [*Example 2*:
105
+
106
+ ``` cpp
107
+ double inf = numeric_limits<double>::infinity();
108
+ double nan = numeric_limits<double>::quiet_NaN();
109
+ string s0 = format("{0:},{0:+},{0:-},{0: }", 1); // value of s0 is "1,+1,1, 1"
110
+ string s1 = format("{0:},{0:+},{0:-},{0: }", -1); // value of s1 is "-1,-1,-1,-1"
111
+ string s2 = format("{0:},{0:+},{0:-},{0: }", inf); // value of s2 is "inf,+inf,inf, inf"
112
+ string s3 = format("{0:},{0:+},{0:-},{0: }", nan); // value of s3 is "nan,+nan,nan, nan"
113
+ ```
114
+
115
+ — *end example*]
116
+
117
+ The `#` option causes the *alternate form* to be used for the
118
+ conversion. This option is valid for arithmetic types other than `charT`
119
+ and `bool` or when an integer presentation type is specified, and not
120
+ otherwise. For integral types, the alternate form inserts the base
121
+ prefix (if any) specified in [[format.type.int]] into the output after
122
+ the sign character (possibly space) if there is one, or before the
123
+ output of `to_chars` otherwise. For floating-point types, the alternate
124
+ form causes the result of the conversion of finite values to always
125
+ contain a decimal-point character, even if no digits follow it.
126
+ Normally, a decimal-point character appears in the result of these
127
+ conversions only if a digit follows it. In addition, for `g` and `G`
128
+ conversions, trailing zeros are not removed from the result.
129
+
130
+ If `{ \opt{arg-id} }` is used in a *width* or *precision*, the value of
131
+ the corresponding formatting argument is used in its place. If the
132
+ corresponding formatting argument is not of integral type, or its value
133
+ is negative for *precision* or non-positive for *width*, an exception of
134
+ type `format_error` is thrown.
135
+
136
+ The *positive-integer* in *width* is a decimal integer defining the
137
+ minimum field width. If *width* is not specified, there is no minimum
138
+ field width, and the field width is determined based on the content of
139
+ the field.
140
+
141
+ The *width* of a string is defined as the estimated number of column
142
+ positions appropriate for displaying it in a terminal.
143
+
144
+ [*Note 5*: This is similar to the semantics of the POSIX `wcswidth`
145
+ function. — *end note*]
146
+
147
+ For the purposes of width computation, a string is assumed to be in a
148
+ locale-independent, implementation-defined encoding. Implementations
149
+ should use a Unicode encoding on platforms capable of displaying Unicode
150
+ text in a terminal.
151
+
152
+ [*Note 6*: This is the case for Windows-based and many POSIX-based
153
+ operating systems. — *end note*]
154
+
155
+ For a string in a Unicode encoding, implementations should estimate the
156
+ width of a string as the sum of estimated widths of the first code
157
+ points in its extended grapheme clusters. The extended grapheme clusters
158
+ of a string are defined by UAX \#29. The estimated width of the
159
+ following code points is 2:
160
+
161
+ - `U+1100-U+115F`
162
+ - `U+2329-U+232A`
163
+ - `U+2E80-U+303E`
164
+ - `U+3040-U+A4CF`
165
+ - `U+AC00-U+D7A3`
166
+ - `U+F900-U+FAFF`
167
+ - `U+FE10-U+FE19`
168
+ - `U+FE30-U+FE6F`
169
+ - `U+FF00-U+FF60`
170
+ - `U+FFE0-U+FFE6`
171
+ - `U+1F300-U+1F64F`
172
+ - `U+1F900-U+1F9FF`
173
+ - `U+20000-U+2FFFD`
174
+ - `U+30000-U+3FFFD`
175
+
176
+ The estimated width of other code points is 1.
177
+
178
+ For a string in a non-Unicode encoding, the width of a string is
179
+ unspecified.
180
+
181
+ A zero (`0`) character preceding the *width* field pads the field with
182
+ leading zeros (following any indication of sign or base) to the field
183
+ width, except when applied to an infinity or NaN. This option is only
184
+ valid for arithmetic types other than `charT` and `bool` or when an
185
+ integer presentation type is specified. If the `0` character and an
186
+ *align* option both appear, the `0` character is ignored.
187
+
188
+ [*Example 3*:
189
+
190
+ ``` cpp
191
+ char c = 120;
192
+ string s1 = format("{:+06d}", c); // value of s1 is "+00120"
193
+ string s2 = format("{:#06x}", 0xa); // value of s2 is "0x000a"
194
+ string s3 = format("{:<06}", -42); // value of s3 is "-42\ \ \ " (0 is ignored because of < alignment)
195
+ ```
196
+
197
+ — *end example*]
198
+
199
+ The *nonnegative-integer* in *precision* is a decimal integer defining
200
+ the precision or maximum field size. It can only be used with
201
+ floating-point and string types. For floating-point types this field
202
+ specifies the formatting precision. For string types, this field
203
+ provides an upper bound for the estimated width of the prefix of the
204
+ input string that is copied into the output. For a string in a Unicode
205
+ encoding, the formatter copies to the output the longest prefix of whole
206
+ extended grapheme clusters whose estimated width is no greater than the
207
+ precision.
208
+
209
+ When the `L` option is used, the form used for the conversion is called
210
+ the *locale-specific form*. The `L` option is only valid for arithmetic
211
+ types, and its effect depends upon the type.
212
+
213
+ - For integral types, the locale-specific form causes the context’s
214
+ locale to be used to insert the appropriate digit group separator
215
+ characters.
216
+ - For floating-point types, the locale-specific form causes the
217
+ context’s locale to be used to insert the appropriate digit group and
218
+ radix separator characters.
219
+ - For the textual representation of `bool`, the locale-specific form
220
+ causes the context’s locale to be used to insert the appropriate
221
+ string as if obtained with `numpunct::truename` or
222
+ `numpunct::falsename`.
223
+
224
+ The *type* determines how the data should be presented.
225
+
226
+ The available string presentation types are specified in
227
+ [[format.type.string]].
228
+
229
+ **Table: Meaning of type options for strings** <a id="format.type.string">[format.type.string]</a>
230
+
231
+ | Type | Meaning |
232
+ | --------- | -------------------------------- |
233
+ | none, `s` | Copies the string to the output. |
234
+
235
+
236
+ The meaning of some non-string presentation types is defined in terms of
237
+ a call to `to_chars`. In such cases, let \[`first`, `last`) be a range
238
+ large enough to hold the `to_chars` output and `value` be the formatting
239
+ argument value. Formatting is done as if by calling `to_chars` as
240
+ specified and copying the output through the output iterator of the
241
+ format context.
242
+
243
+ [*Note 7*: Additional padding and adjustments are performed prior to
244
+ copying the output through the output iterator as specified by the
245
+ format specifiers. — *end note*]
246
+
247
+ The available integer presentation types for integral types other than
248
+ `bool` and `charT` are specified in [[format.type.int]].
249
+
250
+ [*Example 4*:
251
+
252
+ ``` cpp
253
+ string s0 = format("{}", 42); // value of s0 is "42"
254
+ string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // value of s1 is "101010 42 52 2a"
255
+ string s2 = format("{0:#x} {0:#X}", 42); // value of s2 is "0x2a 0X2A"
256
+ string s3 = format("{:L}", 1234); // value of s3 might be "1,234"
257
+ // (depending on the locale)
258
+ ```
259
+
260
+ — *end example*]
261
+
262
+ [*Note 8*: If the formatting argument type is `charT` or `bool`, the
263
+ default is instead `c` or `s`, respectively. — *end note*]
264
+
265
+ The available `charT` presentation types are specified in
266
+ [[format.type.char]].
267
+
268
+ **Table: Meaning of type options for `charT`** <a id="format.type.char">[format.type.char]</a>
269
+
270
+ | Type | Meaning |
271
+ | ------------------------------ | ------------------------------------ |
272
+ | none, `c` | Copies the character to the output. |
273
+ | % `b`, `B`, `d`, `o`, `x`, `X` | As specified in [[format.type.int]]. |
274
+
275
+
276
+ The available `bool` presentation types are specified in
277
+ [[format.type.bool]].
278
+
279
+ **Table: Meaning of type options for `bool`** <a id="format.type.bool">[format.type.bool]</a>
280
+
281
+ | Type | Meaning |
282
+ | ----------------------------------- | -------------------------------------------------------------------------------------- |
283
+ | none, `s` | Copies textual representation, either `true` or `false`, to the output. |
284
+ | % `b`, `B`, `c`, `d`, `o`, `x`, `X` | As specified in [[format.type.int]] for the value `static_cast<unsigned char>(value)`. |
285
+
286
+
287
+ The available floating-point presentation types and their meanings for
288
+ values other than infinity and NaN are specified in
289
+ [[format.type.float]]. For lower-case presentation types, infinity and
290
+ NaN are formatted as `inf` and `nan`, respectively. For upper-case
291
+ presentation types, infinity and NaN are formatted as `INF` and `NAN`,
292
+ respectively.
293
+
294
+ [*Note 9*: In either case, a sign is included if indicated by the
295
+ *sign* option. — *end note*]
296
+
297
+ **Table: Meaning of type options for floating-point types** <a id="format.type.float">[format.type.float]</a>
298
+
299
+ | Type | Meaning |
300
+ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
301
+ | `a` | If precision is specified, equivalent to \begin{codeblock} to_chars(first, last, value, chars_format::hex, precision) \end{codeblock} where `precision` is the specified formatting precision; equivalent to \begin{codeblock} to_chars(first, last, value, chars_format::hex) \end{codeblock} otherwise. |
302
+ | % `A` | The same as `a`, except that it uses uppercase letters for digits above 9 and `P` to indicate the exponent. |
303
+ | % `e` | Equivalent to \begin{codeblock} to_chars(first, last, value, chars_format::scientific, precision) \end{codeblock} where `precision` is the specified formatting precision, or `6` if precision is not specified. |
304
+ | % `E` | The same as `e`, except that it uses `E` to indicate exponent. |
305
+ | % `f`, `F` | Equivalent to \begin{codeblock} to_chars(first, last, value, chars_format::fixed, precision) \end{codeblock} where `precision` is the specified formatting precision, or `6` if precision is not specified. |
306
+ | % `g` | Equivalent to \begin{codeblock} to_chars(first, last, value, chars_format::general, precision) \end{codeblock} where `precision` is the specified formatting precision, or `6` if precision is not specified. |
307
+ | % `G` | The same as `g`, except that it uses `E` to indicate exponent. |
308
+ | % none | If precision is specified, equivalent to \begin{codeblock} to_chars(first, last, value, chars_format::general, precision) \end{codeblock} where `precision` is the specified formatting precision; equivalent to \begin{codeblock} to_chars(first, last, value) \end{codeblock} otherwise. |
309
+
310
+
311
+ The available pointer presentation types and their mapping to `to_chars`
312
+ are specified in [[format.type.ptr]].
313
+
314
+ [*Note 10*: Pointer presentation types also apply to
315
+ `nullptr_t`. — *end note*]
316
+
317
+ **Table: Meaning of type options for pointer types** <a id="format.type.ptr">[format.type.ptr]</a>
318
+
319
+ | Type | Meaning |
320
+ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
321
+ | none, `p` | If `uintptr_t` is defined, \begin{codeblock} to_chars(first, last, reinterpret_cast<uintptr_t>(value), 16) \end{codeblock} with the prefix `0x` added to the output; otherwise, implementation-defined. |
322
+
323
+