about summary refs log tree commit diff
path: root/absl/strings
diff options
context:
space:
mode:
Diffstat (limited to 'absl/strings')
-rw-r--r--absl/strings/str_cat.h27
-rw-r--r--absl/strings/str_cat_test.cc14
-rw-r--r--absl/strings/substitute_test.cc10
3 files changed, 40 insertions, 11 deletions
diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h
index 0c33b6ccd243..e38369cab7ff 100644
--- a/absl/strings/str_cat.h
+++ b/absl/strings/str_cat.h
@@ -127,21 +127,32 @@ struct Hex {
   char fill;
 
   template <typename Int>
-  explicit Hex(Int v, PadSpec spec = absl::kNoPad,
-               typename std::enable_if<sizeof(Int) == 1>::type* = nullptr)
+  explicit Hex(
+      Int v, PadSpec spec = absl::kNoPad,
+      typename std::enable_if<sizeof(Int) == 1 &&
+                              !std::is_pointer<Int>::value>::type* = nullptr)
       : Hex(spec, static_cast<uint8_t>(v)) {}
   template <typename Int>
-  explicit Hex(Int v, PadSpec spec = absl::kNoPad,
-               typename std::enable_if<sizeof(Int) == 2>::type* = nullptr)
+  explicit Hex(
+      Int v, PadSpec spec = absl::kNoPad,
+      typename std::enable_if<sizeof(Int) == 2 &&
+                              !std::is_pointer<Int>::value>::type* = nullptr)
       : Hex(spec, static_cast<uint16_t>(v)) {}
   template <typename Int>
-  explicit Hex(Int v, PadSpec spec = absl::kNoPad,
-               typename std::enable_if<sizeof(Int) == 4>::type* = nullptr)
+  explicit Hex(
+      Int v, PadSpec spec = absl::kNoPad,
+      typename std::enable_if<sizeof(Int) == 4 &&
+                              !std::is_pointer<Int>::value>::type* = nullptr)
       : Hex(spec, static_cast<uint32_t>(v)) {}
   template <typename Int>
-  explicit Hex(Int v, PadSpec spec = absl::kNoPad,
-               typename std::enable_if<sizeof(Int) == 8>::type* = nullptr)
+  explicit Hex(
+      Int v, PadSpec spec = absl::kNoPad,
+      typename std::enable_if<sizeof(Int) == 8 &&
+                              !std::is_pointer<Int>::value>::type* = nullptr)
       : Hex(spec, static_cast<uint64_t>(v)) {}
+  template <typename Pointee>
+  explicit Hex(Pointee* v, PadSpec spec = absl::kNoPad)
+      : Hex(spec, reinterpret_cast<uintptr_t>(v)) {}
 
  private:
   Hex(PadSpec spec, uint64_t v)
diff --git a/absl/strings/str_cat_test.cc b/absl/strings/str_cat_test.cc
index 710113f83b9f..c5a3526d7a6a 100644
--- a/absl/strings/str_cat_test.cc
+++ b/absl/strings/str_cat_test.cc
@@ -234,7 +234,7 @@ TEST(StrCat, CustomAllocator) {
 
 TEST(StrCat, MaxArgs) {
   std::string result;
-  // Test 10 up to 26 arguments, the current maximum
+  // Test 10 up to 26 arguments, the old maximum
   result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a");
   EXPECT_EQ(result, "123456789a");
   result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b");
@@ -469,6 +469,12 @@ void CheckHexDec64(uint64_t v) {
 
   long long llv = static_cast<long long>(ullv);  // NOLINT(runtime/int)
   CheckDec(llv, "%lld", "%0*lld", "%*lld");
+
+  if (sizeof(v) == sizeof(&v)) {
+    auto uintptr = static_cast<uintptr_t>(v);
+    void* ptr = reinterpret_cast<void*>(uintptr);
+    CheckHex(ptr, "%llx", "%0*llx", "%*llx");
+  }
 }
 
 void CheckHexDec32(uint32_t uv) {
@@ -476,6 +482,12 @@ void CheckHexDec32(uint32_t uv) {
   CheckDec(uv, "%u", "%0*u", "%*u");
   int32_t v = static_cast<int32_t>(uv);
   CheckDec(v, "%d", "%0*d", "%*d");
+
+  if (sizeof(v) == sizeof(&v)) {
+    auto uintptr = static_cast<uintptr_t>(v);
+    void* ptr = reinterpret_cast<void*>(uintptr);
+    CheckHex(ptr, "%llx", "%0*llx", "%*llx");
+  }
 }
 
 void CheckAll(uint64_t v) {
diff --git a/absl/strings/substitute_test.cc b/absl/strings/substitute_test.cc
index a6d7d7b0782d..7c9af6b22433 100644
--- a/absl/strings/substitute_test.cc
+++ b/absl/strings/substitute_test.cc
@@ -46,8 +46,14 @@ TEST(SubstituteTest, Substitute) {
   // Pointer.
   const int* int_p = reinterpret_cast<const int*>(0x12345);
   std::string str = absl::Substitute("$0", int_p);
-  EXPECT_EQ(absl::StrCat("0x", absl::Hex(reinterpret_cast<intptr_t>(int_p))),
-            str);
+  EXPECT_EQ(absl::StrCat("0x", absl::Hex(int_p)), str);
+
+  // Volatile Pointer.
+  // Like C++ streamed I/O, such pointers implicitly become bool
+  volatile int vol = 237;
+  volatile int *volatile volptr = &vol;
+  str = absl::Substitute("$0", volptr);
+  EXPECT_EQ("true", str);
 
   // null is special. StrCat prints 0x0. Substitute prints NULL.
   const uint64_t* null_p = nullptr;