diff options
author | asynts <asynts@gmail.com> | 2020-10-07 14:25:19 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-08 09:59:55 +0200 |
commit | d546d31a531f77916e7b66391c9a890b5984b6cf (patch) | |
tree | 7a6733d1cb5e19fe14d91ebd7575c0ef421323ad /AK | |
parent | 2217d6b560d9030d66259f5b0feb292d5e515cff (diff) | |
download | serenity-d546d31a531f77916e7b66391c9a890b5984b6cf.zip |
AK+Format: Make it possible to format characters as integers.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Format.cpp | 11 | ||||
-rw-r--r-- | AK/Format.h | 8 |
2 files changed, 13 insertions, 6 deletions
diff --git a/AK/Format.cpp b/AK/Format.cpp index fece3b2e64..45188c752b 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -549,6 +549,17 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(TypeEra builder.put_i64(value, base, m_alternative_form, upper_case, m_zero_pad, m_align, width, m_fill, m_sign_mode); } +void Formatter<char>::format(TypeErasedFormatParams& params, FormatBuilder& builder, char value) +{ + if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) { + // Trick: signed char != char. (Sometimes weird features are actually helpful.) + Formatter<signed char> formatter { *this }; + return formatter.format(params, builder, static_cast<signed char>(value)); + } else { + Formatter<StringView> formatter { *this }; + return formatter.format(params, builder, { &value, 1 }); + } +} void Formatter<bool>::format(TypeErasedFormatParams& params, FormatBuilder& builder, bool value) { if (m_mode == Mode::Binary || m_mode == Mode::BinaryUppercase || m_mode == Mode::Decimal || m_mode == Mode::Octal || m_mode == Mode::Hexadecimal || m_mode == Mode::HexadecimalUppercase) { diff --git a/AK/Format.h b/AK/Format.h index d66e9b607c..2b800111f1 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -295,13 +295,9 @@ struct Formatter<T*> : StandardFormatter { }; template<> -struct Formatter<char> : Formatter<StringView> { - void format(TypeErasedFormatParams& params, FormatBuilder& builder, char value) - { - Formatter<StringView>::format(params, builder, { &value, 1 }); - } +struct Formatter<char> : StandardFormatter { + void format(TypeErasedFormatParams&, FormatBuilder&, char value); }; - template<> struct Formatter<bool> : StandardFormatter { void format(TypeErasedFormatParams&, FormatBuilder&, bool value); |