From 34eb767645347f100bdd66fc1e35eee96e357961 Mon Sep 17 00:00:00 2001 From: Tsige Solomon Date: Wed, 21 Jun 2023 08:52:53 -0700 Subject: Support for int128 to string type conversion. PiperOrigin-RevId: 542269673 Change-Id: Ib6f7e9a57f83d73dd6fb9c45fc9f85ff0fdd75fe --- absl/numeric/int128.cc | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'absl/numeric/int128.cc') diff --git a/absl/numeric/int128.cc b/absl/numeric/int128.cc index 6ffe43d5..daa32b51 100644 --- a/absl/numeric/int128.cc +++ b/absl/numeric/int128.cc @@ -202,6 +202,10 @@ std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) { } // namespace +std::string uint128::ToString() const { + return Uint128ToFormattedString(*this, std::ios_base::dec); +} + std::ostream& operator<<(std::ostream& os, uint128 v) { std::ios_base::fmtflags flags = os.flags(); std::string rep = Uint128ToFormattedString(v, flags); @@ -285,6 +289,14 @@ int128 operator%(int128 lhs, int128 rhs) { } #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::ios_base::fmtflags flags = os.flags(); std::string rep; -- cgit v1.2.3