diff options
author | Abseil Team <absl-team@google.com> | 2018-02-09 11:56:54 -0800 |
---|---|---|
committer | Daniel Katz <katzdm@google.com> | 2018-02-09 20:09:05 +0000 |
commit | 0dc82b9d55e1616c1745d05973d40c9901903cc9 (patch) | |
tree | 9eaf6866f9625dcaac4c07790fbc06ff3fc099cf /absl/strings/str_cat.cc | |
parent | 4791df7d1ac966e6c7abdeffafa5030d718500df (diff) |
Changes imported from Abseil "staging" branch:
- a0405e7870a80a9dbc6784b06795e7df5a8c90f5 Internal change by Daniel Katz <katzdm@google.com>
- 2888fe17796d7afa45f4b6ca7eb8e88f52739c39 StrCat: Support zero-padding and space-padding for decima... by Jorg Brown <jorg@google.com>
- feebc521195241783730df9700394f6585550ff2 Merge GitHub PR #91. by Derek Mauro <dmauro@google.com>
- e8164335efefb7335f407c17a16fce2ba4f24e3e This changes the value base_internal::kOnceDone from 32-b... by Abseil Team <absl-team@google.com>
- 0f6085f3f0ee1d6baf9a558d07a25c2fcde93273 Remove `compliant` field from some type trait structs. by Matt Armstrong <marmstrong@google.com>
GitOrigin-RevId: a0405e7870a80a9dbc6784b06795e7df5a8c90f5
Change-Id: Ic2efd40f6ec35f79a8aa12d4475cbea3150756d7
Diffstat (limited to 'absl/strings/str_cat.cc')
-rw-r--r-- | absl/strings/str_cat.cc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc index 99eb2890..3fe8c95e 100644 --- a/absl/strings/str_cat.cc +++ b/absl/strings/str_cat.cc @@ -45,6 +45,37 @@ AlphaNum::AlphaNum(Hex hex) { piece_ = absl::string_view(beg, end - beg); } +AlphaNum::AlphaNum(Dec dec) { + assert(dec.width <= numbers_internal::kFastToBufferSize); + char* const end = &digits_[numbers_internal::kFastToBufferSize]; + char* const minfill = end - dec.width; + char* writer = end; + uint64_t value = dec.value; + bool neg = dec.neg; + while (value > 9) { + *--writer = '0' + (value % 10); + value /= 10; + } + *--writer = '0' + value; + if (neg) *--writer = '-'; + + ptrdiff_t fillers = writer - minfill; + if (fillers > 0) { + // Tricky: if the fill character is ' ', then it's <fill><+/-><digits> + // But...: if the fill character is '0', then it's <+/-><fill><digits> + bool add_sign_again = false; + if (neg && dec.fill == '0') { // If filling with '0', + ++writer; // ignore the sign we just added + add_sign_again = true; // and re-add the sign later. + } + writer -= fillers; + std::fill_n(writer, fillers, dec.fill); + if (add_sign_again) *--writer = '-'; + } + + piece_ = absl::string_view(writer, end - writer); +} + // ---------------------------------------------------------------------- // StrCat() // This merges the given strings or integers, with no delimiter. This |