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)