Add ptr, a helper function for pointer formatting

This commit is contained in:
Victor Zverovich 2017-08-27 08:41:28 -07:00
parent 77c892c88e
commit 20168147dd
2 changed files with 8 additions and 2 deletions

View File

@ -3675,6 +3675,12 @@ void vformat_to(basic_buffer<Char> &buffer, basic_string_view<Char> 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 <typename T>
inline const void *ptr(const T *p) { return p; }
} // namespace fmt
#if FMT_USE_USER_DEFINED_LITERALS

View File

@ -1217,6 +1217,7 @@ TEST(FormatterTest, FormatPointer) {
EXPECT_EQ("0x1234", format("{0:p}", reinterpret_cast<void*>(0x1234)));
EXPECT_EQ("0x" + std::string(sizeof(void*) * CHAR_BIT / 4, 'f'),
format("{0}", reinterpret_cast<void*>(~uintptr_t())));
EXPECT_EQ("0x1234", format("{}", fmt::ptr(reinterpret_cast<int*>(0x1234))));
}
TEST(FormatterTest, FormatString) {
@ -1504,8 +1505,7 @@ class MockArgFormatter : public fmt::internal::arg_formatter_base<char> {
public:
typedef fmt::internal::arg_formatter_base<char> 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<char>(b, s) {
EXPECT_CALL(*this, call(42));
}