xtd 0.2.0
format_class_with_specified_formating.cpp

Show how to use format xtd::format class with specified formatting.

#include <xtd/console>
#include <xtd/environment>
#include <xtd/format>
using namespace xtd;
class character : public iformatable {
public:
character() = default;
character(const string& name, const string& rank) noexcept : name_(name), rank_(rank) {}
const string& name() const noexcept {return name_;}
const string& rank() const noexcept {return rank_;}
string to_string() const noexcept {return to_string("", std::locale {});}
string to_string(const string& format, const std::locale& loc) const override {
auto fmt = string::is_empty(format) ? "F" : format;
if (fmt == "F") return name_ + " (" + rank_ + ")";
if (fmt == "N") return name_;
if (fmt == "R") return rank_;
throw format_exception {};
}
private:
string name_;
string rank_;
};
enum class cap {title, middle, end};
auto print_character(const string& text, const character& value, cap c) {
if (c == cap::title)
<< "┌───────────────────────────────────────────────┬────────────┬──────────────────────────────────────────┐" << environment::new_line
<< "│ character │ format │ representation │" << environment::new_line
<< "├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤" << environment::new_line;
console::out << "│ " << text.pad_right(45) << " │ {} │ " << format("{}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(45) << " │ {:F} │ " << format("{:F}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(45) << " │ {:N} │ " << format("{:N}", value).pad_right(40) << " │" << environment::new_line;
console::out << "│ " << text.pad_right(45) << " │ {:R} │ " << format("{:R}", value).pad_right(40) << " │" << environment::new_line;
if (c != cap::end)
console::out << "├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤" << environment::new_line;
else
console::out << "└───────────────────────────────────────────────┴────────────┴──────────────────────────────────────────┘" << environment::new_line;
}
auto main() -> int {
print_character("{}", character {}, cap::title);
print_character("{\"Jean-Luc Picard\", \"Captain\"}", character {"Jean-Luc Picard", "Captain"}, cap::middle);
print_character("{\"William Riker\", \"Commander\"}", character {"William Riker", "Commander"}, cap::middle);
print_character("{\"Data\", \"Commander\"}", character {"Data", "Commander"}, cap::middle);
print_character("{\"Beverly Crusher\", \"Commander\"}", character {"Beverly Crusher", "Commander"}, cap::middle);
print_character("{\"Geordi La Forge\", \"Lieutenant Commander\"}", character {"Geordi La Forge", "Lieutenant Commander"}, cap::middle);
print_character("{\"Worf\", \"Lieutenant Commander\"}", character {"Worf", "Lieutenant Commander"}, cap::middle);
print_character("{\"Tasha Yar\", \"Lieutenant\"}", character {"Tasha Yar", "Lieutenant"}, cap::end);
}
// This code produces the following output :
//
// ┌───────────────────────────────────────────────┬────────────┬──────────────────────────────────────────┐
// │ character │ format │ representation │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {} │ {} │ () │
// │ {} │ {:F} │ () │
// │ {} │ {:N} │ │
// │ {} │ {:R} │ │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"Jean-Luc Picard", "Captain"} │ {} │ Jean-Luc Picard (Captain) │
// │ {"Jean-Luc Picard", "Captain"} │ {:F} │ Jean-Luc Picard (Captain) │
// │ {"Jean-Luc Picard", "Captain"} │ {:N} │ Jean-Luc Picard │
// │ {"Jean-Luc Picard", "Captain"} │ {:R} │ Captain │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"William Riker", "Commander"} │ {} │ William Riker (Commander) │
// │ {"William Riker", "Commander"} │ {:F} │ William Riker (Commander) │
// │ {"William Riker", "Commander"} │ {:N} │ William Riker │
// │ {"William Riker", "Commander"} │ {:R} │ Commander │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"Data", "Commander"} │ {} │ Data (Commander) │
// │ {"Data", "Commander"} │ {:F} │ Data (Commander) │
// │ {"Data", "Commander"} │ {:N} │ Data │
// │ {"Data", "Commander"} │ {:R} │ Commander │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"Beverly Crusher", "Commander"} │ {} │ Beverly Crusher (Commander) │
// │ {"Beverly Crusher", "Commander"} │ {:F} │ Beverly Crusher (Commander) │
// │ {"Beverly Crusher", "Commander"} │ {:N} │ Beverly Crusher │
// │ {"Beverly Crusher", "Commander"} │ {:R} │ Commander │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"Geordi La Forge", "Lieutenant Commander"} │ {} │ Geordi La Forge (Lieutenant Commander) │
// │ {"Geordi La Forge", "Lieutenant Commander"} │ {:F} │ Geordi La Forge (Lieutenant Commander) │
// │ {"Geordi La Forge", "Lieutenant Commander"} │ {:N} │ Geordi La Forge │
// │ {"Geordi La Forge", "Lieutenant Commander"} │ {:R} │ Lieutenant Commander │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"Worf", "Lieutenant Commander"} │ {} │ Worf (Lieutenant Commander) │
// │ {"Worf", "Lieutenant Commander"} │ {:F} │ Worf (Lieutenant Commander) │
// │ {"Worf", "Lieutenant Commander"} │ {:N} │ Worf │
// │ {"Worf", "Lieutenant Commander"} │ {:R} │ Lieutenant Commander │
// ├───────────────────────────────────────────────┼────────────┼──────────────────────────────────────────┤
// │ {"Tasha Yar", "Lieutenant"} │ {} │ Tasha Yar (Lieutenant) │
// │ {"Tasha Yar", "Lieutenant"} │ {:F} │ Tasha Yar (Lieutenant) │
// │ {"Tasha Yar", "Lieutenant"} │ {:N} │ Tasha Yar │
// │ {"Tasha Yar", "Lieutenant"} │ {:R} │ Lieutenant │
// └───────────────────────────────────────────────┴────────────┴──────────────────────────────────────────┘
bool is_empty() const noexcept
Definition basic_string.hpp:1503
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.
The exception that is thrown when the format of an argument does not meet the parameter specification...
Definition format_exception.hpp:19
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:35
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
@ c
The C key.
Definition console_key.hpp:92
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
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
const_iterator end() const
Returns an iterator to the end.
Definition read_only_span.hpp:213