CppCMS
|
a printf like class that allows type-safe and locale aware message formatting More...
#include <booster/booster/locale/format.h>
Classes | |
class | format_guard |
Public Types | |
typedef CharType | char_type |
Underlying character type. | |
typedef basic_message< char_type > | message_type |
typedef std::basic_string < CharType > | string_type |
typedef std::basic_ostream < CharType > | stream_type |
output stream type for this type of character | |
Public Member Functions | |
basic_format (string_type format_string) | |
basic_format (message_type const &trans) | |
template<typename Formattible > | |
basic_format & | operator% (Formattible const &object) |
string_type | str (std::locale const &loc=std::locale()) const |
void | write (stream_type &out) const |
a printf like class that allows type-safe and locale aware message formatting
This class creates a formatted message similar to printf or boost::format and receives formatted entries via operator %.
For example
cout << format("Hello {1}, you are {2} years old") % name % age << endl;
Formatting is enclosed between curly brackets {
}
and defined by a comma separated list of flags in the format key[=value] value may also be text included between single quotes '
that is used for special purposes where inclusion of non-ASCII text is allowed
Including of literal {
and }
is possible by specifying double brackets {{
and }}
accordingly.
For example:
The special key -- a number without a value defines the position of an input parameter. List of keys:
[0-9]+ -- digits, the index of a formatted parameter -- mandatory key.num
or number
-- format a number. Optional values are:hex
-- display hexadecimal numberoct
-- display in octal formatsci
or scientific
-- display in scientific formatfix
or fixed
-- display in fixed formatnumber=sci
cur
or currency
-- format currency. Optional values are:iso
-- display using ISO currency symbol.nat
or national
-- display using national currency symbol.per
or percent
-- format percent value.date
, time
, datetime
or dt
-- format date, time or date and time. Optional values are:s
or short
-- display in short formatm
or medium
-- display in medium format.l
or long
-- display in long format.f
or full
-- display in full format.ftime
with string (quoted) parameter -- display as with strftime
see, as::ftime
manipulatorspell
or spellout
-- spell the number.ord
or ordinal
-- format ordinal number (1st, 2nd... etc)left
or <
-- align to left.right
or >
-- align to right.width
or w
-- set field width (requires parameter).precision
or p
-- set precision (requires parameter).locale
-- with parameter -- switch locale for current operation. This command generates locale with formatting facets giving more fine grained control of formatting. For example: timezone
or tz
-- the name of the timezone to display the time in. For example:local
- display the time in local timegmt
- display the time in UTC time scale Invalid formatting strings are slightly ignored. This would prevent from translator to crash the program in unexpected location.
typedef basic_message<char_type> booster::locale::basic_format< CharType >::message_type |
The translation message type
typedef std::basic_string<CharType> booster::locale::basic_format< CharType >::string_type |
string type for this type of character
booster::locale::basic_format< CharType >::basic_format | ( | string_type | format_string | ) | [inline] |
Create a format class for format_string
booster::locale::basic_format< CharType >::basic_format | ( | message_type const & | trans | ) | [inline] |
Create a format class using message trans. The message if translated first according to the rules of target locale and then interpreted as format string
basic_format& booster::locale::basic_format< CharType >::operator% | ( | Formattible const & | object | ) | [inline] |
Add new parameter to format list. The object should be a type with defined expression out << object where out
is std::basic_ostream
.
string_type booster::locale::basic_format< CharType >::str | ( | std::locale const & | loc = std::locale() | ) | const [inline] |
Format a string using a locale loc
References booster::locale::basic_format< CharType >::write().
void booster::locale::basic_format< CharType >::write | ( | stream_type & | out | ) | const [inline] |
write a formatted string to output stream out using out's locale
References booster::locale::ios_info::domain_id(), booster::locale::ios_info::get(), and booster::locale::basic_message< CharType >::str().
Referenced by booster::locale::operator<<(), and booster::locale::basic_format< CharType >::str().