summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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