From Jason Turner

[format.string]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp4djppoav/{from.md → to.md} +434 -0
tmp/tmp4djppoav/{from.md → to.md} RENAMED
@@ -0,0 +1,434 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ### Format string <a id="format.string">[[format.string]]</a>
2
+
3
+ #### In general <a id="format.string.general">[[format.string.general]]</a>
4
+
5
+ A *format string* for arguments `args` is a (possibly empty) sequence of
6
+ *replacement fields*, *escape sequences*, and characters other than `{`
7
+ and `}`. Let `charT` be the character type of the format string. Each
8
+ character that is not part of a replacement field or an escape sequence
9
+ is copied unchanged to the output. An escape sequence is one of `{{` or
10
+ `}}`. It is replaced with `{` or `}`, respectively, in the output. The
11
+ syntax of replacement fields is as follows:
12
+
13
+ ``` bnf
14
+ replacement-field
15
+ '{' arg-idₒₚₜ format-specifierₒₚₜ '}'
16
+ ```
17
+
18
+ ``` bnf
19
+ arg-id
20
+ '0'
21
+ positive-integer
22
+ ```
23
+
24
+ ``` bnf
25
+ positive-integer
26
+ nonzero-digit
27
+ positive-integer digit
28
+ ```
29
+
30
+ ``` bnf
31
+ nonnegative-integer
32
+ digit
33
+ nonnegative-integer digit
34
+ ```
35
+
36
+ ``` bnf
37
+ nonzero-digit one of
38
+ '1 2 3 4 5 6 7 8 9'
39
+ ```
40
+
41
+ ``` bnf
42
+ digit one of
43
+ '0 1 2 3 4 5 6 7 8 9'
44
+ ```
45
+
46
+ ``` bnf
47
+ format-specifier
48
+ ':' format-spec
49
+ ```
50
+
51
+ ``` bnf
52
+ format-spec
53
+ as specified by the formatter specialization for the argument type
54
+ ```
55
+
56
+ The *arg-id* field specifies the index of the argument in `args` whose
57
+ value is to be formatted and inserted into the output instead of the
58
+ replacement field. If there is no argument with the index *arg-id* in
59
+ `args`, the string is not a format string for `args`. The optional
60
+ *format-specifier* field explicitly specifies a format for the
61
+ replacement value.
62
+
63
+ [*Example 1*:
64
+
65
+ ``` cpp
66
+ string s = format("{0}-{{", 8); // value of s is "8-{"
67
+ ```
68
+
69
+ — *end example*]
70
+
71
+ If all *arg-id*s in a format string are omitted (including those in the
72
+ *format-spec*, as interpreted by the corresponding `formatter`
73
+ specialization), argument indices 0, 1, 2, … will automatically be used
74
+ in that order. If some *arg-id*s are omitted and some are present, the
75
+ string is not a format string.
76
+
77
+ [*Note 1*: A format string cannot contain a mixture of automatic and
78
+ manual indexing. — *end note*]
79
+
80
+ [*Example 2*:
81
+
82
+ ``` cpp
83
+ string s0 = format("{} to {}", "a", "b"); // OK, automatic indexing
84
+ string s1 = format("{1} to {0}", "a", "b"); // OK, manual indexing
85
+ string s2 = format("{0} to {}", "a", "b"); // not a format string (mixing automatic and manual indexing),
86
+ // throws format_error
87
+ string s3 = format("{} to {1}", "a", "b"); // not a format string (mixing automatic and manual indexing),
88
+ // throws format_error
89
+ ```
90
+
91
+ — *end example*]
92
+
93
+ The *format-spec* field contains *format specifications* that define how
94
+ the value should be presented. Each type can define its own
95
+ interpretation of the *format-spec* field. If *format-spec* does not
96
+ conform to the format specifications for the argument type referred to
97
+ by *arg-id*, the string is not a format string for `args`.
98
+
99
+ [*Example 3*:
100
+
101
+ - For arithmetic, pointer, and string types the *format-spec* is
102
+ interpreted as a *std-format-spec* as described in
103
+ [[format.string.std]].
104
+ - For chrono types the *format-spec* is interpreted as a
105
+ *chrono-format-spec* as described in [[time.format]].
106
+ - For user-defined `formatter` specializations, the behavior of the
107
+ `parse` member function determines how the *format-spec* is
108
+ interpreted.
109
+
110
+ — *end example*]
111
+
112
+ #### Standard format specifiers <a id="format.string.std">[[format.string.std]]</a>
113
+
114
+ Each `formatter` specializations described in [[format.formatter.spec]]
115
+ for fundamental and string types interprets *format-spec* as a
116
+ *std-format-spec*.
117
+
118
+ [*Note 1*: The format specification can be used to specify such details
119
+ as field width, alignment, padding, and decimal precision. Some of the
120
+ formatting options are only supported for arithmetic
121
+ types. — *end note*]
122
+
123
+ The syntax of format specifications is as follows:
124
+
125
+ ``` bnf
126
+ std-format-spec
127
+ fill-and-alignₒₚₜ signₒₚₜ '#'ₒₚₜ '0'ₒₚₜ widthₒₚₜ precisionₒₚₜ 'L'ₒₚₜ typeₒₚₜ
128
+ ```
129
+
130
+ ``` bnf
131
+ fill-and-align
132
+ fillₒₚₜ align
133
+ ```
134
+
135
+ ``` bnf
136
+ fill
137
+ any character other than \{ or \}
138
+ ```
139
+
140
+ ``` bnf
141
+ align one of
142
+ '< > ^'
143
+ ```
144
+
145
+ ``` bnf
146
+ sign one of
147
+ '+ -' space
148
+ ```
149
+
150
+ ``` bnf
151
+ width
152
+ positive-integer
153
+ '{' arg-idₒₚₜ '}'
154
+ ```
155
+
156
+ ``` bnf
157
+ precision
158
+ '.' nonnegative-integer
159
+ '.' '{' arg-idₒₚₜ '}'
160
+ ```
161
+
162
+ ``` bnf
163
+ type one of
164
+ 'a A b B c d e E f F g G o p s x X'
165
+ ```
166
+
167
+ [*Note 2*: The *fill* character can be any character other than `{` or
168
+ `}`. The presence of a fill character is signaled by the character
169
+ following it, which must be one of the alignment options. If the second
170
+ character of *std-format-spec* is not a valid alignment option, then it
171
+ is assumed that both the fill character and the alignment option are
172
+ absent. — *end note*]
173
+
174
+ The *align* specifier applies to all argument types. The meaning of the
175
+ various alignment options is as specified in [[format.align]].
176
+
177
+ [*Example 1*:
178
+
179
+ ``` cpp
180
+ char c = 120;
181
+ string s0 = format("{:6}", 42); // value of s0 is "\ \ \ \ 42"
182
+ string s1 = format("{:6}", 'x'); // value of s1 is "x\ \ \ \ \ "
183
+ string s2 = format("{:*<6}", 'x'); // value of s2 is "x*****"
184
+ string s3 = format("{:*>6}", 'x'); // value of s3 is "*****x"
185
+ string s4 = format("{:*^6}", 'x'); // value of s4 is "**x***"
186
+ string s5 = format("{:6d}", c); // value of s5 is "\ \ \ 120"
187
+ string s6 = format("{:6}", true); // value of s6 is "true\ \ "
188
+ ```
189
+
190
+ — *end example*]
191
+
192
+ [*Note 3*: Unless a minimum field width is defined, the field width is
193
+ determined by the size of the content and the alignment option has no
194
+ effect. — *end note*]
195
+
196
+ **Table: Meaning of align options** <a id="format.align">[format.align]</a>
197
+
198
+ | Option | Meaning |
199
+ | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
200
+ | `<` | 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. |
201
+ | % `>` | 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. |
202
+ | % `^` | 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. |
203
+
204
+
205
+ The *sign* option is only valid for arithmetic types other than `charT`
206
+ and `bool` or when an integer presentation type is specified. The
207
+ meaning of the various options is as specified in [[format.sign]].
208
+
209
+ [*Note 4*: For negative numbers and negative zero the output of
210
+ `to_chars` will already contain the sign so no additional transformation
211
+ is performed. — *end note*]
212
+
213
+ The *sign* option applies to floating-point infinity and NaN.
214
+
215
+ [*Example 2*:
216
+
217
+ ``` cpp
218
+ double inf = numeric_limits<double>::infinity();
219
+ double nan = numeric_limits<double>::quiet_NaN();
220
+ string s0 = format("{0:},{0:+},{0:-},{0: }", 1); // value of s0 is "1,+1,1, 1"
221
+ string s1 = format("{0:},{0:+},{0:-},{0: }", -1); // value of s1 is "-1,-1,-1,-1"
222
+ string s2 = format("{0:},{0:+},{0:-},{0: }", inf); // value of s2 is "inf,+inf,inf, inf"
223
+ string s3 = format("{0:},{0:+},{0:-},{0: }", nan); // value of s3 is "nan,+nan,nan, nan"
224
+ ```
225
+
226
+ — *end example*]
227
+
228
+ The `#` option causes the *alternate form* to be used for the
229
+ conversion. This option is valid for arithmetic types other than `charT`
230
+ and `bool` or when an integer presentation type is specified, and not
231
+ otherwise. For integral types, the alternate form inserts the base
232
+ prefix (if any) specified in [[format.type.int]] into the output after
233
+ the sign character (possibly space) if there is one, or before the
234
+ output of `to_chars` otherwise. For floating-point types, the alternate
235
+ form causes the result of the conversion of finite values to always
236
+ contain a decimal-point character, even if no digits follow it.
237
+ Normally, a decimal-point character appears in the result of these
238
+ conversions only if a digit follows it. In addition, for `g` and `G`
239
+ conversions, trailing zeros are not removed from the result.
240
+
241
+ If `{ \opt{arg-id} }` is used in a *width* or *precision*, the value of
242
+ the corresponding formatting argument is used in its place. If the
243
+ corresponding formatting argument is not of integral type, or its value
244
+ is negative for *precision* or non-positive for *width*, an exception of
245
+ type `format_error` is thrown.
246
+
247
+ The *positive-integer* in *width* is a decimal integer defining the
248
+ minimum field width. If *width* is not specified, there is no minimum
249
+ field width, and the field width is determined based on the content of
250
+ the field.
251
+
252
+ The *width* of a string is defined as the estimated number of column
253
+ positions appropriate for displaying it in a terminal.
254
+
255
+ [*Note 5*: This is similar to the semantics of the POSIX `wcswidth`
256
+ function. — *end note*]
257
+
258
+ For the purposes of width computation, a string is assumed to be in a
259
+ locale-independent, implementation-defined encoding. Implementations
260
+ should use a Unicode encoding on platforms capable of displaying Unicode
261
+ text in a terminal.
262
+
263
+ [*Note 6*: This is the case for Windows-based and many POSIX-based
264
+ operating systems. — *end note*]
265
+
266
+ For a string in a Unicode encoding, implementations should estimate the
267
+ width of a string as the sum of estimated widths of the first code
268
+ points in its extended grapheme clusters. The extended grapheme clusters
269
+ of a string are defined by UAX \#29. The estimated width of the
270
+ following code points is 2:
271
+
272
+ - `U+1100-U+115F`
273
+ - `U+2329-U+232A`
274
+ - `U+2E80-U+303E`
275
+ - `U+3040-U+A4CF`
276
+ - `U+AC00-U+D7A3`
277
+ - `U+F900-U+FAFF`
278
+ - `U+FE10-U+FE19`
279
+ - `U+FE30-U+FE6F`
280
+ - `U+FF00-U+FF60`
281
+ - `U+FFE0-U+FFE6`
282
+ - `U+1F300-U+1F64F`
283
+ - `U+1F900-U+1F9FF`
284
+ - `U+20000-U+2FFFD`
285
+ - `U+30000-U+3FFFD`
286
+
287
+ The estimated width of other code points is 1.
288
+
289
+ For a string in a non-Unicode encoding, the width of a string is
290
+ unspecified.
291
+
292
+ A zero (`0`) character preceding the *width* field pads the field with
293
+ leading zeros (following any indication of sign or base) to the field
294
+ width, except when applied to an infinity or NaN. This option is only
295
+ valid for arithmetic types other than `charT` and `bool` or when an
296
+ integer presentation type is specified. If the `0` character and an
297
+ *align* option both appear, the `0` character is ignored.
298
+
299
+ [*Example 3*:
300
+
301
+ ``` cpp
302
+ char c = 120;
303
+ string s1 = format("{:+06d}", c); // value of s1 is "+00120"
304
+ string s2 = format("{:#06x}", 0xa); // value of s2 is "0x000a"
305
+ string s3 = format("{:<06}", -42); // value of s3 is "-42\ \ \ " (0 is ignored because of < alignment)
306
+ ```
307
+
308
+ — *end example*]
309
+
310
+ The *nonnegative-integer* in *precision* is a decimal integer defining
311
+ the precision or maximum field size. It can only be used with
312
+ floating-point and string types. For floating-point types this field
313
+ specifies the formatting precision. For string types, this field
314
+ provides an upper bound for the estimated width of the prefix of the
315
+ input string that is copied into the output. For a string in a Unicode
316
+ encoding, the formatter copies to the output the longest prefix of whole
317
+ extended grapheme clusters whose estimated width is no greater than the
318
+ precision.
319
+
320
+ When the `L` option is used, the form used for the conversion is called
321
+ the *locale-specific form*. The `L` option is only valid for arithmetic
322
+ types, and its effect depends upon the type.
323
+
324
+ - For integral types, the locale-specific form causes the context’s
325
+ locale to be used to insert the appropriate digit group separator
326
+ characters.
327
+ - For floating-point types, the locale-specific form causes the
328
+ context’s locale to be used to insert the appropriate digit group and
329
+ radix separator characters.
330
+ - For the textual representation of `bool`, the locale-specific form
331
+ causes the context’s locale to be used to insert the appropriate
332
+ string as if obtained with `numpunct::truename` or
333
+ `numpunct::falsename`.
334
+
335
+ The *type* determines how the data should be presented.
336
+
337
+ The available string presentation types are specified in
338
+ [[format.type.string]].
339
+
340
+ **Table: Meaning of type options for strings** <a id="format.type.string">[format.type.string]</a>
341
+
342
+ | Type | Meaning |
343
+ | --------- | -------------------------------- |
344
+ | none, `s` | Copies the string to the output. |
345
+
346
+
347
+ The meaning of some non-string presentation types is defined in terms of
348
+ a call to `to_chars`. In such cases, let \[`first`, `last`) be a range
349
+ large enough to hold the `to_chars` output and `value` be the formatting
350
+ argument value. Formatting is done as if by calling `to_chars` as
351
+ specified and copying the output through the output iterator of the
352
+ format context.
353
+
354
+ [*Note 7*: Additional padding and adjustments are performed prior to
355
+ copying the output through the output iterator as specified by the
356
+ format specifiers. — *end note*]
357
+
358
+ The available integer presentation types for integral types other than
359
+ `bool` and `charT` are specified in [[format.type.int]].
360
+
361
+ [*Example 4*:
362
+
363
+ ``` cpp
364
+ string s0 = format("{}", 42); // value of s0 is "42"
365
+ string s1 = format("{0:b} {0:d} {0:o} {0:x}", 42); // value of s1 is "101010 42 52 2a"
366
+ string s2 = format("{0:#x} {0:#X}", 42); // value of s2 is "0x2a 0X2A"
367
+ string s3 = format("{:L}", 1234); // value of s3 might be "1,234"
368
+ // (depending on the locale)
369
+ ```
370
+
371
+ — *end example*]
372
+
373
+ [*Note 8*: If the formatting argument type is `charT` or `bool`, the
374
+ default is instead `c` or `s`, respectively. — *end note*]
375
+
376
+ The available `charT` presentation types are specified in
377
+ [[format.type.char]].
378
+
379
+ **Table: Meaning of type options for `charT`** <a id="format.type.char">[format.type.char]</a>
380
+
381
+ | Type | Meaning |
382
+ | ------------------------------ | ------------------------------------ |
383
+ | none, `c` | Copies the character to the output. |
384
+ | % `b`, `B`, `d`, `o`, `x`, `X` | As specified in [[format.type.int]]. |
385
+
386
+
387
+ The available `bool` presentation types are specified in
388
+ [[format.type.bool]].
389
+
390
+ **Table: Meaning of type options for `bool`** <a id="format.type.bool">[format.type.bool]</a>
391
+
392
+ | Type | Meaning |
393
+ | ----------------------------------- | -------------------------------------------------------------------------------------- |
394
+ | none, `s` | Copies textual representation, either `true` or `false`, to the output. |
395
+ | % `b`, `B`, `c`, `d`, `o`, `x`, `X` | As specified in [[format.type.int]] for the value `static_cast<unsigned char>(value)`. |
396
+
397
+
398
+ The available floating-point presentation types and their meanings for
399
+ values other than infinity and NaN are specified in
400
+ [[format.type.float]]. For lower-case presentation types, infinity and
401
+ NaN are formatted as `inf` and `nan`, respectively. For upper-case
402
+ presentation types, infinity and NaN are formatted as `INF` and `NAN`,
403
+ respectively.
404
+
405
+ [*Note 9*: In either case, a sign is included if indicated by the
406
+ *sign* option. — *end note*]
407
+
408
+ **Table: Meaning of type options for floating-point types** <a id="format.type.float">[format.type.float]</a>
409
+
410
+ | Type | Meaning |
411
+ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
412
+ | `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. |
413
+ | % `A` | The same as `a`, except that it uses uppercase letters for digits above 9 and `P` to indicate the exponent. |
414
+ | % `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. |
415
+ | % `E` | The same as `e`, except that it uses `E` to indicate exponent. |
416
+ | % `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. |
417
+ | % `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. |
418
+ | % `G` | The same as `g`, except that it uses `E` to indicate exponent. |
419
+ | % 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. |
420
+
421
+
422
+ The available pointer presentation types and their mapping to `to_chars`
423
+ are specified in [[format.type.ptr]].
424
+
425
+ [*Note 10*: Pointer presentation types also apply to
426
+ `nullptr_t`. — *end note*]
427
+
428
+ **Table: Meaning of type options for pointer types** <a id="format.type.ptr">[format.type.ptr]</a>
429
+
430
+ | Type | Meaning |
431
+ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
432
+ | 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. |
433
+
434
+