xtd 0.2.0
format_character.cpp

Show how to use format xtd::format class with characters.

#include <xtd/console>
#include <xtd/environment>
#include <xtd/format>
using namespace xtd;
enum class cap {title, middle, end};
auto print_character(const u32string& text, char32 value, cap c) {
if (c == cap::title)
<< "┌───────────┬────────────┬──────────────────────────────────┐" << environment::new_line
<< "│ char │ format │ representation │" << environment::new_line
<< "├───────────┼────────────┼──────────────────────────────────┤" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {} │ " << format(U"{}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:g} │ " << format(U"{:g}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:G} │ " << format(U"{:G}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:d} │ " << format(U"{:d}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:D} │ " << format(U"{:D}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:x} │ " << format(U"{:x}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:X} │ " << format(U"{:X}", value).pad_right(32) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(9) << " │ {:ANY_FMT} │ " << format(U"{:ANY_FMT}", value).pad_right(32) << " │" << environment::new_line;
if (c != cap::end)
console::out << "├───────────┼────────────┼──────────────────────────────────┤" << environment::new_line;
else
console::out << "└───────────┴────────────┴──────────────────────────────────┘" << environment::new_line;
}
auto main() -> int {
print_character(string {"a"}, 'a', cap::title);
print_character(string {u8"1"}, u8'1', cap::middle);
print_character(string {L"\x1F603"}, L'\x1F603', cap::middle);
print_character(string {u"\u4eb0"}, u'\u4eb0', cap::middle);
print_character(string {U"\U0001F428"}, U'\U0001F428', cap::end);
}
// This code produces the following output :
//
// ┌───────────┬────────────┬──────────────────────────────────┐
// │ char │ format │ representation │
// ├───────────┼────────────┼──────────────────────────────────┤
// │ a │ {} │ a │
// │ a │ {:g} │ a │
// │ a │ {:G} │ a │
// │ a │ {:d} │ a │
// │ a │ {:D} │ a │
// │ a │ {:x} │ a │
// │ a │ {:X} │ a │
// │ a │ {:ANY_FMT} │ a │
// ├───────────┼────────────┼──────────────────────────────────┤
// │ 1 │ {} │ 1 │
// │ 1 │ {:g} │ 1 │
// │ 1 │ {:G} │ 1 │
// │ 1 │ {:d} │ 1 │
// │ 1 │ {:D} │ 1 │
// │ 1 │ {:x} │ 1 │
// │ 1 │ {:X} │ 1 │
// │ 1 │ {:ANY_FMT} │ 1 │
// ├───────────┼────────────┼──────────────────────────────────┤
// │ 😃 │ {} │ 😃 │
// │ 😃 │ {:g} │ 😃 │
// │ 😃 │ {:G} │ 😃 │
// │ 😃 │ {:d} │ 😃 │
// │ 😃 │ {:D} │ 😃 │
// │ 😃 │ {:x} │ 😃 │
// │ 😃 │ {:X} │ 😃 │
// │ 😃 │ {:ANY_FMT} │ 😃 │
// ├───────────┼────────────┼──────────────────────────────────┤
// │ 亰 │ {} │ 亰 │
// │ 亰 │ {:g} │ 亰 │
// │ 亰 │ {:G} │ 亰 │
// │ 亰 │ {:d} │ 亰 │
// │ 亰 │ {:D} │ 亰 │
// │ 亰 │ {:x} │ 亰 │
// │ 亰 │ {:X} │ 亰 │
// │ 亰 │ {:ANY_FMT} │ 亰 │
// ├───────────┼────────────┼──────────────────────────────────┤
// │ 🐨 │ {} │ 🐨 │
// │ 🐨 │ {:g} │ 🐨 │
// │ 🐨 │ {:G} │ 🐨 │
// │ 🐨 │ {:d} │ 🐨 │
// │ 🐨 │ {:D} │ 🐨 │
// │ 🐨 │ {:x} │ 🐨 │
// │ 🐨 │ {:X} │ 🐨 │
// │ 🐨 │ {:ANY_FMT} │ 🐨 │
// └───────────┴────────────┴──────────────────────────────────┘
static int32 output_code_page()
Gets the code page the console uses to write output.
static std::ostream out
Gets the standard output stream. A std::basic_ostream<char_t> that represents the standard output str...
Definition console.hpp:52
static xtd::string new_line() noexcept
Gets the newline string defined for this environment.
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:20
xtd::basic_string< xtd::char32 > u32string
Represents text as a sequence of UTF-32 code units.
Definition __string_definitions.hpp:65
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:26
@ c
The C key.
Definition console_key.hpp:92
@ u
The U key.
Definition console_key.hpp:128
Contains classes that represent ASCII and Unicode character encodings; abstract base classes for conv...
Definition basic_string_builder.hpp:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213