aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuliano Procida <gprocida@google.com>2024-02-05 17:36:39 +0000
committerGiuliano Procida <gprocida@google.com>2024-02-09 12:54:52 +0000
commit39f91d9020ca7c902903d4d328d57d23801959a2 (patch)
tree4f6ab0b06e0d00c0e7e36bb1eb8af825a64ca404
parentf6e571253290ef1d3c2d948932e8471e849ce7bb (diff)
downloadstg-39f91d9020ca7c902903d4d328d57d23801959a2.tar.gz
Always format zero as hex with a leading 0x
The standard library's `std::setbase` specifies special behaviour for zero (no leading `0x`). Clang doesn't follow the standard, but GCC does. This change avoids both the problem of unexpected (but standards-compliant) output with GCC and the inconsistency between the two toolchains. PiperOrigin-RevId: 604344160 Change-Id: I320fd033f291b887e53ee4dc2723615458da0f3e
-rw-r--r--error.h2
-rw-r--r--proto_writer.cc3
2 files changed, 2 insertions, 3 deletions
diff --git a/error.h b/error.h
index 5ca6c57..b72faaf 100644
--- a/error.h
+++ b/error.h
@@ -124,7 +124,7 @@ template <typename T>
std::ostream& operator<<(std::ostream& os, const Hex<T>& hex_value) {
// not quite right if an exception is thrown
const auto flags = os.flags();
- os << std::hex << std::showbase << hex_value.value;
+ os << "0x" << std::hex << hex_value.value;
os.flags(flags);
return os;
}
diff --git a/proto_writer.cc b/proto_writer.cc
index 122872a..d3de03d 100644
--- a/proto_writer.cc
+++ b/proto_writer.cc
@@ -497,8 +497,7 @@ class HexPrinter : public google::protobuf::TextFormat::FastFieldValuePrinter {
google::protobuf::TextFormat::BaseTextGenerator* generator) const override {
std::ostringstream os;
// 0x01234567
- os << std::showbase << std::hex << std::setfill('0') << std::internal
- << std::setw(10) << value;
+ os << "0x" << std::hex << std::setfill('0') << std::setw(8) << value;
generator->PrintString(os.str());
}
};