From 632ff01e174f4efc0da6dc9f2170932a3c400fde Mon Sep 17 00:00:00 2001 From: asynts Date: Fri, 1 Jan 2021 23:34:00 +0100 Subject: [PATCH] Piggyback: AK: Add formatter for std::nullptr_t. --- AK/Format.h | 11 +++++++++++ AK/Tests/TestFormat.cpp | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/AK/Format.h b/AK/Format.h index ea2bd6edf7..90030765ec 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -348,6 +348,17 @@ struct Formatter : StandardFormatter { }; #endif +template<> +struct Formatter : Formatter { + void format(FormatBuilder& builder, std::nullptr_t) + { + if (m_mode == Mode::Default) + m_mode = Mode::Pointer; + + return Formatter::format(builder, 0); + } +}; + void vformat(StringBuilder& builder, StringView fmtstr, TypeErasedFormatParams); void vformat(const LogStream& stream, StringView fmtstr, TypeErasedFormatParams); diff --git a/AK/Tests/TestFormat.cpp b/AK/Tests/TestFormat.cpp index bcf9b91889..cfe7fe2950 100644 --- a/AK/Tests/TestFormat.cpp +++ b/AK/Tests/TestFormat.cpp @@ -269,4 +269,9 @@ TEST_CASE(yay_this_implementation_sucks) EXPECT_EQ(String::formatted("{:.0}", .99999999999), "0."); } +TEST_CASE(format_nullptr) +{ + EXPECT_EQ(String::formatted("{}", nullptr), String::formatted("{:p}", static_cast(0))); +} + TEST_MAIN(Format)