about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2017-11-17T15·42-0800
committerJohn Olson <jolson@google.com>2017-11-17T19·58-0500
commit4f3edeb1b4cc3d6e1d26eff6706fd3006961b9ed (patch)
tree75b9894ed55fd57283a0ea8f51ad10bed383c6b7
parentb05b79538ffc0aea824f2ce7b8a617a62a14d7c0 (diff)
Changes imported from Abseil "staging" branch:
  - c910f1792eae8cfdabb93bc261bf26e72f9a6717 Fix uint128 streaming to work with std::ios::internal. by Alex Strelnikov <strel@google.com>

GitOrigin-RevId: c910f1792eae8cfdabb93bc261bf26e72f9a6717
Change-Id: I8ccce2ce6ac91ef7ce16fa8870a718fdfb8fa9a6
-rw-r--r--absl/numeric/int128.cc8
-rw-r--r--absl/numeric/int128_test.cc20
2 files changed, 26 insertions, 2 deletions
diff --git a/absl/numeric/int128.cc b/absl/numeric/int128.cc
index 7debf56a62b7..5081def5e1b0 100644
--- a/absl/numeric/int128.cc
+++ b/absl/numeric/int128.cc
@@ -187,8 +187,14 @@ std::ostream& operator<<(std::ostream& o, const uint128& b) {
   // Add the requisite padding.
   std::streamsize width = o.width(0);
   if (static_cast<size_t>(width) > rep.size()) {
-    if ((flags & std::ios::adjustfield) == std::ios::left) {
+    std::ios::fmtflags adjustfield = flags & std::ios::adjustfield;
+    if (adjustfield == std::ios::left) {
       rep.append(width - rep.size(), o.fill());
+    } else if (adjustfield == std::ios::internal &&
+               (flags & std::ios::showbase) &&
+               (flags & std::ios::basefield) != std::ios::dec) {
+      size_t base_size = (flags & std::ios::basefield) == std::ios::hex ? 2 : 1;
+      rep.insert(base_size, width - rep.size(), o.fill());
     } else {
       rep.insert(0, width - rep.size(), o.fill());
     }
diff --git a/absl/numeric/int128_test.cc b/absl/numeric/int128_test.cc
index 1772d0ef2443..46d5546f9a3e 100644
--- a/absl/numeric/int128_test.cc
+++ b/absl/numeric/int128_test.cc
@@ -475,12 +475,30 @@ TEST(Uint128, OStream) {
       {absl::uint128(0), std::ios::hex | std::ios::showbase, 0, '_', "0"},
       // showpos does nothing on unsigned types
       {absl::uint128(1), std::ios::dec | std::ios::showpos, 0, '_', "1"},
-      // padding
+      // right adjustment
       {absl::uint128(9), std::ios::dec, 6, '_', "_____9"},
       {absl::uint128(12345), std::ios::dec, 6, '_', "_12345"},
+      {absl::uint128(31), std::ios::hex | std::ios::showbase, 6, '_', "__0x1f"},
+      {absl::uint128(7), std::ios::oct | std::ios::showbase, 6, '_', "____07"},
       // left adjustment
       {absl::uint128(9), std::ios::dec | std::ios::left, 6, '_', "9_____"},
       {absl::uint128(12345), std::ios::dec | std::ios::left, 6, '_', "12345_"},
+      {absl::uint128(31), std::ios::hex | std::ios::left | std::ios::showbase,
+       6, '_', "0x1f__"},
+      {absl::uint128(7), std::ios::oct | std::ios::left | std::ios::showbase, 6,
+       '_', "07____"},
+      // internal adjustment
+      {absl::uint128(123),
+       std::ios::dec | std::ios::internal | std::ios::showbase, 6, '_',
+       "___123"},
+      {absl::uint128(31),
+       std::ios::hex | std::ios::internal | std::ios::showbase, 6, '_',
+       "0x__1f"},
+      {absl::uint128(7),
+       std::ios::oct | std::ios::internal | std::ios::showbase, 6, '_',
+       "0____7"},
+      {absl::uint128(34), std::ios::hex | std::ios::internal, 6, '_', "____22"},
+      {absl::uint128(9), std::ios::oct | std::ios::internal, 6, '_', "____11"},
   };
   for (const auto& test_case : cases) {
     std::ostringstream os;