README (4311B)
1 A format string consists of a string of literal characters, to be printed 2 verbatim, and format sequences, which describe how to format arguments from a 3 set of variadic parameters for printing. 4 5 A format sequence is enclosed in curly braces "{}". An empty sequence takes the 6 next argument from the parameter list, in order. A specific parameter can be 7 selected by indexing it from zero: "{0}", "{1}", and so on. To print "{", use 8 "{{", and for "}", use "}}". 9 10 There are two ways to specify how an argument will be formatted: inline format 11 modifiers, and parametric format modifiers. 12 13 Inline format modifiers are a series of characters within a format sequence. 14 You can use a colon to add format modifiers; for example, "{:x}" will format an 15 argument in hexadecimal, and "{3:-10}" will left-align the 4th argument (zero 16 indexed) to at least 10 characters. 17 18 Format modifiers can be written in any order, and can also be repeated. If a 19 modifier is repeated (or two conflicting modifiers are given, such as both "x" 20 and "X") the one furthest to the right will be used. 21 22 A format modifier can be any of the following: 23 - A number N: Sets the width to N. If the value would otherwise be shorter than 24 N runes, insert padding characters in order to make it N runes long. By 25 default, the value is right-aligned, with padding inserted on the left side, 26 and the padding character is " " (a space). 27 - "-": Left-align the value, inserting padding characters inserted on the right 28 side of the value in order to meet the width requirement. 29 - "=": Center-align the value, inserting the same amount of padding on the left 30 as on the right. If an odd number of padding characters need to be placed, the 31 extra one will be on the left of the value. 32 - "_" followed by a rune: Use the given rune as the padding character rather 33 than the default of " " (a space). 34 - " " (a space): Insert a space before positive integers, where "-" would be if 35 it were negative. 36 - "+": Insert a "+" before positive integers. 37 - "x": Format numbers in lowercase hexadecimal. 38 - "X": Format numbers in uppercase hexadecimal. 39 - "o": Format numbers in octal. 40 - "b": Format numbers in binary. 41 - "e": Format floats in scientific notation. 42 - "f": Format floats in fixed-point notation. 43 - "g": Format floats in whichever of scientific and fixed-point notation is 44 shortest. This is the default. 45 - "F" followed by "s": Use a sign for both positive and negative numbers. 46 - "F" followed by ".": Always include at least one digit after the decimal 47 point. 48 - "F" followed by "U": Uppercase INFINITY and NAN. 49 - "F" followed by "E": Uppercase exponent symbols (E and P rather than e and p). 50 - "F" followed by "S": Use a sign for both positive and negative exponents. 51 - "F" followed by "2": Show at least two digits of the exponent. 52 - "." followed by a number N: Sets the precision to N. Integers will be 53 left-padded with "0"s between the sign and the number itself. Strings 54 will be truncated to N runes. Floats will only include up to N digits after 55 the decimal point. 56 57 Some inline modifier examples: 58 59 fmt::printf("hello {}", "world"); // "hello world" 60 fmt::printf("{1} {0}", "hello", "world"); // "world hello" 61 fmt::printf("{:x} {:X}", 51966, 61453); // "cafe F00D" 62 fmt::printf("{:-5}", 42); // "42 " 63 fmt::printf("{:5}", 42); // " 42" 64 fmt::printf("{:.5}", 42); // "00042" 65 66 A parametric format modifier is a secondary argument from the parameter list, 67 which is a pointer to an instance of [[mods]]. This modifier parameter 68 describes how the primary formattable argument is formatted. 69 70 A parametric format sequence of this sort takes the form of "{i%j}", where i is 71 the formattable parameter index, j is the modifiers parameter index, and i & j 72 are optional. If either i or j aren't explicitly provided by the user, they 73 will evaluate to the index of the next unused argument. 74 75 Some parametric modifier examples: 76 77 // "hello world hello" 78 fmt::printf("{%} {%} {0%1}", // evaluates to "{0%1} {2%3} {0%1}" 79 "hello", &fmt::mods { ... }, 80 "world", &fmt::mods { ... }); 81 82 // "|hello| world|0000000123|BEEF|" 83 fmt::printf("|{%}|{%}|{%}|{%}|", 84 "hello", &fmt::mods { ... }, 85 "world", &fmt::mods { pad = ' ', width = 10, ... }, 86 123, &fmt::mods { prec = 10, ... }, 87 0xBEEF, &fmt::mods { base = strconv::base::HEX, ... }); 88