summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AK/Format.cpp11
-rw-r--r--AK/Format.h17
-rw-r--r--AK/Tests/TestFormat.cpp11
3 files changed, 39 insertions, 0 deletions
diff --git a/AK/Format.cpp b/AK/Format.cpp
index e09a160d10..d31a3c7790 100644
--- a/AK/Format.cpp
+++ b/AK/Format.cpp
@@ -460,6 +460,17 @@ void Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type>::format(StringB
PrintfImplementation::convert_signed_to_string(value, builder, base, m_alternative_form, upper_case, m_zero_pad, align, width, m_fill, sign_mode);
}
+void Formatter<bool>::format(StringBuilder& builder, bool value, FormatterContext& context)
+{
+ 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) {
+ Formatter<u8> formatter { *this };
+ return formatter.format(builder, static_cast<u8>(value), context);
+ } else {
+ Formatter<StringView> formatter { *this };
+ formatter.format(builder, value ? "true" : "false", context);
+ }
+}
+
template struct Formatter<unsigned char, void>;
template struct Formatter<unsigned short, void>;
template struct Formatter<unsigned int, void>;
diff --git a/AK/Format.h b/AK/Format.h
index 78f2d4f10c..5389565865 100644
--- a/AK/Format.h
+++ b/AK/Format.h
@@ -166,6 +166,12 @@ struct StandardFormatter {
template<>
struct Formatter<StringView> : StandardFormatter {
+ Formatter() { }
+ explicit Formatter(StandardFormatter formatter)
+ : StandardFormatter(formatter)
+ {
+ }
+
void format(StringBuilder& builder, StringView value, FormatterContext&);
};
template<>
@@ -183,9 +189,20 @@ struct Formatter<String> : Formatter<StringView> {
template<typename T>
struct Formatter<T, typename EnableIf<IsIntegral<T>::value>::Type> : StandardFormatter {
+ Formatter() { }
+ explicit Formatter(StandardFormatter formatter)
+ : StandardFormatter(formatter)
+ {
+ }
+
void format(StringBuilder&, T value, FormatterContext&);
};
+template<>
+struct Formatter<bool> : StandardFormatter {
+ void format(StringBuilder&, bool value, FormatterContext&);
+};
+
template<typename... Parameters>
Array<TypeErasedParameter, sizeof...(Parameters)> make_type_erased_parameters(const Parameters&... parameters)
{
diff --git a/AK/Tests/TestFormat.cpp b/AK/Tests/TestFormat.cpp
index fa26f0caf2..fe8c6032e0 100644
--- a/AK/Tests/TestFormat.cpp
+++ b/AK/Tests/TestFormat.cpp
@@ -136,4 +136,15 @@ TEST_CASE(cast_integer_to_character)
EXPECT_EQ(String::formatted("{:c}", static_cast<unsigned int>('f')), "f");
}
+TEST_CASE(boolean_values)
+{
+ EXPECT_EQ(String::formatted("{}", true), "true");
+ EXPECT_EQ(String::formatted("{}", false), "false");
+ EXPECT_EQ(String::formatted("{:6}", true), "true ");
+ EXPECT_EQ(String::formatted("{:>4}", false), "false");
+ EXPECT_EQ(String::formatted("{:d}", false), "0");
+ EXPECT_EQ(String::formatted("{:d}", true), "1");
+ EXPECT_EQ(String::formatted("{:#08x}", true), "0x000001");
+}
+
TEST_MAIN(Format)