A C++ template library for embedded applications
MIT licensed
Join the ETL community
Designed and
maintained by
John Wellbelove
Like the ETL? Become a patron!
or

basic_format_spec

format_spec

wformat_spec

u16format_spec

u32format_spec


A template class and four typedefs that allow a specification for string formatting functions and streams.
It defines specifications for strings, bool, integrals, floating point and pointers.
Used in conjunction with etl::to_string and string streams

The class stores the following specifications:
base           The number base. Predefined settings for binary, octal, decimal and hex.
width          The total minimum field width for the value's text representation.
precision      The total number of decimal places.
upper_case     If true then numerical digits for bases >10 are in upper case.
left_justified If true then the text representation is left justified with padding up to the field width on the right.
boolalpha      If true then Boolean values are rendered as "true" and "false".
showbase       If true then binary, octal and hex values are prefixed with the base tag.
fill           Determines the character used for the padding for width.
____________________________________________________________________________________________________
basic_format_spec

template <typename TString>
class basic_format_spec
____________________________________________________________________________________________________
basic_format_spec()
The default constructor.
Sets:-
base           : 10
width          : 0
precision      : 0
upper case     : false
left justified : false
boolalpha      : false
show base
fill           : ' '
____________________________________________________________________________________________________
basic_format_spec& base(const uint32_t b)
Sets the base to b.
____________________________________________________________________________________________________
basic_format_spec& binary()
Sets the base to binary.
____________________________________________________________________________________________________
basic_format_spec& octal()
Sets the base to octal.
____________________________________________________________________________________________________
basic_format_spec& hex()
Sets the base to hexadecimal.
____________________________________________________________________________________________________
basic_format_spec& octal()
Sets the base to octal.
____________________________________________________________________________________________________
uint32_t get_base() const
Returns the current setting for base.
____________________________________________________________________________________________________
basic_format_spec& width(const uint32_t w)
Sets the width to w.
____________________________________________________________________________________________________
uint32_t get_width() const
Returns the current width value.
____________________________________________________________________________________________________
basic_format_spec& precision(const uint32_t p)
Sets the precision to p.
____________________________________________________________________________________________________
uint32_t get_precision() const
Returns the current precision value.
____________________________________________________________________________________________________
basic_format_spec& upper_case(const bool b)
Sets the upper case flag  to b.
Only affects numerical output such as hexadecimal.
____________________________________________________________________________________________________
bool is_upper_case() const
Returns true if the upper case flag is set.
____________________________________________________________________________________________________
basic_format_spec& show_base(const bool b)
Sets the showbase flag  to b.
Only affects binary and hexadecimal output.
____________________________________________________________________________________________________
bool is_show_base() const
Returns true if the showbase flag is set.
____________________________________________________________________________________________________
basic_format_spec& fill(const typename TString::value_type c)
Sets the fill character to c.
____________________________________________________________________________________________________
typename TString::value_type get_fill() const
Returns the fill character.
____________________________________________________________________________________________________
basic_format_spec& left()
Sets the left justified flag  to true.
____________________________________________________________________________________________________
bool is_left() const
Returns true if the left justified flag is set.
____________________________________________________________________________________________________
basic_format_spec& right()

Sets the left justified flag  to false.
____________________________________________________________________________________________________
bool is_right() const
Returns true if the left justified flag is clear.
____________________________________________________________________________________________________
basic_format_spec& boolalpha(bool b)
Sets the bool alpha flag  to b.
____________________________________________________________________________________________________
bool is_boolalpha() const
Returns true if the bool alpha flag is set.
____________________________________________________________________________________________________
Typedefs

typedef etl::basic_format_spec<etl::istring>    format_spec;
typedef etl::basic_format_spec<etl::iwstring>   wformat_spec;
typedef etl::basic_format_spec<etl::iu16string> u16format_spec;
typedef etl::basic_format_spec<etl::iu32string> u32format_spec;
____________________________________________________________________________________________________
Stream manipulators
These manipulators are used in conjunction with the ETL's string streams.
Streams may also use a format spec as a manipulator.

setbase(uint32_t base)
Sets the base for numerical output to base.
____________________________________________________________________________________________________
setw(uint32_t width)
Sets the format width to width.
____________________________________________________________________________________________________
template <typename TChar>
setfill(TChar fill)
Sets the fill character to fill.
____________________________________________________________________________________________________
setprecision(uint32_t precision)
Sets the number of decimal places to precision.
____________________________________________________________________________________________________
bin
Sets the base for numerical output to binary.
____________________________________________________________________________________________________
oct
Sets the base for numerical output to octal.
____________________________________________________________________________________________________
dec
Sets the base for numerical output to decimal.
____________________________________________________________________________________________________
hex
Sets the base for numerical output to hexadecimal.
____________________________________________________________________________________________________
left
Sets the alignment to left.
____________________________________________________________________________________________________
right
Sets the alignment to right.
____________________________________________________________________________________________________
boolalpha
Sets the boolalpha flag to true.
____________________________________________________________________________________________________
noboolalpha
Sets the boolalpha flag to false.
____________________________________________________________________________________________________
uppercase
Sets the uppercase flag to true.
____________________________________________________________________________________________________
nouppercase
Sets the uppercase flag to false.
____________________________________________________________________________________________________
showbase
Sets the showbase flag to true.
____________________________________________________________________________________________________
noshowbase
Sets the showbase flag to false.
____________________________________________________________________________________________________
Example (to_string)

etl::format_spec format;

// Format as a hex character, minimum fill width of 8, fill with zeros.
format.hex().width(8).fill('0');

etl::string<8> text;

// 'text' is set to "00123456"
etl::to_string(1193046, text, format);

// Format minimum fill width of 8, fill with space and three decimal digits.
format.width(8).fill(' ').precision(3);

// 'text' is set to "   3.142"
etl::to_string(3.1415, text, format);
____________________________________________________________________________________________________
Example (string_stream)

etl::string<20> buffer;
etl::string_stream stream(buffer);

// Manipulators
stream << etl::showbase
       << etl::bin << 123456 << " "
       << etl::oct << 123456 << " "
       << etl::dec << 123456 << " "
       << etl::hex << 123456;

The generated string:-

0b11110001001000000 0361100 123456 0x1e240

// Format spec
etl::format_spec format;
format.show_base(true).hex();

stream << format << 123456;

The generated string:-

0x1e240

format_spec.h