From 0dc82b9d55e1616c1745d05973d40c9901903cc9 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Fri, 9 Feb 2018 11:56:54 -0800 Subject: Changes imported from Abseil "staging" branch: - a0405e7870a80a9dbc6784b06795e7df5a8c90f5 Internal change by Daniel Katz - 2888fe17796d7afa45f4b6ca7eb8e88f52739c39 StrCat: Support zero-padding and space-padding for decima... by Jorg Brown - feebc521195241783730df9700394f6585550ff2 Merge GitHub PR #91. by Derek Mauro - e8164335efefb7335f407c17a16fce2ba4f24e3e This changes the value base_internal::kOnceDone from 32-b... by Abseil Team - 0f6085f3f0ee1d6baf9a558d07a25c2fcde93273 Remove `compliant` field from some type trait structs. by Matt Armstrong GitOrigin-RevId: a0405e7870a80a9dbc6784b06795e7df5a8c90f5 Change-Id: Ic2efd40f6ec35f79a8aa12d4475cbea3150756d7 --- absl/strings/str_cat.h | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'absl/strings/str_cat.h') diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h index 1cf7b11f..0c33b6cc 100644 --- a/absl/strings/str_cat.h +++ b/absl/strings/str_cat.h @@ -76,10 +76,10 @@ struct AlphaNumBuffer { } // namespace strings_internal -// Enum that specifies the number of significant digits to return in a `Hex` -// conversion and fill character to use. A `kZeroPad2` value, for example, would -// produce hexadecimal strings such as "0A","0F" and 'kSpacePad5' value would -// produce hexadecimal strings such as " A"," F". +// Enum that specifies the number of significant digits to return in a `Hex` or +// `Dec` conversion and fill character to use. A `kZeroPad2` value, for example, +// would produce hexadecimal strings such as "0A","0F" and a 'kSpacePad5' value +// would produce hexadecimal strings such as " A"," F". enum PadSpec { kNoPad = 1, kZeroPad2, @@ -153,6 +153,32 @@ struct Hex { fill(spec >= absl::kSpacePad2 ? ' ' : '0') {} }; +// ----------------------------------------------------------------------------- +// Dec +// ----------------------------------------------------------------------------- +// +// `Dec` stores a set of decimal std::string conversion parameters for use +// within `AlphaNum` std::string conversions. Dec is slower than the default +// integer conversion, so use it only if you need padding. +struct Dec { + uint64_t value; + uint8_t width; + char fill; + bool neg; + + template + explicit Dec(Int v, PadSpec spec = absl::kNoPad, + typename std::enable_if<(sizeof(Int) <= 8)>::type* = nullptr) + : value(v >= 0 ? static_cast(v) + : uint64_t{0} - static_cast(v)), + width(spec == absl::kNoPad + ? 1 + : spec >= absl::kSpacePad2 ? spec - absl::kSpacePad2 + 2 + : spec - absl::kZeroPad2 + 2), + fill(spec >= absl::kSpacePad2 ? ' ' : '0'), + neg(v < 0) {} +}; + // ----------------------------------------------------------------------------- // AlphaNum // ----------------------------------------------------------------------------- @@ -191,6 +217,7 @@ class AlphaNum { : piece_(digits_, numbers_internal::SixDigitsToBuffer(f, digits_)) {} AlphaNum(Hex hex); // NOLINT(runtime/explicit) + AlphaNum(Dec dec); // NOLINT(runtime/explicit) template AlphaNum( // NOLINT(runtime/explicit) -- cgit v1.2.3