about summary refs log tree commit diff
path: root/absl/strings/cord.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2020-03-05T16·37-0800
committerDerek Mauro <dmauro@google.com>2020-03-05T20·52+0000
commitcf3a1998e9d41709d4141e2f13375993cba1130e (patch)
tree9574720f5b23f1842a3e884fe79210508c8c6f39 /absl/strings/cord.h
parentb19ba96766db08b1f32605cb4424a0e7ea0c7584 (diff)
Export of internal Abseil changes
--
44ccc0320ffaa2106ba3c6393b5a40c3b4f7b901 by Abseil Team <absl-team@google.com>:

Clarify span iterator documentation.

PiperOrigin-RevId: 299110584

--
80d016d8026b8d6904aa0ff2d5e1c3ae27f129bb by Greg Falcon <gfalcon@google.com>:

Add Cord::TryFlat().

PiperOrigin-RevId: 298889772

--
da6900203f1e4131d5693cbca157b6dba099bbed by Greg Falcon <gfalcon@google.com>:

clang-format cord_test.cc.

PiperOrigin-RevId: 298851425
GitOrigin-RevId: 44ccc0320ffaa2106ba3c6393b5a40c3b4f7b901
Change-Id: Ia5394f6fbb473d206726fdd48a00eb07a6acad6a
Diffstat (limited to 'absl/strings/cord.h')
-rw-r--r--absl/strings/cord.h19
1 files changed, 18 insertions, 1 deletions
diff --git a/absl/strings/cord.h b/absl/strings/cord.h
index 68a7e52feb7a..29ed7f755d11 100644
--- a/absl/strings/cord.h
+++ b/absl/strings/cord.h
@@ -53,6 +53,7 @@
 #include "absl/strings/internal/cord_internal.h"
 #include "absl/strings/internal/resize_uninitialized.h"
 #include "absl/strings/string_view.h"
+#include "absl/types/optional.h"
 
 namespace absl {
 ABSL_NAMESPACE_BEGIN
@@ -512,6 +513,10 @@ class Cord {
   // REQUIRES: 0 <= i < size()
   char operator[](size_t i) const;
 
+  // If this cord's representation is a single flat array, return a
+  // string_view referencing that array.  Otherwise return nullopt.
+  absl::optional<absl::string_view> TryFlat() const;
+
   // Flattens the cord into a single array and returns a view of the data.
   //
   // If the cord was already flat, the contents are not modified.
@@ -630,7 +635,7 @@ class Cord {
   // Helper for MemoryUsage()
   static size_t MemoryUsageAux(const absl::cord_internal::CordRep* rep);
 
-  // Helper for GetFlat()
+  // Helper for GetFlat() and TryFlat()
   static bool GetFlatAux(absl::cord_internal::CordRep* rep,
                          absl::string_view* fragment);
 
@@ -942,6 +947,18 @@ inline size_t Cord::EstimatedMemoryUsage() const {
   return result;
 }
 
+inline absl::optional<absl::string_view> Cord::TryFlat() const {
+  absl::cord_internal::CordRep* rep = contents_.tree();
+  if (rep == nullptr) {
+    return absl::string_view(contents_.data(), contents_.size());
+  }
+  absl::string_view fragment;
+  if (GetFlatAux(rep, &fragment)) {
+    return fragment;
+  }
+  return absl::nullopt;
+}
+
 inline absl::string_view Cord::Flatten() {
   absl::cord_internal::CordRep* rep = contents_.tree();
   if (rep == nullptr) {