From 448ad42005c5f524eff5d8b5ee3d670c6e0b1a5c Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sat, 6 Aug 2022 00:39:03 +0200 Subject: [PATCH] Util: Add integral formatting to char and bool types --- src/util/format/formatter.cpp | 29 +++++++++++++++++++++++++++-- src/util/format/formatter.h | 5 ++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/util/format/formatter.cpp b/src/util/format/formatter.cpp index 201e3e3..aef77c0 100644 --- a/src/util/format/formatter.cpp +++ b/src/util/format/formatter.cpp @@ -21,13 +21,38 @@ namespace Util::Format { template<> void Formatter::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 formatter { .specifier = specifier }; + return formatter.format(builder, static_cast(value)); + } + + Formatter formatter { .specifier = specifier }; + return formatter.format(builder, { &value, 1 }); } template<> void Formatter::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 formatter { .specifier = specifier }; + return formatter.format(builder, static_cast(value)); + } + default: + break; + }; + + Formatter formatter { .specifier = specifier }; + formatter.format(builder, value ? "true" : "false"); } // String diff --git a/src/util/format/formatter.h b/src/util/format/formatter.h index 572a314..fdeada8 100644 --- a/src/util/format/formatter.h +++ b/src/util/format/formatter.h @@ -68,7 +68,10 @@ struct Formatter { constexpr void parse(Parser& parser) { - if (std::is_same_v) { + if constexpr (std::is_same_v) { + parser.parseSpecifier(specifier, Parser::ParameterType::Char); + } + else if (std::is_same_v) { parser.parseSpecifier(specifier, Parser::ParameterType::Char); } else if (std::is_same_v) {