Changes absl::crc32c_t insertion operator (<<) to return value as 0-padded hex instead of dec

PiperOrigin-RevId: 552927211
Change-Id: I0375d60a9df4cdfc694fe8d3b3d790f80fc614a1
This commit is contained in:
Abseil Team
2023-08-01 14:40:45 -07:00
committed by Copybara-Service
parent 5b3b0ed81c
commit fc1dcc0f6a
4 changed files with 23 additions and 1 deletions

View File

@@ -93,6 +93,7 @@ cc_library(
"//absl/base:endian",
"//absl/base:prefetch",
"//absl/strings",
"//absl/strings:str_format",
],
)

View File

@@ -77,6 +77,7 @@ absl_cc_library(
absl::dynamic_annotations
absl::endian
absl::prefetch
absl::str_format
absl::strings
)

View File

@@ -29,6 +29,7 @@
#include <ostream>
#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<uint32_t>(crc);
return os << absl::StreamFormat("%08x", static_cast<uint32_t>(crc));
}
ABSL_NAMESPACE_END

View File

@@ -18,6 +18,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <sstream>
#include <string>
#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