proxygen
|
folly/Format.h
provides a fast, powerful, type-safe, flexible facility for formatting text, using a specification language similar to Python's str.format. By default, it can format strings, numbers (integral and floating point), and dynamically-typed folly::dynamic
objects, and can extract values from random-access containers and string-keyed maps. In many cases, format
is faster than sprintf
as well as being fully type-safe.
To use format
, you need to be using gcc 4.6 or later. You'll want to include folly/Format.h
.
Here are some code samples to get started:
Format string (format
): "{" [arg_index] ["[" key "]"] [":" format_spec] "}"
arg_index
: index of argument to format; default = next argument. Note that a format string may have either default argument indexes or non-default argument indexes, but not both (to avoid confusion).key
: if the argument is a container (C-style array or pointer, std::array
, vector, deque, map), you may use this to select the element to format; works with random-access sequences and integer- and string-keyed maps. Multiple level keys work as well, with components separated with "."; for example, given map<string, map<string, string>> m
, {[foo.bar]}
selects m["foo"]["bar"]
.format_spec
: format specification, see belowFormat string (vformat
): "{" [ key ] [":" format_spec] "}"
key
: select the argument to format from the container argument; works with random-access sequences and integer- and string-keyed maps. Multiple level keys work as well, with components separated with "."; for example, given map<string, map<string, string>> m
, {foo.bar}
selects m["foo"]["bar"]
.format_spec
: format specification, see belowFormat specification: [[fill] align] [sign] ["#"] ["0"] [width] [","] ["." precision] ["."] [type]
fill
(may only be specified if align
is also specified): pad with this character ('' (space) or '0
' (zero) might be useful; space is default)align
: one of '<
', '>
', '=
', '^
':<
': left-align (default for most objects)>
': right-align (default for numbers)=
': pad after sign, but before significant digits; used to print -0000120
; only valid for numbers^
': centersign
: one of '+
', '-
', ' ' (space) (only valid for numbers)+
': output '+
' if positive or zero, '-
' if negative-
': output '-
' if negative, nothing otherwise (default)-
' if negative#
': output base prefix (0
for octal, 0b
or 0B
for binary, 0x
or 0X
for hexadecimal; only valid for integers)0
': 0-pad after sign, same as specifying "`0=`" as the fill
and align
parameters (only valid for numbers)width
: minimum field width. May be '*
' to indicate that the field width is given by an argument. Defaults to the next argument (preceding the value to be formatted) but an explicit argument index may be given following the '*
'. Not supported in vformat()
.,
' (comma): output comma as thousands' separator (only valid for integers, and only for decimal output)precision
(not allowed for integers):f
' or 'F
' presentation) or number of significant digits ('g
' or 'G
').
' (when used after precision or in lieu of precison): Forces a trailing decimal point to make it clear this is a floating point value.type
: presentation format, see belowPresentation formats:
folly::StringPiece
, std::string
, folly::fbstring
, const char*
):s
' (default)b
': output in binary (base 2) ("`0b`" prefix if '#
' specified)B
': output in binary (base 2) ("`0B`" prefix if '#
' specified)c
': output as a character (cast to char
)d
': output in decimal (base 10) (default)o
': output in octal (base 8)O
': output in octal (base 8) (same as 'o
')x
': output in hexadecimal (base 16) (lower-case digits above 9)X
': output in hexadecimal (base 16) (upper-case digits above 9)n
': locale-aware output (currently same as 'd
')bool
:char
:c
' instead of 'd
'float
, double
; long double
is not implemented):e
': scientific notation using 'e
' as exponent characterE
': scientific notation using 'E
' as exponent characterf
': fixed pointF
': fixed point (same as 'f
')g
': general; use either 'f
' or 'e
' depending on magnitude (default)G
': general; use either 'f
' or 'E
' depending on magnituden
': locale-aware version of 'g
' (currently same as 'g
')%
': percentage: multiply by 100 then display as 'f
'You can extend format
for your own class by providing a specialization for folly::FormatValue
. See folly/Format.h
and folly/FormatArg.h
for details, and the existing specialization for folly::dynamic
in folly/dynamic-inl.h
for an implementation example.