Util: Add integral formatting to char and bool types

This commit is contained in:
Riyyi
2022-08-06 00:39:03 +02:00
parent 80c37496e8
commit 448ad42005
2 changed files with 31 additions and 3 deletions
+27 -2
View File
@@ -21,13 +21,38 @@ namespace Util::Format {
template<>
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<>
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
+4 -1
View File
@@ -68,7 +68,10 @@ struct Formatter {
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);
}
else if (std::is_same_v<T, std::string_view>) {