diff options
author | Abseil Team <absl-team@google.com> | 2022-09-30 12:11:27 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-09-30 12:12:26 -0700 |
commit | 7f3c0d781134d25b24e0b55346482a2d366bf59b (patch) | |
tree | 922945db62ad94b4aaad106e88454e196a9c1f16 /absl/strings/str_cat.cc | |
parent | e8304a5c62576cdad9d9265449ab5575115fd4f2 (diff) |
Allows absl::StrCat to accept types that implement AbslStringify()
PiperOrigin-RevId: 478050535
Change-Id: I8e4a4b01aceb8d712476101633eac0ce8647823a
Diffstat (limited to 'absl/strings/str_cat.cc')
-rw-r--r-- | absl/strings/str_cat.cc | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/absl/strings/str_cat.cc b/absl/strings/str_cat.cc index 2f9df673..6981347a 100644 --- a/absl/strings/str_cat.cc +++ b/absl/strings/str_cat.cc @@ -17,16 +17,52 @@ #include <assert.h> #include <algorithm> +#include <cstddef> #include <cstdint> #include <cstring> +#include <string> #include "absl/strings/ascii.h" #include "absl/strings/internal/resize_uninitialized.h" #include "absl/strings/numbers.h" +#include "absl/strings/string_view.h" namespace absl { ABSL_NAMESPACE_BEGIN +namespace strings_internal { +void StringifySink::Append(size_t count, char ch) { buffer_.append(count, ch); } + +void StringifySink::Append(string_view v) { + buffer_.append(v.data(), v.size()); +} + +bool StringifySink::PutPaddedString(string_view v, int width, int precision, + bool left) { + size_t space_remaining = 0; + + if (width >= 0) space_remaining = static_cast<size_t>(width); + + size_t n = v.size(); + + if (precision >= 0) n = (std::min)(n, static_cast<size_t>(precision)); + + string_view shown(v.data(), n); + + if (shown.size() < space_remaining) { + space_remaining = space_remaining - shown.size(); + } else { + space_remaining = 0; + } + + if (!left) Append(space_remaining, ' '); + Append(shown); + if (left) Append(space_remaining, ' '); + return true; +} + +} // namespace strings_internal + AlphaNum::AlphaNum(Hex hex) { static_assert(numbers_internal::kFastToBufferSize >= 32, "This function only works when output buffer >= 32 bytes long"); |