summaryrefslogtreecommitdiff
path: root/absl/strings/cord.h
diff options
context:
space:
mode:
authorGravatar Martijn Vels <mvels@google.com>2022-07-15 13:39:31 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-07-15 13:40:26 -0700
commitb707b6c1799bf4c058d4145d9062825384c156bd (patch)
tree659f1f6758e73ea5eaad303e455dbf1f6a68c0f6 /absl/strings/cord.h
parentd6f96eda14aca77748a439f05a567a46ce87e462 (diff)
Add GetCustomAppendBuffer method to absl::Cord
The Cord::GetCustomAppendBuffer() method provides the same functionality as Cord::GetAppendBuffer(), except that callers can specify a custom block size/limit if the method returns a newly allocated buffer. In other words: Cord::GetAppendBuffer() defaults to CordBuffer::CreateWithDefaultLimit(), Cord::GetCustomAppendBuffer() defaults to CordBuffer::CreateWithCustomLimit(). PiperOrigin-RevId: 461231989 Change-Id: I5c03f31139d9b068feee1bea76d59e1c5e30ef07
Diffstat (limited to 'absl/strings/cord.h')
-rw-r--r--absl/strings/cord.h28
1 files changed, 26 insertions, 2 deletions
diff --git a/absl/strings/cord.h b/absl/strings/cord.h
index 18d6ab85..d0ebb870 100644
--- a/absl/strings/cord.h
+++ b/absl/strings/cord.h
@@ -284,6 +284,19 @@ class Cord {
// }
CordBuffer GetAppendBuffer(size_t capacity, size_t min_capacity = 16);
+ // Returns a CordBuffer, re-using potential existing capacity in this cord.
+ //
+ // This function is identical to `GetAppendBuffer`, except that in the case
+ // where a new `CordBuffer` is allocated, it is allocated using the provided
+ // custom limit instead of the default limit. `GetAppendBuffer` will default
+ // to `CordBuffer::CreateWithDefaultLimit(capacity)` whereas this method
+ // will default to `CordBuffer::CreateWithCustomLimit(block_size, capacity)`.
+ // This method is equivalent to `GetAppendBuffer` if `block_size` is zero.
+ // See the documentation for `CreateWithCustomLimit` for more details on the
+ // restrictions and legal values for `block_size`.
+ CordBuffer GetCustomAppendBuffer(size_t block_size, size_t capacity,
+ size_t min_capacity = 16);
+
// Cord::Prepend()
//
// Prepends data to the Cord, which may come from another Cord or other string
@@ -980,7 +993,8 @@ class Cord {
void AppendPrecise(absl::string_view src, MethodIdentifier method);
void PrependPrecise(absl::string_view src, MethodIdentifier method);
- CordBuffer GetAppendBufferSlowPath(size_t capacity, size_t min_capacity);
+ CordBuffer GetAppendBufferSlowPath(size_t block_size, size_t capacity,
+ size_t min_capacity);
// Prepends the provided data to this instance. `method` contains the public
// API method for this action which is tracked for Cordz sampling purposes.
@@ -1360,7 +1374,17 @@ inline void Cord::Prepend(CordBuffer buffer) {
inline CordBuffer Cord::GetAppendBuffer(size_t capacity, size_t min_capacity) {
if (empty()) return CordBuffer::CreateWithDefaultLimit(capacity);
- return GetAppendBufferSlowPath(capacity, min_capacity);
+ return GetAppendBufferSlowPath(0, capacity, min_capacity);
+}
+
+inline CordBuffer Cord::GetCustomAppendBuffer(size_t block_size,
+ size_t capacity,
+ size_t min_capacity) {
+ if (empty()) {
+ return block_size ? CordBuffer::CreateWithCustomLimit(block_size, capacity)
+ : CordBuffer::CreateWithDefaultLimit(capacity);
+ }
+ return GetAppendBufferSlowPath(block_size, capacity, min_capacity);
}
extern template void Cord::Append(std::string&& src);