From 20168147dd461c05a51d007c1f7747c30144edca Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 27 Aug 2017 08:41:28 -0700 Subject: [PATCH] Add ptr, a helper function for pointer formatting --- fmt/format.h | 6 ++++++ test/format-test.cc | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index c364cbaa..3cc6d0c0 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -3675,6 +3675,12 @@ void vformat_to(basic_buffer &buffer, basic_string_view format_str, } buffer.append(pointer_from(start), pointer_from(it)); } + +// Casts ``p`` to ``const void*`` for pointer formatting. +// Example: +// auto s = format("{}", ptr(p)); +template +inline const void *ptr(const T *p) { return p; } } // namespace fmt #if FMT_USE_USER_DEFINED_LITERALS diff --git a/test/format-test.cc b/test/format-test.cc index 88d30b72..08dafb3b 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1217,6 +1217,7 @@ TEST(FormatterTest, FormatPointer) { EXPECT_EQ("0x1234", format("{0:p}", reinterpret_cast(0x1234))); EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'), format("{0}", reinterpret_cast(~uintptr_t()))); + EXPECT_EQ("0x1234", format("{}", fmt::ptr(reinterpret_cast(0x1234)))); } TEST(FormatterTest, FormatString) { @@ -1504,8 +1505,7 @@ class MockArgFormatter : public fmt::internal::arg_formatter_base { public: typedef fmt::internal::arg_formatter_base Base; - MockArgFormatter(fmt::buffer &b, fmt::context &ctx, - fmt::format_specs &s) + MockArgFormatter(fmt::buffer &b, fmt::context &, fmt::format_specs &s) : fmt::internal::arg_formatter_base(b, s) { EXPECT_CALL(*this, call(42)); }