Skip to content

Commit 34eb767

Browse files
Tsige Solomoncopybara-github
authored andcommitted
Support for int128 to string type conversion.
PiperOrigin-RevId: 542269673 Change-Id: Ib6f7e9a57f83d73dd6fb9c45fc9f85ff0fdd75fe
1 parent 166d71d commit 34eb767

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

absl/numeric/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ cc_test(
9797
"//absl/base",
9898
"//absl/hash:hash_testing",
9999
"//absl/meta:type_traits",
100+
"//absl/strings",
100101
"@com_google_googletest//:gtest_main",
101102
],
102103
)

absl/numeric/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ absl_cc_test(
7272
absl::base
7373
absl::hash_testing
7474
absl::type_traits
75+
absl::strings
7576
GTest::gmock_main
7677
)
7778

absl/numeric/int128.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) {
202202

203203
} // namespace
204204

205+
std::string uint128::ToString() const {
206+
return Uint128ToFormattedString(*this, std::ios_base::dec);
207+
}
208+
205209
std::ostream& operator<<(std::ostream& os, uint128 v) {
206210
std::ios_base::fmtflags flags = os.flags();
207211
std::string rep = Uint128ToFormattedString(v, flags);
@@ -285,6 +289,14 @@ int128 operator%(int128 lhs, int128 rhs) {
285289
}
286290
#endif // ABSL_HAVE_INTRINSIC_INT128
287291

292+
std::string int128::ToString() const {
293+
std::string rep;
294+
if (Int128High64(*this) < 0) rep = "-";
295+
rep.append(Uint128ToFormattedString(UnsignedAbsoluteValue(*this),
296+
std::ios_base::dec));
297+
return rep;
298+
}
299+
288300
std::ostream& operator<<(std::ostream& os, int128 v) {
289301
std::ios_base::fmtflags flags = os.flags();
290302
std::string rep;

absl/numeric/int128.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <cstring>
3333
#include <iosfwd>
3434
#include <limits>
35+
#include <string>
3536
#include <utility>
3637

3738
#include "absl/base/config.h"
@@ -217,9 +218,17 @@ class
217218
return H::combine(std::move(h), Uint128High64(v), Uint128Low64(v));
218219
}
219220

221+
// Support for absl::StrCat() etc.
222+
template <typename Sink>
223+
friend void AbslStringify(Sink& sink, uint128 v) {
224+
sink.Append(v.ToString());
225+
}
226+
220227
private:
221228
constexpr uint128(uint64_t high, uint64_t low);
222229

230+
std::string ToString() const;
231+
223232
// TODO(strel) Update implementation to use __int128 once all users of
224233
// uint128 are fixed to not depend on alignof(uint128) == 8. Also add
225234
// alignas(16) to class definition to keep alignment consistent across
@@ -454,9 +463,17 @@ class int128 {
454463
return H::combine(std::move(h), Int128High64(v), Int128Low64(v));
455464
}
456465

466+
// Support for absl::StrCat() etc.
467+
template <typename Sink>
468+
friend void AbslStringify(Sink& sink, int128 v) {
469+
sink.Append(v.ToString());
470+
}
471+
457472
private:
458473
constexpr int128(int64_t high, uint64_t low);
459474

475+
std::string ToString() const;
476+
460477
#if defined(ABSL_HAVE_INTRINSIC_INT128)
461478
__int128 v_;
462479
#else // ABSL_HAVE_INTRINSIC_INT128

absl/numeric/int128_stream_test.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <string>
1919

2020
#include "gtest/gtest.h"
21+
#include "absl/strings/str_cat.h"
2122

2223
namespace {
2324

@@ -87,6 +88,9 @@ constexpr std::ios::fmtflags kBase = std::ios::showbase;
8788
constexpr std::ios::fmtflags kPos = std::ios::showpos;
8889

8990
void CheckUint128Case(const Uint128TestCase& test_case) {
91+
if (test_case.flags == kDec && test_case.width == 0) {
92+
EXPECT_EQ(absl::StrCat(test_case.value), test_case.expected);
93+
}
9094
std::ostringstream os;
9195
os.flags(test_case.flags);
9296
os.width(test_case.width);
@@ -155,6 +159,9 @@ struct Int128TestCase {
155159
};
156160

157161
void CheckInt128Case(const Int128TestCase& test_case) {
162+
if (test_case.flags == kDec && test_case.width == 0) {
163+
EXPECT_EQ(absl::StrCat(test_case.value), test_case.expected);
164+
}
158165
std::ostringstream os;
159166
os.flags(test_case.flags);
160167
os.width(test_case.width);

0 commit comments

Comments
 (0)