diff options
author | Abseil Team <absl-team@google.com> | 2023-10-24 08:32:58 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-10-24 08:33:49 -0700 |
commit | 822e3244e4b842fbcba9168e536ef71380a024b0 (patch) | |
tree | cbc7f8586e76104504bdd9a7b468ac6ecfcb603f /absl/strings | |
parent | a6d9a9cddade9139616a980c8092431492b84f3c (diff) |
Add `AbslStringify()` support to `absl::Cord`
PiperOrigin-RevId: 576149980
Change-Id: I0a7e3df7a01edc78ee5d15d8d8e82e7bbc5fc0f3
Diffstat (limited to 'absl/strings')
-rw-r--r-- | absl/strings/cord.h | 8 | ||||
-rw-r--r-- | absl/strings/cord_test.cc | 7 | ||||
-rw-r--r-- | absl/strings/internal/str_format/arg.h | 1 |
3 files changed, 16 insertions, 0 deletions
diff --git a/absl/strings/cord.h b/absl/strings/cord.h index 1d30fde8..af013df0 100644 --- a/absl/strings/cord.h +++ b/absl/strings/cord.h @@ -765,6 +765,14 @@ class Cord { cord->Append(part); } + // Support automatic stringification with absl::StrCat and absl::StrFormat. + template <typename Sink> + friend void AbslStringify(Sink& sink, const absl::Cord& cord) { + for (absl::string_view chunk : cord.Chunks()) { + sink.Append(chunk); + } + } + // Cord::SetExpectedChecksum() // // Stores a checksum value with this non-empty cord instance, for later diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc index bf507094..f1a5f39c 100644 --- a/absl/strings/cord_test.cc +++ b/absl/strings/cord_test.cc @@ -2656,6 +2656,13 @@ TEST_P(CordTest, Format) { EXPECT_EQ(c, "There were 0003 little pigs.And 1 bad wolf!"); } +TEST_P(CordTest, Stringify) { + absl::Cord c = + absl::MakeFragmentedCord({"A ", "small ", "fragmented ", "Cord", "."}); + MaybeHarden(c); + EXPECT_EQ(absl::StrCat(c), "A small fragmented Cord."); +} + TEST_P(CordTest, Hardening) { absl::Cord cord("hello"); MaybeHarden(cord); diff --git a/absl/strings/internal/str_format/arg.h b/absl/strings/internal/str_format/arg.h index 2e017549..20483aff 100644 --- a/absl/strings/internal/str_format/arg.h +++ b/absl/strings/internal/str_format/arg.h @@ -158,6 +158,7 @@ template <typename T> auto FormatConvertImpl(const T& v, FormatConversionSpecImpl, FormatSinkImpl* sink) -> std::enable_if_t<!std::is_enum<T>::value && + !std::is_same<T, absl::Cord>::value && std::is_void<decltype(AbslStringify( std::declval<FormatSink&>(), v))>::value, ArgConvertResult<FormatConversionCharSetInternal::v>> { |