summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-08-01 15:23:59 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-08-01 15:24:56 -0700
commite945c8d98719d2cca66ef1f0b83696becfa6a880 (patch)
tree29fca764686878fdf1381197ebedd634b3f45bb2
parentfc1dcc0f6a6b22f4ef4a30fc2020d4c81ab1b3c5 (diff)
Implement AbslStringify for crc32c_t in order to support absl::StrFormat natively
PiperOrigin-RevId: 552940359 Change-Id: I925764757404c0c9f2a13ed729190d51f4ac46cf
-rw-r--r--absl/crc/BUILD.bazel1
-rw-r--r--absl/crc/CMakeLists.txt1
-rw-r--r--absl/crc/crc32c.h5
-rw-r--r--absl/crc/crc32c_test.cc14
4 files changed, 21 insertions, 0 deletions
diff --git a/absl/crc/BUILD.bazel b/absl/crc/BUILD.bazel
index 29a9964f..cdbaa9b2 100644
--- a/absl/crc/BUILD.bazel
+++ b/absl/crc/BUILD.bazel
@@ -106,6 +106,7 @@ cc_test(
deps = [
":crc32c",
"//absl/strings",
+ "//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)
diff --git a/absl/crc/CMakeLists.txt b/absl/crc/CMakeLists.txt
index f49b4b0b..21247160 100644
--- a/absl/crc/CMakeLists.txt
+++ b/absl/crc/CMakeLists.txt
@@ -91,6 +91,7 @@ absl_cc_test(
DEPS
absl::crc32c
absl::strings
+ absl::str_format
GTest::gtest_main
)
diff --git a/absl/crc/crc32c.h b/absl/crc/crc32c.h
index 69799c8b..362861e4 100644
--- a/absl/crc/crc32c.h
+++ b/absl/crc/crc32c.h
@@ -62,6 +62,11 @@ class crc32c_t final {
friend bool operator!=(crc32c_t lhs, crc32c_t rhs) { return !(lhs == rhs); }
+ template <typename Sink>
+ friend void AbslStringify(Sink& sink, crc32c_t crc) {
+ absl::Format(&sink, "%08x", static_cast<uint32_t>(crc));
+ }
+
private:
uint32_t crc_;
};
diff --git a/absl/crc/crc32c_test.cc b/absl/crc/crc32c_test.cc
index 635424aa..df0afb3e 100644
--- a/absl/crc/crc32c_test.cc
+++ b/absl/crc/crc32c_test.cc
@@ -24,6 +24,7 @@
#include "gtest/gtest.h"
#include "absl/crc/internal/crc32c.h"
#include "absl/strings/str_cat.h"
+#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
namespace {
@@ -210,4 +211,17 @@ TEST(CRC32C, InsertionOperator) {
EXPECT_EQ(buf.str(), "00000011");
}
}
+
+TEST(CRC32C, AbslStringify) {
+ // StrFormat
+ EXPECT_EQ(absl::StrFormat("%v", absl::crc32c_t{0xc99465aa}), "c99465aa");
+ EXPECT_EQ(absl::StrFormat("%v", absl::crc32c_t{0}), "00000000");
+ EXPECT_EQ(absl::StrFormat("%v", absl::crc32c_t{17}), "00000011");
+
+ // StrCat
+ EXPECT_EQ(absl::StrCat(absl::crc32c_t{0xc99465aa}), "c99465aa");
+ EXPECT_EQ(absl::StrCat(absl::crc32c_t{0}), "00000000");
+ EXPECT_EQ(absl::StrCat(absl::crc32c_t{17}), "00000011");
+}
+
} // namespace