Commit e945c8d9 by Abseil Team Committed by Copybara-Service

Implement AbslStringify for crc32c_t in order to support absl::StrFormat natively

PiperOrigin-RevId: 552940359
Change-Id: I925764757404c0c9f2a13ed729190d51f4ac46cf
parent fc1dcc0f
......@@ -106,6 +106,7 @@ cc_test(
deps = [
":crc32c",
"//absl/strings",
"//absl/strings:str_format",
"@com_google_googletest//:gtest_main",
],
)
......
......@@ -91,6 +91,7 @@ absl_cc_test(
DEPS
absl::crc32c
absl::strings
absl::str_format
GTest::gtest_main
)
......
......@@ -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_;
};
......
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment