Commit 34eb7676 by Tsige Solomon Committed by Copybara-Service

Support for int128 to string type conversion.

PiperOrigin-RevId: 542269673
Change-Id: Ib6f7e9a57f83d73dd6fb9c45fc9f85ff0fdd75fe
parent 166d71d1
...@@ -97,6 +97,7 @@ cc_test( ...@@ -97,6 +97,7 @@ cc_test(
"//absl/base", "//absl/base",
"//absl/hash:hash_testing", "//absl/hash:hash_testing",
"//absl/meta:type_traits", "//absl/meta:type_traits",
"//absl/strings",
"@com_google_googletest//:gtest_main", "@com_google_googletest//:gtest_main",
], ],
) )
......
...@@ -72,6 +72,7 @@ absl_cc_test( ...@@ -72,6 +72,7 @@ absl_cc_test(
absl::base absl::base
absl::hash_testing absl::hash_testing
absl::type_traits absl::type_traits
absl::strings
GTest::gmock_main GTest::gmock_main
) )
......
...@@ -202,6 +202,10 @@ std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) { ...@@ -202,6 +202,10 @@ std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) {
} // namespace } // namespace
std::string uint128::ToString() const {
return Uint128ToFormattedString(*this, std::ios_base::dec);
}
std::ostream& operator<<(std::ostream& os, uint128 v) { std::ostream& operator<<(std::ostream& os, uint128 v) {
std::ios_base::fmtflags flags = os.flags(); std::ios_base::fmtflags flags = os.flags();
std::string rep = Uint128ToFormattedString(v, flags); std::string rep = Uint128ToFormattedString(v, flags);
...@@ -285,6 +289,14 @@ int128 operator%(int128 lhs, int128 rhs) { ...@@ -285,6 +289,14 @@ int128 operator%(int128 lhs, int128 rhs) {
} }
#endif // ABSL_HAVE_INTRINSIC_INT128 #endif // ABSL_HAVE_INTRINSIC_INT128
std::string int128::ToString() const {
std::string rep;
if (Int128High64(*this) < 0) rep = "-";
rep.append(Uint128ToFormattedString(UnsignedAbsoluteValue(*this),
std::ios_base::dec));
return rep;
}
std::ostream& operator<<(std::ostream& os, int128 v) { std::ostream& operator<<(std::ostream& os, int128 v) {
std::ios_base::fmtflags flags = os.flags(); std::ios_base::fmtflags flags = os.flags();
std::string rep; std::string rep;
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#include <cstring> #include <cstring>
#include <iosfwd> #include <iosfwd>
#include <limits> #include <limits>
#include <string>
#include <utility> #include <utility>
#include "absl/base/config.h" #include "absl/base/config.h"
...@@ -217,9 +218,17 @@ class ...@@ -217,9 +218,17 @@ class
return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v)); return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v));
} }
// Support for absl::StrCat() etc.
template <typename Sink>
friend void AbslStringify(Sink& sink, uint128 v) {
sink.Append(v.ToString());
}
private: private:
constexpr uint128(uint64_t high, uint64_t low); constexpr uint128(uint64_t high, uint64_t low);
std::string ToString() const;
// TODO(strel) Update implementation to use __int128 once all users of // TODO(strel) Update implementation to use __int128 once all users of
// uint128 are fixed to not depend on alignof(uint128) == 8. Also add // uint128 are fixed to not depend on alignof(uint128) == 8. Also add
// alignas(16) to class definition to keep alignment consistent across // alignas(16) to class definition to keep alignment consistent across
...@@ -454,9 +463,17 @@ class int128 { ...@@ -454,9 +463,17 @@ class int128 {
return H::combine(std::move(h), Int128High64(v), Int128Low64(v)); return H::combine(std::move(h), Int128High64(v), Int128Low64(v));
} }
// Support for absl::StrCat() etc.
template <typename Sink>
friend void AbslStringify(Sink& sink, int128 v) {
sink.Append(v.ToString());
}
private: private:
constexpr int128(int64_t high, uint64_t low); constexpr int128(int64_t high, uint64_t low);
std::string ToString() const;
#if defined(ABSL_HAVE_INTRINSIC_INT128) #if defined(ABSL_HAVE_INTRINSIC_INT128)
__int128 v_; __int128 v_;
#else // ABSL_HAVE_INTRINSIC_INT128 #else // ABSL_HAVE_INTRINSIC_INT128
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <string> #include <string>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "absl/strings/str_cat.h"
namespace { namespace {
...@@ -87,6 +88,9 @@ constexpr std::ios::fmtflags kBase = std::ios::showbase; ...@@ -87,6 +88,9 @@ constexpr std::ios::fmtflags kBase = std::ios::showbase;
constexpr std::ios::fmtflags kPos = std::ios::showpos; constexpr std::ios::fmtflags kPos = std::ios::showpos;
void CheckUint128Case(const Uint128TestCase& test_case) { void CheckUint128Case(const Uint128TestCase& test_case) {
if (test_case.flags == kDec && test_case.width == 0) {
EXPECT_EQ(absl::StrCat(test_case.value), test_case.expected);
}
std::ostringstream os; std::ostringstream os;
os.flags(test_case.flags); os.flags(test_case.flags);
os.width(test_case.width); os.width(test_case.width);
...@@ -155,6 +159,9 @@ struct Int128TestCase { ...@@ -155,6 +159,9 @@ struct Int128TestCase {
}; };
void CheckInt128Case(const Int128TestCase& test_case) { void CheckInt128Case(const Int128TestCase& test_case) {
if (test_case.flags == kDec && test_case.width == 0) {
EXPECT_EQ(absl::StrCat(test_case.value), test_case.expected);
}
std::ostringstream os; std::ostringstream os;
os.flags(test_case.flags); os.flags(test_case.flags);
os.width(test_case.width); os.width(test_case.width);
......
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