From fc1dcc0f6a6b22f4ef4a30fc2020d4c81ab1b3c5 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 1 Aug 2023 14:40:45 -0700 Subject: Changes absl::crc32c_t insertion operator (<<) to return value as 0-padded hex instead of dec PiperOrigin-RevId: 552927211 Change-Id: I0375d60a9df4cdfc694fe8d3b3d790f80fc614a1 --- absl/crc/BUILD.bazel | 1 + absl/crc/CMakeLists.txt | 1 + absl/crc/crc32c.h | 3 ++- absl/crc/crc32c_test.cc | 19 +++++++++++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/absl/crc/BUILD.bazel b/absl/crc/BUILD.bazel index 1c58f46c..29a9964f 100644 --- a/absl/crc/BUILD.bazel +++ b/absl/crc/BUILD.bazel @@ -93,6 +93,7 @@ cc_library( "//absl/base:endian", "//absl/base:prefetch", "//absl/strings", + "//absl/strings:str_format", ], ) diff --git a/absl/crc/CMakeLists.txt b/absl/crc/CMakeLists.txt index 72ea2094..f49b4b0b 100644 --- a/absl/crc/CMakeLists.txt +++ b/absl/crc/CMakeLists.txt @@ -77,6 +77,7 @@ absl_cc_library( absl::dynamic_annotations absl::endian absl::prefetch + absl::str_format absl::strings ) diff --git a/absl/crc/crc32c.h b/absl/crc/crc32c.h index 79059dc1..69799c8b 100644 --- a/absl/crc/crc32c.h +++ b/absl/crc/crc32c.h @@ -29,6 +29,7 @@ #include #include "absl/crc/internal/crc32c_inline.h" +#include "absl/strings/str_format.h" #include "absl/strings/string_view.h" namespace absl { @@ -175,7 +176,7 @@ crc32c_t RemoveCrc32cSuffix(crc32c_t full_string_crc, crc32c_t suffix_crc, // // Streams the CRC32C value `crc` to the stream `os`. inline std::ostream& operator<<(std::ostream& os, crc32c_t crc) { - return os << static_cast(crc); + return os << absl::StreamFormat("%08x", static_cast(crc)); } ABSL_NAMESPACE_END diff --git a/absl/crc/crc32c_test.cc b/absl/crc/crc32c_test.cc index 72d422a1..635424aa 100644 --- a/absl/crc/crc32c_test.cc +++ b/absl/crc/crc32c_test.cc @@ -18,6 +18,7 @@ #include #include #include +#include #include #include "gtest/gtest.h" @@ -191,4 +192,22 @@ TEST(CRC32C, RemoveSuffix) { EXPECT_EQ(absl::RemoveCrc32cSuffix(crc_ab, crc_b, world.size()), crc_a); } + +TEST(CRC32C, InsertionOperator) { + { + std::ostringstream buf; + buf << absl::crc32c_t{0xc99465aa}; + EXPECT_EQ(buf.str(), "c99465aa"); + } + { + std::ostringstream buf; + buf << absl::crc32c_t{0}; + EXPECT_EQ(buf.str(), "00000000"); + } + { + std::ostringstream buf; + buf << absl::crc32c_t{17}; + EXPECT_EQ(buf.str(), "00000011"); + } +} } // namespace -- cgit v1.2.3