FOLLY: Format.h














































FOLLY: Format.h



folly/Format.h

It provides a fast, strong and flexible facility for formatting text. It uses a specification language. It can format strings, numbers and dynamically-typed folly::dynamic objects. It can extract values from random-access containers.

 

Format string syntax

Format string (format): "{" [arg_index] ["[" key "]"] [":" format_spec] "}"

 

arg_index: index of argument to format; default = next argument

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.

Format 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:  '<', '>', '=', '^':

'<': left-align

'>': right-align, it is default for numbers.

'=': pad after sign, but before significant digits; only valid for numbers

'^': center

sign: one of '+', '-' , ‘ ’.

'+': output '+' if positive or zero, '-' if negative

'-': output '-' if negative, nothing otherwise (default)

' ' (space): output ' ' blank space if positive or zero, '-' 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 but an explicit argument index may be given following the '*'.

',' (comma): output comma as thousands' separator

 

precision (not allowed for integers):

for floating point values, number of digits after decimal point ('f' or 'F' presentation) or number of significant digits ('g' or 'G')

for others, maximum field size (truncate subsequent characters)

'.' (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 below

 

Presentation formats:

Strings (folly::StringPiece, std::string, folly::fbstring, const char*):

's' (default)

Integers:

'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:

default: output "true" or "false" as strings

integer presentations allowed as well

char:

same as other integers, but default is 'c' instead of 'd'

Floating point (float, double; long double is not implemented):

'e': scientific notation using 'e' as exponent character

'E': scientific notation using 'E' as exponent character

'f': fixed point

'F': 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 magnitude

'n': locale-aware version of 'g' (currently same as 'g')

'%': percentage: multiply by 100 then display as 'f'


Comments