]> git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/doc/syntax.rst
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / fmt / doc / syntax.rst
1 .. _syntax:
2
3 ********************
4 Format String Syntax
5 ********************
6
7 Formatting functions such as :ref:`fmt::format() <format>` and
8 :ref:`fmt::print() <print>` use the same format string syntax described in this
9 section.
10
11 Format strings contain "replacement fields" surrounded by curly braces ``{}``.
12 Anything that is not contained in braces is considered literal text, which is
13 copied unchanged to the output. If you need to include a brace character in the
14 literal text, it can be escaped by doubling: ``{{`` and ``}}``.
15
16 The grammar for a replacement field is as follows:
17
18 .. productionlist:: sf
19 replacement_field: "{" [`arg_id`] [":" `format_spec`] "}"
20 arg_id: `integer` | `identifier`
21 integer: `digit`+
22 digit: "0"..."9"
23 identifier: `id_start` `id_continue`*
24 id_start: "a"..."z" | "A"..."Z" | "_"
25 id_continue: `id_start` | `digit`
26
27 In less formal terms, the replacement field can start with an *arg_id*
28 that specifies the argument whose value is to be formatted and inserted into
29 the output instead of the replacement field.
30 The *arg_id* is optionally followed by a *format_spec*, which is preceded
31 by a colon ``':'``. These specify a non-default format for the replacement value.
32
33 See also the :ref:`formatspec` section.
34
35 If the numerical arg_ids in a format string are 0, 1, 2, ... in sequence,
36 they can all be omitted (not just some) and the numbers 0, 1, 2, ... will be
37 automatically inserted in that order.
38
39 Named arguments can be referred to by their names or indices.
40
41 Some simple format string examples::
42
43 "First, thou shalt count to {0}" // References the first argument
44 "Bring me a {}" // Implicitly references the first argument
45 "From {} to {}" // Same as "From {0} to {1}"
46
47 The *format_spec* field contains a specification of how the value should be
48 presented, including such details as field width, alignment, padding, decimal
49 precision and so on. Each value type can define its own "formatting
50 mini-language" or interpretation of the *format_spec*.
51
52 Most built-in types support a common formatting mini-language, which is
53 described in the next section.
54
55 A *format_spec* field can also include nested replacement fields in certain
56 positions within it. These nested replacement fields can contain only an
57 argument id; format specifications are not allowed. This allows the formatting
58 of a value to be dynamically specified.
59
60 See the :ref:`formatexamples` section for some examples.
61
62 .. _formatspec:
63
64 Format Specification Mini-Language
65 ==================================
66
67 "Format specifications" are used within replacement fields contained within a
68 format string to define how individual values are presented (see
69 :ref:`syntax`). Each formattable type may define how the format
70 specification is to be interpreted.
71
72 Most built-in types implement the following options for format specifications,
73 although some of the formatting options are only supported by the numeric types.
74
75 The general form of a *standard format specifier* is:
76
77 .. productionlist:: sf
78 format_spec: [[`fill`]`align`][`sign`]["#"]["0"][`width`]["." `precision`][`type`]
79 fill: <a character other than '{' or '}'>
80 align: "<" | ">" | "^"
81 sign: "+" | "-" | " "
82 width: `integer` | "{" [`arg_id`] "}"
83 precision: `integer` | "{" [`arg_id`] "}"
84 type: `int_type` | "a" | "A" | "c" | "e" | "E" | "f" | "F" | "g" | "G" | "L" | "p" | "s"
85 int_type: "b" | "B" | "d" | "o" | "x" | "X"
86
87 The *fill* character can be any Unicode code point other than ``'{'`` or
88 ``'}'``. The presence of a fill character is signaled by the character following
89 it, which must be one of the alignment options. If the second character of
90 *format_spec* is not a valid alignment option, then it is assumed that both the
91 fill character and the alignment option are absent.
92
93 The meaning of the various alignment options is as follows:
94
95 +---------+----------------------------------------------------------+
96 | Option | Meaning |
97 +=========+==========================================================+
98 | ``'<'`` | Forces the field to be left-aligned within the available |
99 | | space (this is the default for most objects). |
100 +---------+----------------------------------------------------------+
101 | ``'>'`` | Forces the field to be right-aligned within the |
102 | | available space (this is the default for numbers). |
103 +---------+----------------------------------------------------------+
104 | ``'^'`` | Forces the field to be centered within the available |
105 | | space. |
106 +---------+----------------------------------------------------------+
107
108 Note that unless a minimum field width is defined, the field width will always
109 be the same size as the data to fill it, so that the alignment option has no
110 meaning in this case.
111
112 The *sign* option is only valid for number types, and can be one of the
113 following:
114
115 +---------+----------------------------------------------------------+
116 | Option | Meaning |
117 +=========+==========================================================+
118 | ``'+'`` | indicates that a sign should be used for both |
119 | | positive as well as negative numbers. |
120 +---------+----------------------------------------------------------+
121 | ``'-'`` | indicates that a sign should be used only for negative |
122 | | numbers (this is the default behavior). |
123 +---------+----------------------------------------------------------+
124 | space | indicates that a leading space should be used on |
125 | | positive numbers, and a minus sign on negative numbers. |
126 +---------+----------------------------------------------------------+
127
128 The ``'#'`` option causes the "alternate form" to be used for the
129 conversion. The alternate form is defined differently for different
130 types. This option is only valid for integer and floating-point types.
131 For integers, when binary, octal, or hexadecimal output is used, this
132 option adds the prefix respective ``"0b"`` (``"0B"``), ``"0"``, or
133 ``"0x"`` (``"0X"``) to the output value. Whether the prefix is
134 lower-case or upper-case is determined by the case of the type
135 specifier, for example, the prefix ``"0x"`` is used for the type ``'x'``
136 and ``"0X"`` is used for ``'X'``. For floating-point numbers the
137 alternate form causes the result of the conversion to always contain a
138 decimal-point character, even if no digits follow it. Normally, a
139 decimal-point character appears in the result of these conversions
140 only if a digit follows it. In addition, for ``'g'`` and ``'G'``
141 conversions, trailing zeros are not removed from the result.
142
143 .. ifconfig:: False
144
145 The ``','`` option signals the use of a comma for a thousands separator.
146 For a locale aware separator, use the ``'L'`` integer presentation type
147 instead.
148
149 *width* is a decimal integer defining the minimum field width. If not
150 specified, then the field width will be determined by the content.
151
152 Preceding the *width* field by a zero (``'0'``) character enables sign-aware
153 zero-padding for numeric types. It forces the padding to be placed after the
154 sign or base (if any) but before the digits. This is used for printing fields in
155 the form '+000000120'. This option is only valid for numeric types and it has no
156 effect on formatting of infinity and NaN.
157
158 The *precision* is a decimal number indicating how many digits should be
159 displayed after the decimal point for a floating-point value formatted with
160 ``'f'`` and ``'F'``, or before and after the decimal point for a floating-point
161 value formatted with ``'g'`` or ``'G'``. For non-number types the field
162 indicates the maximum field size - in other words, how many characters will be
163 used from the field content. The *precision* is not allowed for integer,
164 character, Boolean, and pointer values.
165
166 Finally, the *type* determines how the data should be presented.
167
168 The available string presentation types are:
169
170 +---------+----------------------------------------------------------+
171 | Type | Meaning |
172 +=========+==========================================================+
173 | ``'s'`` | String format. This is the default type for strings and |
174 | | may be omitted. |
175 +---------+----------------------------------------------------------+
176 | none | The same as ``'s'``. |
177 +---------+----------------------------------------------------------+
178
179 The available character presentation types are:
180
181 +---------+----------------------------------------------------------+
182 | Type | Meaning |
183 +=========+==========================================================+
184 | ``'c'`` | Character format. This is the default type for |
185 | | characters and may be omitted. |
186 +---------+----------------------------------------------------------+
187 | none | The same as ``'c'``. |
188 +---------+----------------------------------------------------------+
189
190 The available integer presentation types are:
191
192 +---------+----------------------------------------------------------+
193 | Type | Meaning |
194 +=========+==========================================================+
195 | ``'b'`` | Binary format. Outputs the number in base 2. Using the |
196 | | ``'#'`` option with this type adds the prefix ``"0b"`` |
197 | | to the output value. |
198 +---------+----------------------------------------------------------+
199 | ``'B'`` | Binary format. Outputs the number in base 2. Using the |
200 | | ``'#'`` option with this type adds the prefix ``"0B"`` |
201 | | to the output value. |
202 +---------+----------------------------------------------------------+
203 | ``'d'`` | Decimal integer. Outputs the number in base 10. |
204 +---------+----------------------------------------------------------+
205 | ``'o'`` | Octal format. Outputs the number in base 8. |
206 +---------+----------------------------------------------------------+
207 | ``'x'`` | Hex format. Outputs the number in base 16, using |
208 | | lower-case letters for the digits above 9. Using the |
209 | | ``'#'`` option with this type adds the prefix ``"0x"`` |
210 | | to the output value. |
211 +---------+----------------------------------------------------------+
212 | ``'X'`` | Hex format. Outputs the number in base 16, using |
213 | | upper-case letters for the digits above 9. Using the |
214 | | ``'#'`` option with this type adds the prefix ``"0X"`` |
215 | | to the output value. |
216 +---------+----------------------------------------------------------+
217 | ``'L'`` | Locale-specific format. This is the same as ``'d'``, |
218 | | except that it uses the current locale setting to insert |
219 | | the appropriate number separator characters. |
220 +---------+----------------------------------------------------------+
221 | none | The same as ``'d'``. |
222 +---------+----------------------------------------------------------+
223
224 Integer presentation types can also be used with character and Boolean values.
225 Boolean values are formatted using textual representation, either ``true`` or
226 ``false``, if the presentation type is not specified.
227
228 The available presentation types for floating-point values are:
229
230 +---------+----------------------------------------------------------+
231 | Type | Meaning |
232 +=========+==========================================================+
233 | ``'a'`` | Hexadecimal floating point format. Prints the number in |
234 | | base 16 with prefix ``"0x"`` and lower-case letters for |
235 | | digits above 9. Uses ``'p'`` to indicate the exponent. |
236 +---------+----------------------------------------------------------+
237 | ``'A'`` | Same as ``'a'`` except it uses upper-case letters for |
238 | | the prefix, digits above 9 and to indicate the exponent. |
239 +---------+----------------------------------------------------------+
240 | ``'e'`` | Exponent notation. Prints the number in scientific |
241 | | notation using the letter 'e' to indicate the exponent. |
242 +---------+----------------------------------------------------------+
243 | ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an |
244 | | upper-case ``'E'`` as the separator character. |
245 +---------+----------------------------------------------------------+
246 | ``'f'`` | Fixed point. Displays the number as a fixed-point |
247 | | number. |
248 +---------+----------------------------------------------------------+
249 | ``'F'`` | Fixed point. Same as ``'f'``, but converts ``nan`` to |
250 | | ``NAN`` and ``inf`` to ``INF``. |
251 +---------+----------------------------------------------------------+
252 | ``'g'`` | General format. For a given precision ``p >= 1``, |
253 | | this rounds the number to ``p`` significant digits and |
254 | | then formats the result in either fixed-point format |
255 | | or in scientific notation, depending on its magnitude. |
256 | | |
257 | | A precision of ``0`` is treated as equivalent to a |
258 | | precision of ``1``. |
259 +---------+----------------------------------------------------------+
260 | ``'G'`` | General format. Same as ``'g'`` except switches to |
261 | | ``'E'`` if the number gets too large. The |
262 | | representations of infinity and NaN are uppercased, too. |
263 +---------+----------------------------------------------------------+
264 | ``'L'`` | Locale-specific format. This is the same as ``'g'``, |
265 | | except that it uses the current locale setting to insert |
266 | | the appropriate number separator characters. |
267 +---------+----------------------------------------------------------+
268 | none | Similar to ``'g'``, except that fixed-point notation, |
269 | | when used, has at least one digit past the decimal |
270 | | point. The default precision is as high as needed to |
271 | | represent the particular value. |
272 +---------+----------------------------------------------------------+
273
274 .. ifconfig:: False
275
276 +---------+----------------------------------------------------------+
277 | | The precise rules are as follows: suppose that the |
278 | | result formatted with presentation type ``'e'`` and |
279 | | precision ``p-1`` would have exponent ``exp``. Then |
280 | | if ``-4 <= exp < p``, the number is formatted |
281 | | with presentation type ``'f'`` and precision |
282 | | ``p-1-exp``. Otherwise, the number is formatted |
283 | | with presentation type ``'e'`` and precision ``p-1``. |
284 | | In both cases insignificant trailing zeros are removed |
285 | | from the significand, and the decimal point is also |
286 | | removed if there are no remaining digits following it. |
287 | | |
288 | | Positive and negative infinity, positive and negative |
289 | | zero, and nans, are formatted as ``inf``, ``-inf``, |
290 | | ``0``, ``-0`` and ``nan`` respectively, regardless of |
291 | | the precision. |
292 | | |
293 +---------+----------------------------------------------------------+
294
295 The available presentation types for pointers are:
296
297 +---------+----------------------------------------------------------+
298 | Type | Meaning |
299 +=========+==========================================================+
300 | ``'p'`` | Pointer format. This is the default type for |
301 | | pointers and may be omitted. |
302 +---------+----------------------------------------------------------+
303 | none | The same as ``'p'``. |
304 +---------+----------------------------------------------------------+
305
306 .. _formatexamples:
307
308 Format Examples
309 ===============
310
311 This section contains examples of the format syntax and comparison with
312 the printf formatting.
313
314 In most of the cases the syntax is similar to the printf formatting, with the
315 addition of the ``{}`` and with ``:`` used instead of ``%``.
316 For example, ``"%03.2f"`` can be translated to ``"{:03.2f}"``.
317
318 The new format syntax also supports new and different options, shown in the
319 following examples.
320
321 Accessing arguments by position::
322
323 fmt::format("{0}, {1}, {2}", 'a', 'b', 'c');
324 // Result: "a, b, c"
325 fmt::format("{}, {}, {}", 'a', 'b', 'c');
326 // Result: "a, b, c"
327 fmt::format("{2}, {1}, {0}", 'a', 'b', 'c');
328 // Result: "c, b, a"
329 fmt::format("{0}{1}{0}", "abra", "cad"); // arguments' indices can be repeated
330 // Result: "abracadabra"
331
332 Aligning the text and specifying a width::
333
334 fmt::format("{:<30}", "left aligned");
335 // Result: "left aligned "
336 fmt::format("{:>30}", "right aligned");
337 // Result: " right aligned"
338 fmt::format("{:^30}", "centered");
339 // Result: " centered "
340 fmt::format("{:*^30}", "centered"); // use '*' as a fill char
341 // Result: "***********centered***********"
342
343 Dynamic width::
344
345 fmt::format("{:<{}}", "left aligned", 30);
346 // Result: "left aligned "
347
348 Dynamic precision::
349
350 fmt::format("{:.{}f}", 3.14, 1);
351 // Result: "3.1"
352
353 Replacing ``%+f``, ``%-f``, and ``% f`` and specifying a sign::
354
355 fmt::format("{:+f}; {:+f}", 3.14, -3.14); // show it always
356 // Result: "+3.140000; -3.140000"
357 fmt::format("{: f}; {: f}", 3.14, -3.14); // show a space for positive numbers
358 // Result: " 3.140000; -3.140000"
359 fmt::format("{:-f}; {:-f}", 3.14, -3.14); // show only the minus -- same as '{:f}; {:f}'
360 // Result: "3.140000; -3.140000"
361
362 Replacing ``%x`` and ``%o`` and converting the value to different bases::
363
364 fmt::format("int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
365 // Result: "int: 42; hex: 2a; oct: 52; bin: 101010"
366 // with 0x or 0 or 0b as prefix:
367 fmt::format("int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}", 42);
368 // Result: "int: 42; hex: 0x2a; oct: 052; bin: 0b101010"
369
370 Padded hex byte with prefix and always prints both hex characters::
371
372 fmt::format("{:#04x}", 0);
373 // Result: "0x00"
374
375 Box drawing using Unicode fill::
376
377 fmt::print(
378 "┌{0:─^{2}}┐\n"
379 "│{1: ^{2}}│\n"
380 "└{0:─^{2}}┘\n", "", "Hello, world!", 20);
381
382 prints::
383
384 ┌────────────────────┐
385 │ Hello, world! │
386 └────────────────────┘
387
388 Using type-specific formatting::
389
390 #include <fmt/chrono.h>
391
392 auto t = tm();
393 t.tm_year = 2010 - 1900;
394 t.tm_mon = 6;
395 t.tm_mday = 4;
396 t.tm_hour = 12;
397 t.tm_min = 15;
398 t.tm_sec = 58;
399 fmt::print("{:%Y-%m-%d %H:%M:%S}", t);
400 // Prints: 2010-08-04 12:15:58
401
402 Using the comma as a thousands separator::
403
404 #include <fmt/locale.h>
405
406 auto s = fmt::format(std::locale("en_US.UTF-8"), "{:L}", 1234567890);
407 // s == "1,234,567,890"
408
409 .. ifconfig:: False
410
411 Nesting arguments and more complex examples::
412
413 >>> for align, text in zip('<^>', ['left', 'center', 'right']):
414 ... '{0:{fill}{align}16}") << text, fill=align, align=align)
415 ...
416 'left<<<<<<<<<<<<'
417 '^^^^^center^^^^^'
418 '>>>>>>>>>>>right'
419 >>>
420 >>> octets = [192, 168, 0, 1]
421 Format("{:02X}{:02X}{:02X}{:02X}") << *octets)
422 'C0A80001'
423 >>> int(_, 16)
424 3232235521
425 >>>
426 >>> width = 5
427 >>> for num in range(5,12):
428 ... for base in 'dXob':
429 ... print('{0:{width}{base}}") << num, base=base, width=width), end=' ')
430 ... print()
431 ...
432 5 5 5 101
433 6 6 6 110
434 7 7 7 111
435 8 8 10 1000
436 9 9 11 1001
437 10 A 12 1010
438 11 B 13 1011