Browse Source

Util: Add integral formatting to char and bool types

master
Riyyi 2 years ago
parent
commit
448ad42005
  1. 29
      src/util/format/formatter.cpp
  2. 5
      src/util/format/formatter.h

29
src/util/format/formatter.cpp

@ -21,13 +21,38 @@ namespace Util::Format {
template<> template<>
void Formatter<char>::format(Builder& builder, char value) const void Formatter<char>::format(Builder& builder, char value) const
{ {
builder.putCharacter(value); if (specifier.type != PresentationType::None && specifier.type != PresentationType::Character) {
// "Type char is a distinct type that has an implementation-defined
// choice of “signed char” or “unsigned char” as its underlying type."
// http://eel.is/c++draft/basic#fundamental
Formatter<signed char> formatter { .specifier = specifier };
return formatter.format(builder, static_cast<signed char>(value));
}
Formatter<std::string_view> formatter { .specifier = specifier };
return formatter.format(builder, { &value, 1 });
} }
template<> template<>
void Formatter<bool>::format(Builder& builder, bool value) const void Formatter<bool>::format(Builder& builder, bool value) const
{ {
builder.putString(value ? "true" : "false"); switch (specifier.type) {
case PresentationType::Binary:
case PresentationType::BinaryUppercase:
case PresentationType::Character:
case PresentationType::Decimal:
case PresentationType::Octal:
case PresentationType::Hex:
case PresentationType::HexUppercase: {
Formatter<uint8_t> formatter { .specifier = specifier };
return formatter.format(builder, static_cast<uint8_t>(value));
}
default:
break;
};
Formatter<std::string_view> formatter { .specifier = specifier };
formatter.format(builder, value ? "true" : "false");
} }
// String // String

5
src/util/format/formatter.h

@ -68,7 +68,10 @@ struct Formatter {
constexpr void parse(Parser& parser) constexpr void parse(Parser& parser)
{ {
if (std::is_same_v<T, char>) { if constexpr (std::is_same_v<T, char>) {
parser.parseSpecifier(specifier, Parser::ParameterType::Char);
}
else if (std::is_same_v<T, bool>) {
parser.parseSpecifier(specifier, Parser::ParameterType::Char); parser.parseSpecifier(specifier, Parser::ParameterType::Char);
} }
else if (std::is_same_v<T, std::string_view>) { else if (std::is_same_v<T, std::string_view>) {

Loading…
Cancel
Save