Coverage Report

Created: 2023-09-25 06:27

/src/abseil-cpp/absl/strings/cord.h
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 The Abseil Authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
//
15
// -----------------------------------------------------------------------------
16
// File: cord.h
17
// -----------------------------------------------------------------------------
18
//
19
// This file defines the `absl::Cord` data structure and operations on that data
20
// structure. A Cord is a string-like sequence of characters optimized for
21
// specific use cases. Unlike a `std::string`, which stores an array of
22
// contiguous characters, Cord data is stored in a structure consisting of
23
// separate, reference-counted "chunks."
24
//
25
// Because a Cord consists of these chunks, data can be added to or removed from
26
// a Cord during its lifetime. Chunks may also be shared between Cords. Unlike a
27
// `std::string`, a Cord can therefore accommodate data that changes over its
28
// lifetime, though it's not quite "mutable"; it can change only in the
29
// attachment, detachment, or rearrangement of chunks of its constituent data.
30
//
31
// A Cord provides some benefit over `std::string` under the following (albeit
32
// narrow) circumstances:
33
//
34
//   * Cord data is designed to grow and shrink over a Cord's lifetime. Cord
35
//     provides efficient insertions and deletions at the start and end of the
36
//     character sequences, avoiding copies in those cases. Static data should
37
//     generally be stored as strings.
38
//   * External memory consisting of string-like data can be directly added to
39
//     a Cord without requiring copies or allocations.
40
//   * Cord data may be shared and copied cheaply. Cord provides a copy-on-write
41
//     implementation and cheap sub-Cord operations. Copying a Cord is an O(1)
42
//     operation.
43
//
44
// As a consequence to the above, Cord data is generally large. Small data
45
// should generally use strings, as construction of a Cord requires some
46
// overhead. Small Cords (<= 15 bytes) are represented inline, but most small
47
// Cords are expected to grow over their lifetimes.
48
//
49
// Note that because a Cord is made up of separate chunked data, random access
50
// to character data within a Cord is slower than within a `std::string`.
51
//
52
// Thread Safety
53
//
54
// Cord has the same thread-safety properties as many other types like
55
// std::string, std::vector<>, int, etc -- it is thread-compatible. In
56
// particular, if threads do not call non-const methods, then it is safe to call
57
// const methods without synchronization. Copying a Cord produces a new instance
58
// that can be used concurrently with the original in arbitrary ways.
59
60
#ifndef ABSL_STRINGS_CORD_H_
61
#define ABSL_STRINGS_CORD_H_
62
63
#include <algorithm>
64
#include <cstddef>
65
#include <cstdint>
66
#include <cstring>
67
#include <iosfwd>
68
#include <iterator>
69
#include <string>
70
#include <type_traits>
71
72
#include "absl/base/attributes.h"
73
#include "absl/base/config.h"
74
#include "absl/base/internal/endian.h"
75
#include "absl/base/internal/per_thread_tls.h"
76
#include "absl/base/macros.h"
77
#include "absl/base/port.h"
78
#include "absl/container/inlined_vector.h"
79
#include "absl/crc/internal/crc_cord_state.h"
80
#include "absl/functional/function_ref.h"
81
#include "absl/meta/type_traits.h"
82
#include "absl/strings/cord_analysis.h"
83
#include "absl/strings/cord_buffer.h"
84
#include "absl/strings/internal/cord_data_edge.h"
85
#include "absl/strings/internal/cord_internal.h"
86
#include "absl/strings/internal/cord_rep_btree.h"
87
#include "absl/strings/internal/cord_rep_btree_reader.h"
88
#include "absl/strings/internal/cord_rep_crc.h"
89
#include "absl/strings/internal/cordz_functions.h"
90
#include "absl/strings/internal/cordz_info.h"
91
#include "absl/strings/internal/cordz_statistics.h"
92
#include "absl/strings/internal/cordz_update_scope.h"
93
#include "absl/strings/internal/cordz_update_tracker.h"
94
#include "absl/strings/internal/resize_uninitialized.h"
95
#include "absl/strings/internal/string_constant.h"
96
#include "absl/strings/string_view.h"
97
#include "absl/types/optional.h"
98
99
namespace absl {
100
ABSL_NAMESPACE_BEGIN
101
class Cord;
102
class CordTestPeer;
103
template <typename Releaser>
104
Cord MakeCordFromExternal(absl::string_view, Releaser&&);
105
void CopyCordToString(const Cord& src, std::string* dst);
106
107
// Cord memory accounting modes
108
enum class CordMemoryAccounting {
109
  // Counts the *approximate* number of bytes held in full or in part by this
110
  // Cord (which may not remain the same between invocations). Cords that share
111
  // memory could each be "charged" independently for the same shared memory.
112
  // See also comment on `kTotalMorePrecise` on internally shared memory.
113
  kTotal,
114
115
  // Counts the *approximate* number of bytes held in full or in part by this
116
  // Cord for the distinct memory held by this cord. This option is similar
117
  // to `kTotal`, except that if the cord has multiple references to the same
118
  // memory, that memory is only counted once.
119
  //
120
  // For example:
121
  //   absl::Cord cord;
122
  //   cord.append(some_other_cord);
123
  //   cord.append(some_other_cord);
124
  //   // Counts `some_other_cord` twice:
125
  //   cord.EstimatedMemoryUsage(kTotal);
126
  //   // Counts `some_other_cord` once:
127
  //   cord.EstimatedMemoryUsage(kTotalMorePrecise);
128
  //
129
  // The `kTotalMorePrecise` number is more expensive to compute as it requires
130
  // deduplicating all memory references. Applications should prefer to use
131
  // `kFairShare` or `kTotal` unless they really need a more precise estimate
132
  // on "how much memory is potentially held / kept alive by this cord?"
133
  kTotalMorePrecise,
134
135
  // Counts the *approximate* number of bytes held in full or in part by this
136
  // Cord weighted by the sharing ratio of that data. For example, if some data
137
  // edge is shared by 4 different Cords, then each cord is attributed 1/4th of
138
  // the total memory usage as a 'fair share' of the total memory usage.
139
  kFairShare,
140
};
141
142
// Cord
143
//
144
// A Cord is a sequence of characters, designed to be more efficient than a
145
// `std::string` in certain circumstances: namely, large string data that needs
146
// to change over its lifetime or shared, especially when such data is shared
147
// across API boundaries.
148
//
149
// A Cord stores its character data in a structure that allows efficient prepend
150
// and append operations. This makes a Cord useful for large string data sent
151
// over in a wire format that may need to be prepended or appended at some point
152
// during the data exchange (e.g. HTTP, protocol buffers). For example, a
153
// Cord is useful for storing an HTTP request, and prepending an HTTP header to
154
// such a request.
155
//
156
// Cords should not be used for storing general string data, however. They
157
// require overhead to construct and are slower than strings for random access.
158
//
159
// The Cord API provides the following common API operations:
160
//
161
// * Create or assign Cords out of existing string data, memory, or other Cords
162
// * Append and prepend data to an existing Cord
163
// * Create new Sub-Cords from existing Cord data
164
// * Swap Cord data and compare Cord equality
165
// * Write out Cord data by constructing a `std::string`
166
//
167
// Additionally, the API provides iterator utilities to iterate through Cord
168
// data via chunks or character bytes.
169
//
170
class Cord {
171
 private:
172
  template <typename T>
173
  using EnableIfString =
174
      absl::enable_if_t<std::is_same<T, std::string>::value, int>;
175
176
 public:
177
  // Cord::Cord() Constructors.
178
179
  // Creates an empty Cord.
180
  constexpr Cord() noexcept;
181
182
  // Creates a Cord from an existing Cord. Cord is copyable and efficiently
183
  // movable. The moved-from state is valid but unspecified.
184
  Cord(const Cord& src);
185
  Cord(Cord&& src) noexcept;
186
  Cord& operator=(const Cord& x);
187
  Cord& operator=(Cord&& x) noexcept;
188
189
  // Creates a Cord from a `src` string. This constructor is marked explicit to
190
  // prevent implicit Cord constructions from arguments convertible to an
191
  // `absl::string_view`.
192
  explicit Cord(absl::string_view src);
193
  Cord& operator=(absl::string_view src);
194
195
  // Creates a Cord from a `std::string&&` rvalue. These constructors are
196
  // templated to avoid ambiguities for types that are convertible to both
197
  // `absl::string_view` and `std::string`, such as `const char*`.
198
  template <typename T, EnableIfString<T> = 0>
199
  explicit Cord(T&& src);
200
  template <typename T, EnableIfString<T> = 0>
201
  Cord& operator=(T&& src);
202
203
  // Cord::~Cord()
204
  //
205
  // Destructs the Cord.
206
0
  ~Cord() {
207
0
    if (contents_.is_tree()) DestroyCordSlow();
208
0
  }
209
210
  // MakeCordFromExternal()
211
  //
212
  // Creates a Cord that takes ownership of external string memory. The
213
  // contents of `data` are not copied to the Cord; instead, the external
214
  // memory is added to the Cord and reference-counted. This data may not be
215
  // changed for the life of the Cord, though it may be prepended or appended
216
  // to.
217
  //
218
  // `MakeCordFromExternal()` takes a callable "releaser" that is invoked when
219
  // the reference count for `data` reaches zero. As noted above, this data must
220
  // remain live until the releaser is invoked. The callable releaser also must:
221
  //
222
  //   * be move constructible
223
  //   * support `void operator()(absl::string_view) const` or `void operator()`
224
  //
225
  // Example:
226
  //
227
  // Cord MakeCord(BlockPool* pool) {
228
  //   Block* block = pool->NewBlock();
229
  //   FillBlock(block);
230
  //   return absl::MakeCordFromExternal(
231
  //       block->ToStringView(),
232
  //       [pool, block](absl::string_view v) {
233
  //         pool->FreeBlock(block, v);
234
  //       });
235
  // }
236
  //
237
  // WARNING: Because a Cord can be reference-counted, it's likely a bug if your
238
  // releaser doesn't do anything. For example, consider the following:
239
  //
240
  // void Foo(const char* buffer, int len) {
241
  //   auto c = absl::MakeCordFromExternal(absl::string_view(buffer, len),
242
  //                                       [](absl::string_view) {});
243
  //
244
  //   // BUG: If Bar() copies its cord for any reason, including keeping a
245
  //   // substring of it, the lifetime of buffer might be extended beyond
246
  //   // when Foo() returns.
247
  //   Bar(c);
248
  // }
249
  template <typename Releaser>
250
  friend Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser);
251
252
  // Cord::Clear()
253
  //
254
  // Releases the Cord data. Any nodes that share data with other Cords, if
255
  // applicable, will have their reference counts reduced by 1.
256
  ABSL_ATTRIBUTE_REINITIALIZES void Clear();
257
258
  // Cord::Append()
259
  //
260
  // Appends data to the Cord, which may come from another Cord or other string
261
  // data.
262
  void Append(const Cord& src);
263
  void Append(Cord&& src);
264
  void Append(absl::string_view src);
265
  template <typename T, EnableIfString<T> = 0>
266
  void Append(T&& src);
267
268
  // Appends `buffer` to this cord, unless `buffer` has a zero length in which
269
  // case this method has no effect on this cord instance.
270
  // This method is guaranteed to consume `buffer`.
271
  void Append(CordBuffer buffer);
272
273
  // Returns a CordBuffer, re-using potential existing capacity in this cord.
274
  //
275
  // Cord instances may have additional unused capacity in the last (or first)
276
  // nodes of the underlying tree to facilitate amortized growth. This method
277
  // allows applications to explicitly use this spare capacity if available,
278
  // or create a new CordBuffer instance otherwise.
279
  // If this cord has a final non-shared node with at least `min_capacity`
280
  // available, then this method will return that buffer including its data
281
  // contents. I.e.; the returned buffer will have a non-zero length, and
282
  // a capacity of at least `buffer.length + min_capacity`. Otherwise, this
283
  // method will return `CordBuffer::CreateWithDefaultLimit(capacity)`.
284
  //
285
  // Below an example of using GetAppendBuffer. Notice that in this example we
286
  // use `GetAppendBuffer()` only on the first iteration. As we know nothing
287
  // about any initial extra capacity in `cord`, we may be able to use the extra
288
  // capacity. But as we add new buffers with fully utilized contents after that
289
  // we avoid calling `GetAppendBuffer()` on subsequent iterations: while this
290
  // works fine, it results in an unnecessary inspection of cord contents:
291
  //
292
  //   void AppendRandomDataToCord(absl::Cord &cord, size_t n) {
293
  //     bool first = true;
294
  //     while (n > 0) {
295
  //       CordBuffer buffer = first ? cord.GetAppendBuffer(n)
296
  //                                 : CordBuffer::CreateWithDefaultLimit(n);
297
  //       absl::Span<char> data = buffer.available_up_to(n);
298
  //       FillRandomValues(data.data(), data.size());
299
  //       buffer.IncreaseLengthBy(data.size());
300
  //       cord.Append(std::move(buffer));
301
  //       n -= data.size();
302
  //       first = false;
303
  //     }
304
  //   }
305
  CordBuffer GetAppendBuffer(size_t capacity, size_t min_capacity = 16);
306
307
  // Returns a CordBuffer, re-using potential existing capacity in this cord.
308
  //
309
  // This function is identical to `GetAppendBuffer`, except that in the case
310
  // where a new `CordBuffer` is allocated, it is allocated using the provided
311
  // custom limit instead of the default limit. `GetAppendBuffer` will default
312
  // to `CordBuffer::CreateWithDefaultLimit(capacity)` whereas this method
313
  // will default to `CordBuffer::CreateWithCustomLimit(block_size, capacity)`.
314
  // This method is equivalent to `GetAppendBuffer` if `block_size` is zero.
315
  // See the documentation for `CreateWithCustomLimit` for more details on the
316
  // restrictions and legal values for `block_size`.
317
  CordBuffer GetCustomAppendBuffer(size_t block_size, size_t capacity,
318
                                   size_t min_capacity = 16);
319
320
  // Cord::Prepend()
321
  //
322
  // Prepends data to the Cord, which may come from another Cord or other string
323
  // data.
324
  void Prepend(const Cord& src);
325
  void Prepend(absl::string_view src);
326
  template <typename T, EnableIfString<T> = 0>
327
  void Prepend(T&& src);
328
329
  // Prepends `buffer` to this cord, unless `buffer` has a zero length in which
330
  // case this method has no effect on this cord instance.
331
  // This method is guaranteed to consume `buffer`.
332
  void Prepend(CordBuffer buffer);
333
334
  // Cord::RemovePrefix()
335
  //
336
  // Removes the first `n` bytes of a Cord.
337
  void RemovePrefix(size_t n);
338
  void RemoveSuffix(size_t n);
339
340
  // Cord::Subcord()
341
  //
342
  // Returns a new Cord representing the subrange [pos, pos + new_size) of
343
  // *this. If pos >= size(), the result is empty(). If
344
  // (pos + new_size) >= size(), the result is the subrange [pos, size()).
345
  Cord Subcord(size_t pos, size_t new_size) const;
346
347
  // Cord::swap()
348
  //
349
  // Swaps the contents of the Cord with `other`.
350
  void swap(Cord& other) noexcept;
351
352
  // swap()
353
  //
354
  // Swaps the contents of two Cords.
355
0
  friend void swap(Cord& x, Cord& y) noexcept { x.swap(y); }
356
357
  // Cord::size()
358
  //
359
  // Returns the size of the Cord.
360
  size_t size() const;
361
362
  // Cord::empty()
363
  //
364
  // Determines whether the given Cord is empty, returning `true` is so.
365
  bool empty() const;
366
367
  // Cord::EstimatedMemoryUsage()
368
  //
369
  // Returns the *approximate* number of bytes held by this cord.
370
  // See CordMemoryAccounting for more information on the accounting method.
371
  size_t EstimatedMemoryUsage(CordMemoryAccounting accounting_method =
372
                                  CordMemoryAccounting::kTotal) const;
373
374
  // Cord::Compare()
375
  //
376
  // Compares 'this' Cord with rhs. This function and its relatives treat Cords
377
  // as sequences of unsigned bytes. The comparison is a straightforward
378
  // lexicographic comparison. `Cord::Compare()` returns values as follows:
379
  //
380
  //   -1  'this' Cord is smaller
381
  //    0  two Cords are equal
382
  //    1  'this' Cord is larger
383
  int Compare(absl::string_view rhs) const;
384
  int Compare(const Cord& rhs) const;
385
386
  // Cord::StartsWith()
387
  //
388
  // Determines whether the Cord starts with the passed string data `rhs`.
389
  bool StartsWith(const Cord& rhs) const;
390
  bool StartsWith(absl::string_view rhs) const;
391
392
  // Cord::EndsWith()
393
  //
394
  // Determines whether the Cord ends with the passed string data `rhs`.
395
  bool EndsWith(absl::string_view rhs) const;
396
  bool EndsWith(const Cord& rhs) const;
397
398
  // Cord::Contains()
399
  //
400
  // Determines whether the Cord contains the passed string data `rhs`.
401
  bool Contains(absl::string_view rhs) const;
402
  bool Contains(const Cord& rhs) const;
403
404
  // Cord::operator std::string()
405
  //
406
  // Converts a Cord into a `std::string()`. This operator is marked explicit to
407
  // prevent unintended Cord usage in functions that take a string.
408
  explicit operator std::string() const;
409
410
  // CopyCordToString()
411
  //
412
  // Copies the contents of a `src` Cord into a `*dst` string.
413
  //
414
  // This function optimizes the case of reusing the destination string since it
415
  // can reuse previously allocated capacity. However, this function does not
416
  // guarantee that pointers previously returned by `dst->data()` remain valid
417
  // even if `*dst` had enough capacity to hold `src`. If `*dst` is a new
418
  // object, prefer to simply use the conversion operator to `std::string`.
419
  friend void CopyCordToString(const Cord& src, std::string* dst);
420
421
  class CharIterator;
422
423
  //----------------------------------------------------------------------------
424
  // Cord::ChunkIterator
425
  //----------------------------------------------------------------------------
426
  //
427
  // A `Cord::ChunkIterator` allows iteration over the constituent chunks of its
428
  // Cord. Such iteration allows you to perform non-const operations on the data
429
  // of a Cord without modifying it.
430
  //
431
  // Generally, you do not instantiate a `Cord::ChunkIterator` directly;
432
  // instead, you create one implicitly through use of the `Cord::Chunks()`
433
  // member function.
434
  //
435
  // The `Cord::ChunkIterator` has the following properties:
436
  //
437
  //   * The iterator is invalidated after any non-const operation on the
438
  //     Cord object over which it iterates.
439
  //   * The `string_view` returned by dereferencing a valid, non-`end()`
440
  //     iterator is guaranteed to be non-empty.
441
  //   * Two `ChunkIterator` objects can be compared equal if and only if they
442
  //     remain valid and iterate over the same Cord.
443
  //   * The iterator in this case is a proxy iterator; the `string_view`
444
  //     returned by the iterator does not live inside the Cord, and its
445
  //     lifetime is limited to the lifetime of the iterator itself. To help
446
  //     prevent lifetime issues, `ChunkIterator::reference` is not a true
447
  //     reference type and is equivalent to `value_type`.
448
  //   * The iterator keeps state that can grow for Cords that contain many
449
  //     nodes and are imbalanced due to sharing. Prefer to pass this type by
450
  //     const reference instead of by value.
451
  class ChunkIterator {
452
   public:
453
    using iterator_category = std::input_iterator_tag;
454
    using value_type = absl::string_view;
455
    using difference_type = ptrdiff_t;
456
    using pointer = const value_type*;
457
    using reference = value_type;
458
459
    ChunkIterator() = default;
460
461
    ChunkIterator& operator++();
462
    ChunkIterator operator++(int);
463
    bool operator==(const ChunkIterator& other) const;
464
    bool operator!=(const ChunkIterator& other) const;
465
    reference operator*() const;
466
    pointer operator->() const;
467
468
    friend class Cord;
469
    friend class CharIterator;
470
471
   private:
472
    using CordRep = absl::cord_internal::CordRep;
473
    using CordRepBtree = absl::cord_internal::CordRepBtree;
474
    using CordRepBtreeReader = absl::cord_internal::CordRepBtreeReader;
475
476
    // Constructs a `begin()` iterator from `tree`. `tree` must not be null.
477
    explicit ChunkIterator(cord_internal::CordRep* tree);
478
479
    // Constructs a `begin()` iterator from `cord`.
480
    explicit ChunkIterator(const Cord* cord);
481
482
    // Initializes this instance from a tree. Invoked by constructors.
483
    void InitTree(cord_internal::CordRep* tree);
484
485
    // Removes `n` bytes from `current_chunk_`. Expects `n` to be smaller than
486
    // `current_chunk_.size()`.
487
    void RemoveChunkPrefix(size_t n);
488
    Cord AdvanceAndReadBytes(size_t n);
489
    void AdvanceBytes(size_t n);
490
491
    // Btree specific operator++
492
    ChunkIterator& AdvanceBtree();
493
    void AdvanceBytesBtree(size_t n);
494
495
    // A view into bytes of the current `CordRep`. It may only be a view to a
496
    // suffix of bytes if this is being used by `CharIterator`.
497
    absl::string_view current_chunk_;
498
    // The current leaf, or `nullptr` if the iterator points to short data.
499
    // If the current chunk is a substring node, current_leaf_ points to the
500
    // underlying flat or external node.
501
    absl::cord_internal::CordRep* current_leaf_ = nullptr;
502
    // The number of bytes left in the `Cord` over which we are iterating.
503
    size_t bytes_remaining_ = 0;
504
505
    // Cord reader for cord btrees. Empty if not traversing a btree.
506
    CordRepBtreeReader btree_reader_;
507
  };
508
509
  // Cord::chunk_begin()
510
  //
511
  // Returns an iterator to the first chunk of the `Cord`.
512
  //
513
  // Generally, prefer using `Cord::Chunks()` within a range-based for loop for
514
  // iterating over the chunks of a Cord. This method may be useful for getting
515
  // a `ChunkIterator` where range-based for-loops are not useful.
516
  //
517
  // Example:
518
  //
519
  //   absl::Cord::ChunkIterator FindAsChunk(const absl::Cord& c,
520
  //                                         absl::string_view s) {
521
  //     return std::find(c.chunk_begin(), c.chunk_end(), s);
522
  //   }
523
  ChunkIterator chunk_begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
524
525
  // Cord::chunk_end()
526
  //
527
  // Returns an iterator one increment past the last chunk of the `Cord`.
528
  //
529
  // Generally, prefer using `Cord::Chunks()` within a range-based for loop for
530
  // iterating over the chunks of a Cord. This method may be useful for getting
531
  // a `ChunkIterator` where range-based for-loops may not be available.
532
  ChunkIterator chunk_end() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
533
534
  //----------------------------------------------------------------------------
535
  // Cord::ChunkRange
536
  //----------------------------------------------------------------------------
537
  //
538
  // `ChunkRange` is a helper class for iterating over the chunks of the `Cord`,
539
  // producing an iterator which can be used within a range-based for loop.
540
  // Construction of a `ChunkRange` will return an iterator pointing to the
541
  // first chunk of the Cord. Generally, do not construct a `ChunkRange`
542
  // directly; instead, prefer to use the `Cord::Chunks()` method.
543
  //
544
  // Implementation note: `ChunkRange` is simply a convenience wrapper over
545
  // `Cord::chunk_begin()` and `Cord::chunk_end()`.
546
  class ChunkRange {
547
   public:
548
    // Fulfill minimum c++ container requirements [container.requirements]
549
    // These (partial) container type definitions allow ChunkRange to be used
550
    // in various utilities expecting a subset of [container.requirements].
551
    // For example, the below enables using `::testing::ElementsAre(...)`
552
    using value_type = absl::string_view;
553
    using reference = value_type&;
554
    using const_reference = const value_type&;
555
    using iterator = ChunkIterator;
556
    using const_iterator = ChunkIterator;
557
558
0
    explicit ChunkRange(const Cord* cord) : cord_(cord) {}
559
560
    ChunkIterator begin() const;
561
    ChunkIterator end() const;
562
563
   private:
564
    const Cord* cord_;
565
  };
566
567
  // Cord::Chunks()
568
  //
569
  // Returns a `Cord::ChunkRange` for iterating over the chunks of a `Cord` with
570
  // a range-based for-loop. For most iteration tasks on a Cord, use
571
  // `Cord::Chunks()` to retrieve this iterator.
572
  //
573
  // Example:
574
  //
575
  //   void ProcessChunks(const Cord& cord) {
576
  //     for (absl::string_view chunk : cord.Chunks()) { ... }
577
  //   }
578
  //
579
  // Note that the ordinary caveats of temporary lifetime extension apply:
580
  //
581
  //   void Process() {
582
  //     for (absl::string_view chunk : CordFactory().Chunks()) {
583
  //       // The temporary Cord returned by CordFactory has been destroyed!
584
  //     }
585
  //   }
586
  ChunkRange Chunks() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
587
588
  //----------------------------------------------------------------------------
589
  // Cord::CharIterator
590
  //----------------------------------------------------------------------------
591
  //
592
  // A `Cord::CharIterator` allows iteration over the constituent characters of
593
  // a `Cord`.
594
  //
595
  // Generally, you do not instantiate a `Cord::CharIterator` directly; instead,
596
  // you create one implicitly through use of the `Cord::Chars()` member
597
  // function.
598
  //
599
  // A `Cord::CharIterator` has the following properties:
600
  //
601
  //   * The iterator is invalidated after any non-const operation on the
602
  //     Cord object over which it iterates.
603
  //   * Two `CharIterator` objects can be compared equal if and only if they
604
  //     remain valid and iterate over the same Cord.
605
  //   * The iterator keeps state that can grow for Cords that contain many
606
  //     nodes and are imbalanced due to sharing. Prefer to pass this type by
607
  //     const reference instead of by value.
608
  //   * This type cannot act as a forward iterator because a `Cord` can reuse
609
  //     sections of memory. This fact violates the requirement for forward
610
  //     iterators to compare equal if dereferencing them returns the same
611
  //     object.
612
  class CharIterator {
613
   public:
614
    using iterator_category = std::input_iterator_tag;
615
    using value_type = char;
616
    using difference_type = ptrdiff_t;
617
    using pointer = const char*;
618
    using reference = const char&;
619
620
    CharIterator() = default;
621
622
    CharIterator& operator++();
623
    CharIterator operator++(int);
624
    bool operator==(const CharIterator& other) const;
625
    bool operator!=(const CharIterator& other) const;
626
    reference operator*() const;
627
    pointer operator->() const;
628
629
    friend Cord;
630
631
   private:
632
0
    explicit CharIterator(const Cord* cord) : chunk_iterator_(cord) {}
633
634
    ChunkIterator chunk_iterator_;
635
  };
636
637
  // Cord::AdvanceAndRead()
638
  //
639
  // Advances the `Cord::CharIterator` by `n_bytes` and returns the bytes
640
  // advanced as a separate `Cord`. `n_bytes` must be less than or equal to the
641
  // number of bytes within the Cord; otherwise, behavior is undefined. It is
642
  // valid to pass `char_end()` and `0`.
643
  static Cord AdvanceAndRead(CharIterator* it, size_t n_bytes);
644
645
  // Cord::Advance()
646
  //
647
  // Advances the `Cord::CharIterator` by `n_bytes`. `n_bytes` must be less than
648
  // or equal to the number of bytes remaining within the Cord; otherwise,
649
  // behavior is undefined. It is valid to pass `char_end()` and `0`.
650
  static void Advance(CharIterator* it, size_t n_bytes);
651
652
  // Cord::ChunkRemaining()
653
  //
654
  // Returns the longest contiguous view starting at the iterator's position.
655
  //
656
  // `it` must be dereferenceable.
657
  static absl::string_view ChunkRemaining(const CharIterator& it);
658
659
  // Cord::char_begin()
660
  //
661
  // Returns an iterator to the first character of the `Cord`.
662
  //
663
  // Generally, prefer using `Cord::Chars()` within a range-based for loop for
664
  // iterating over the chunks of a Cord. This method may be useful for getting
665
  // a `CharIterator` where range-based for-loops may not be available.
666
  CharIterator char_begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
667
668
  // Cord::char_end()
669
  //
670
  // Returns an iterator to one past the last character of the `Cord`.
671
  //
672
  // Generally, prefer using `Cord::Chars()` within a range-based for loop for
673
  // iterating over the chunks of a Cord. This method may be useful for getting
674
  // a `CharIterator` where range-based for-loops are not useful.
675
  CharIterator char_end() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
676
677
  // Cord::CharRange
678
  //
679
  // `CharRange` is a helper class for iterating over the characters of a
680
  // producing an iterator which can be used within a range-based for loop.
681
  // Construction of a `CharRange` will return an iterator pointing to the first
682
  // character of the Cord. Generally, do not construct a `CharRange` directly;
683
  // instead, prefer to use the `Cord::Chars()` method shown below.
684
  //
685
  // Implementation note: `CharRange` is simply a convenience wrapper over
686
  // `Cord::char_begin()` and `Cord::char_end()`.
687
  class CharRange {
688
   public:
689
    // Fulfill minimum c++ container requirements [container.requirements]
690
    // These (partial) container type definitions allow CharRange to be used
691
    // in various utilities expecting a subset of [container.requirements].
692
    // For example, the below enables using `::testing::ElementsAre(...)`
693
    using value_type = char;
694
    using reference = value_type&;
695
    using const_reference = const value_type&;
696
    using iterator = CharIterator;
697
    using const_iterator = CharIterator;
698
699
0
    explicit CharRange(const Cord* cord) : cord_(cord) {}
700
701
    CharIterator begin() const;
702
    CharIterator end() const;
703
704
   private:
705
    const Cord* cord_;
706
  };
707
708
  // Cord::Chars()
709
  //
710
  // Returns a `Cord::CharRange` for iterating over the characters of a `Cord`
711
  // with a range-based for-loop. For most character-based iteration tasks on a
712
  // Cord, use `Cord::Chars()` to retrieve this iterator.
713
  //
714
  // Example:
715
  //
716
  //   void ProcessCord(const Cord& cord) {
717
  //     for (char c : cord.Chars()) { ... }
718
  //   }
719
  //
720
  // Note that the ordinary caveats of temporary lifetime extension apply:
721
  //
722
  //   void Process() {
723
  //     for (char c : CordFactory().Chars()) {
724
  //       // The temporary Cord returned by CordFactory has been destroyed!
725
  //     }
726
  //   }
727
  CharRange Chars() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
728
729
  // Cord::operator[]
730
  //
731
  // Gets the "i"th character of the Cord and returns it, provided that
732
  // 0 <= i < Cord.size().
733
  //
734
  // NOTE: This routine is reasonably efficient. It is roughly
735
  // logarithmic based on the number of chunks that make up the cord. Still,
736
  // if you need to iterate over the contents of a cord, you should
737
  // use a CharIterator/ChunkIterator rather than call operator[] or Get()
738
  // repeatedly in a loop.
739
  char operator[](size_t i) const;
740
741
  // Cord::TryFlat()
742
  //
743
  // If this cord's representation is a single flat array, returns a
744
  // string_view referencing that array.  Otherwise returns nullopt.
745
  absl::optional<absl::string_view> TryFlat() const
746
      ABSL_ATTRIBUTE_LIFETIME_BOUND;
747
748
  // Cord::Flatten()
749
  //
750
  // Flattens the cord into a single array and returns a view of the data.
751
  //
752
  // If the cord was already flat, the contents are not modified.
753
  absl::string_view Flatten() ABSL_ATTRIBUTE_LIFETIME_BOUND;
754
755
  // Cord::Find()
756
  //
757
  // Returns an iterator to the first occurrance of the substring `needle`.
758
  //
759
  // If the substring `needle` does not occur, `Cord::char_end()` is returned.
760
  CharIterator Find(absl::string_view needle) const;
761
  CharIterator Find(const absl::Cord& needle) const;
762
763
  // Supports absl::Cord as a sink object for absl::Format().
764
0
  friend void AbslFormatFlush(absl::Cord* cord, absl::string_view part) {
765
0
    cord->Append(part);
766
0
  }
767
768
  // Cord::SetExpectedChecksum()
769
  //
770
  // Stores a checksum value with this non-empty cord instance, for later
771
  // retrieval.
772
  //
773
  // The expected checksum is a number stored out-of-band, alongside the data.
774
  // It is preserved across copies and assignments, but any mutations to a cord
775
  // will cause it to lose its expected checksum.
776
  //
777
  // The expected checksum is not part of a Cord's value, and does not affect
778
  // operations such as equality or hashing.
779
  //
780
  // This field is intended to store a CRC32C checksum for later validation, to
781
  // help support end-to-end checksum workflows.  However, the Cord API itself
782
  // does no CRC validation, and assigns no meaning to this number.
783
  //
784
  // This call has no effect if this cord is empty.
785
  void SetExpectedChecksum(uint32_t crc);
786
787
  // Returns this cord's expected checksum, if it has one.  Otherwise, returns
788
  // nullopt.
789
  absl::optional<uint32_t> ExpectedChecksum() const;
790
791
  template <typename H>
792
0
  friend H AbslHashValue(H hash_state, const absl::Cord& c) {
793
0
    absl::optional<absl::string_view> maybe_flat = c.TryFlat();
794
0
    if (maybe_flat.has_value()) {
795
0
      return H::combine(std::move(hash_state), *maybe_flat);
796
0
    }
797
0
    return c.HashFragmented(std::move(hash_state));
798
0
  }
799
800
  // Create a Cord with the contents of StringConstant<T>::value.
801
  // No allocations will be done and no data will be copied.
802
  // This is an INTERNAL API and subject to change or removal. This API can only
803
  // be used by spelling absl::strings_internal::MakeStringConstant, which is
804
  // also an internal API.
805
  template <typename T>
806
  // NOLINTNEXTLINE(google-explicit-constructor)
807
  constexpr Cord(strings_internal::StringConstant<T>);
808
809
 private:
810
  using CordRep = absl::cord_internal::CordRep;
811
  using CordRepFlat = absl::cord_internal::CordRepFlat;
812
  using CordzInfo = cord_internal::CordzInfo;
813
  using CordzUpdateScope = cord_internal::CordzUpdateScope;
814
  using CordzUpdateTracker = cord_internal::CordzUpdateTracker;
815
  using InlineData = cord_internal::InlineData;
816
  using MethodIdentifier = CordzUpdateTracker::MethodIdentifier;
817
818
  // Creates a cord instance with `method` representing the originating
819
  // public API call causing the cord to be created.
820
  explicit Cord(absl::string_view src, MethodIdentifier method);
821
822
  friend class CordTestPeer;
823
  friend bool operator==(const Cord& lhs, const Cord& rhs);
824
  friend bool operator==(const Cord& lhs, absl::string_view rhs);
825
826
  friend const CordzInfo* GetCordzInfoForTesting(const Cord& cord);
827
828
  // Calls the provided function once for each cord chunk, in order.  Unlike
829
  // Chunks(), this API will not allocate memory.
830
  void ForEachChunk(absl::FunctionRef<void(absl::string_view)>) const;
831
832
  // Allocates new contiguous storage for the contents of the cord. This is
833
  // called by Flatten() when the cord was not already flat.
834
  absl::string_view FlattenSlowPath();
835
836
  // Actual cord contents are hidden inside the following simple
837
  // class so that we can isolate the bulk of cord.cc from changes
838
  // to the representation.
839
  //
840
  // InlineRep holds either a tree pointer, or an array of kMaxInline bytes.
841
  class InlineRep {
842
   public:
843
    static constexpr unsigned char kMaxInline = cord_internal::kMaxInline;
844
    static_assert(kMaxInline >= sizeof(absl::cord_internal::CordRep*), "");
845
846
0
    constexpr InlineRep() : data_() {}
847
0
    explicit InlineRep(InlineData::DefaultInitType init) : data_(init) {}
848
    InlineRep(const InlineRep& src);
849
    InlineRep(InlineRep&& src);
850
    InlineRep& operator=(const InlineRep& src);
851
    InlineRep& operator=(InlineRep&& src) noexcept;
852
853
    explicit constexpr InlineRep(absl::string_view sv, CordRep* rep);
854
855
    void Swap(InlineRep* rhs);
856
    size_t size() const;
857
    const char* data() const;  // Returns nullptr if holding pointer
858
    void set_data(const char* data, size_t n);  // Discards pointer, if any
859
    char* set_data(size_t n);                   // Write data to the result
860
    // Returns nullptr if holding bytes
861
    absl::cord_internal::CordRep* tree() const;
862
    absl::cord_internal::CordRep* as_tree() const;
863
    const char* as_chars() const;
864
    // Returns non-null iff was holding a pointer
865
    absl::cord_internal::CordRep* clear();
866
    // Converts to pointer if necessary.
867
    void reduce_size(size_t n);    // REQUIRES: holding data
868
    void remove_prefix(size_t n);  // REQUIRES: holding data
869
    void AppendArray(absl::string_view src, MethodIdentifier method);
870
    absl::string_view FindFlatStartPiece() const;
871
872
    // Creates a CordRepFlat instance from the current inlined data with `extra'
873
    // bytes of desired additional capacity.
874
    CordRepFlat* MakeFlatWithExtraCapacity(size_t extra);
875
876
    // Sets the tree value for this instance. `rep` must not be null.
877
    // Requires the current instance to hold a tree, and a lock to be held on
878
    // any CordzInfo referenced by this instance. The latter is enforced through
879
    // the CordzUpdateScope argument. If the current instance is sampled, then
880
    // the CordzInfo instance is updated to reference the new `rep` value.
881
    void SetTree(CordRep* rep, const CordzUpdateScope& scope);
882
883
    // Identical to SetTree(), except that `rep` is allowed to be null, in
884
    // which case the current instance is reset to an empty value.
885
    void SetTreeOrEmpty(CordRep* rep, const CordzUpdateScope& scope);
886
887
    // Sets the tree value for this instance, and randomly samples this cord.
888
    // This function disregards existing contents in `data_`, and should be
889
    // called when a Cord is 'promoted' from an 'uninitialized' or 'inlined'
890
    // value to a non-inlined (tree / ring) value.
891
    void EmplaceTree(CordRep* rep, MethodIdentifier method);
892
893
    // Identical to EmplaceTree, except that it copies the parent stack from
894
    // the provided `parent` data if the parent is sampled.
895
    void EmplaceTree(CordRep* rep, const InlineData& parent,
896
                     MethodIdentifier method);
897
898
    // Commits the change of a newly created, or updated `rep` root value into
899
    // this cord. `old_rep` indicates the old (inlined or tree) value of the
900
    // cord, and determines if the commit invokes SetTree() or EmplaceTree().
901
    void CommitTree(const CordRep* old_rep, CordRep* rep,
902
                    const CordzUpdateScope& scope, MethodIdentifier method);
903
904
    void AppendTreeToInlined(CordRep* tree, MethodIdentifier method);
905
    void AppendTreeToTree(CordRep* tree, MethodIdentifier method);
906
    void AppendTree(CordRep* tree, MethodIdentifier method);
907
    void PrependTreeToInlined(CordRep* tree, MethodIdentifier method);
908
    void PrependTreeToTree(CordRep* tree, MethodIdentifier method);
909
    void PrependTree(CordRep* tree, MethodIdentifier method);
910
911
0
    bool IsSame(const InlineRep& other) const { return data_ == other.data_; }
912
913
0
    void CopyTo(std::string* dst) const {
914
0
      // memcpy is much faster when operating on a known size. On most supported
915
0
      // platforms, the small string optimization is large enough that resizing
916
0
      // to 15 bytes does not cause a memory allocation.
917
0
      absl::strings_internal::STLStringResizeUninitialized(dst, kMaxInline);
918
0
      data_.copy_max_inline_to(&(*dst)[0]);
919
0
      // erase is faster than resize because the logic for memory allocation is
920
0
      // not needed.
921
0
      dst->erase(inline_size());
922
0
    }
923
924
    // Copies the inline contents into `dst`. Assumes the cord is not empty.
925
    void CopyToArray(char* dst) const;
926
927
0
    bool is_tree() const { return data_.is_tree(); }
928
929
    // Returns true if the Cord is being profiled by cordz.
930
0
    bool is_profiled() const { return data_.is_tree() && data_.is_profiled(); }
931
932
    // Returns the available inlined capacity, or 0 if is_tree() == true.
933
0
    size_t remaining_inline_capacity() const {
934
0
      return data_.is_tree() ? 0 : kMaxInline - data_.inline_size();
935
0
    }
936
937
    // Returns the profiled CordzInfo, or nullptr if not sampled.
938
0
    absl::cord_internal::CordzInfo* cordz_info() const {
939
0
      return data_.cordz_info();
940
0
    }
941
942
    // Sets the profiled CordzInfo. `cordz_info` must not be null.
943
0
    void set_cordz_info(cord_internal::CordzInfo* cordz_info) {
944
0
      assert(cordz_info != nullptr);
945
0
      data_.set_cordz_info(cordz_info);
946
0
    }
947
948
    // Resets the current cordz_info to null / empty.
949
0
    void clear_cordz_info() { data_.clear_cordz_info(); }
950
951
   private:
952
    friend class Cord;
953
954
    void AssignSlow(const InlineRep& src);
955
    // Unrefs the tree and stops profiling.
956
    void UnrefTree();
957
958
0
    void ResetToEmpty() { data_ = {}; }
959
960
0
    void set_inline_size(size_t size) { data_.set_inline_size(size); }
961
0
    size_t inline_size() const { return data_.inline_size(); }
962
963
    // Empty cords that carry a checksum have a CordRepCrc node with a null
964
    // child node. The code can avoid lots of special cases where it would
965
    // otherwise transition from tree to inline storage if we just remove the
966
    // CordRepCrc node before mutations. Must never be called inside a
967
    // CordzUpdateScope since it untracks the cordz info.
968
    void MaybeRemoveEmptyCrcNode();
969
970
    cord_internal::InlineData data_;
971
  };
972
  InlineRep contents_;
973
974
  // Helper for GetFlat() and TryFlat().
975
  static bool GetFlatAux(absl::cord_internal::CordRep* rep,
976
                         absl::string_view* fragment);
977
978
  // Helper for ForEachChunk().
979
  static void ForEachChunkAux(
980
      absl::cord_internal::CordRep* rep,
981
      absl::FunctionRef<void(absl::string_view)> callback);
982
983
  // The destructor for non-empty Cords.
984
  void DestroyCordSlow();
985
986
  // Out-of-line implementation of slower parts of logic.
987
  void CopyToArraySlowPath(char* dst) const;
988
  int CompareSlowPath(absl::string_view rhs, size_t compared_size,
989
                      size_t size_to_compare) const;
990
  int CompareSlowPath(const Cord& rhs, size_t compared_size,
991
                      size_t size_to_compare) const;
992
  bool EqualsImpl(absl::string_view rhs, size_t size_to_compare) const;
993
  bool EqualsImpl(const Cord& rhs, size_t size_to_compare) const;
994
  int CompareImpl(const Cord& rhs) const;
995
996
  template <typename ResultType, typename RHS>
997
  friend ResultType GenericCompare(const Cord& lhs, const RHS& rhs,
998
                                   size_t size_to_compare);
999
  static absl::string_view GetFirstChunk(const Cord& c);
1000
  static absl::string_view GetFirstChunk(absl::string_view sv);
1001
1002
  // Returns a new reference to contents_.tree(), or steals an existing
1003
  // reference if called on an rvalue.
1004
  absl::cord_internal::CordRep* TakeRep() const&;
1005
  absl::cord_internal::CordRep* TakeRep() &&;
1006
1007
  // Helper for Append().
1008
  template <typename C>
1009
  void AppendImpl(C&& src);
1010
1011
  // Appends / Prepends `src` to this instance, using precise sizing.
1012
  // This method does explicitly not attempt to use any spare capacity
1013
  // in any pending last added private owned flat.
1014
  // Requires `src` to be <= kMaxFlatLength.
1015
  void AppendPrecise(absl::string_view src, MethodIdentifier method);
1016
  void PrependPrecise(absl::string_view src, MethodIdentifier method);
1017
1018
  CordBuffer GetAppendBufferSlowPath(size_t block_size, size_t capacity,
1019
                                     size_t min_capacity);
1020
1021
  // Prepends the provided data to this instance. `method` contains the public
1022
  // API method for this action which is tracked for Cordz sampling purposes.
1023
  void PrependArray(absl::string_view src, MethodIdentifier method);
1024
1025
  // Assigns the value in 'src' to this instance, 'stealing' its contents.
1026
  // Requires src.length() > kMaxBytesToCopy.
1027
  Cord& AssignLargeString(std::string&& src);
1028
1029
  // Helper for AbslHashValue().
1030
  template <typename H>
1031
0
  H HashFragmented(H hash_state) const {
1032
0
    typename H::AbslInternalPiecewiseCombiner combiner;
1033
0
    ForEachChunk([&combiner, &hash_state](absl::string_view chunk) {
1034
0
      hash_state = combiner.add_buffer(std::move(hash_state), chunk.data(),
1035
0
                                       chunk.size());
1036
0
    });
1037
0
    return H::combine(combiner.finalize(std::move(hash_state)), size());
1038
0
  }
1039
1040
  friend class CrcCord;
1041
  void SetCrcCordState(crc_internal::CrcCordState state);
1042
  const crc_internal::CrcCordState* MaybeGetCrcCordState() const;
1043
1044
  CharIterator FindImpl(CharIterator it, absl::string_view needle) const;
1045
};
1046
1047
ABSL_NAMESPACE_END
1048
}  // namespace absl
1049
1050
namespace absl {
1051
ABSL_NAMESPACE_BEGIN
1052
1053
// allow a Cord to be logged
1054
extern std::ostream& operator<<(std::ostream& out, const Cord& cord);
1055
1056
// ------------------------------------------------------------------
1057
// Internal details follow.  Clients should ignore.
1058
1059
namespace cord_internal {
1060
1061
// Does non-template-specific `CordRepExternal` initialization.
1062
// Requires `data` to be non-empty.
1063
void InitializeCordRepExternal(absl::string_view data, CordRepExternal* rep);
1064
1065
// Creates a new `CordRep` that owns `data` and `releaser` and returns a pointer
1066
// to it. Requires `data` to be non-empty.
1067
template <typename Releaser>
1068
// NOLINTNEXTLINE - suppress clang-tidy raw pointer return.
1069
0
CordRep* NewExternalRep(absl::string_view data, Releaser&& releaser) {
1070
0
  assert(!data.empty());
1071
0
  using ReleaserType = absl::decay_t<Releaser>;
1072
0
  CordRepExternal* rep = new CordRepExternalImpl<ReleaserType>(
1073
0
      std::forward<Releaser>(releaser), 0);
1074
0
  InitializeCordRepExternal(data, rep);
1075
0
  return rep;
1076
0
}
1077
1078
// Overload for function reference types that dispatches using a function
1079
// pointer because there are no `alignof()` or `sizeof()` a function reference.
1080
// NOLINTNEXTLINE - suppress clang-tidy raw pointer return.
1081
inline CordRep* NewExternalRep(absl::string_view data,
1082
0
                               void (&releaser)(absl::string_view)) {
1083
0
  return NewExternalRep(data, &releaser);
1084
0
}
1085
1086
}  // namespace cord_internal
1087
1088
template <typename Releaser>
1089
Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser) {
1090
  Cord cord;
1091
  if (ABSL_PREDICT_TRUE(!data.empty())) {
1092
    cord.contents_.EmplaceTree(::absl::cord_internal::NewExternalRep(
1093
                                   data, std::forward<Releaser>(releaser)),
1094
                               Cord::MethodIdentifier::kMakeCordFromExternal);
1095
  } else {
1096
    using ReleaserType = absl::decay_t<Releaser>;
1097
    cord_internal::InvokeReleaser(
1098
        cord_internal::Rank0{}, ReleaserType(std::forward<Releaser>(releaser)),
1099
        data);
1100
  }
1101
  return cord;
1102
}
1103
1104
constexpr Cord::InlineRep::InlineRep(absl::string_view sv, CordRep* rep)
1105
    : data_(sv, rep) {}
1106
1107
inline Cord::InlineRep::InlineRep(const Cord::InlineRep& src)
1108
    : data_(InlineData::kDefaultInit) {
1109
  if (CordRep* tree = src.tree()) {
1110
    EmplaceTree(CordRep::Ref(tree), src.data_,
1111
                CordzUpdateTracker::kConstructorCord);
1112
  } else {
1113
    data_ = src.data_;
1114
  }
1115
}
1116
1117
inline Cord::InlineRep::InlineRep(Cord::InlineRep&& src) : data_(src.data_) {
1118
  src.ResetToEmpty();
1119
}
1120
1121
0
inline Cord::InlineRep& Cord::InlineRep::operator=(const Cord::InlineRep& src) {
1122
0
  if (this == &src) {
1123
0
    return *this;
1124
0
  }
1125
0
  if (!is_tree() && !src.is_tree()) {
1126
0
    data_ = src.data_;
1127
0
    return *this;
1128
0
  }
1129
0
  AssignSlow(src);
1130
0
  return *this;
1131
0
}
1132
1133
inline Cord::InlineRep& Cord::InlineRep::operator=(
1134
0
    Cord::InlineRep&& src) noexcept {
1135
0
  if (is_tree()) {
1136
0
    UnrefTree();
1137
0
  }
1138
0
  data_ = src.data_;
1139
0
  src.ResetToEmpty();
1140
0
  return *this;
1141
0
}
1142
1143
0
inline void Cord::InlineRep::Swap(Cord::InlineRep* rhs) {
1144
0
  if (rhs == this) {
1145
0
    return;
1146
0
  }
1147
0
  std::swap(data_, rhs->data_);
1148
0
}
1149
1150
0
inline const char* Cord::InlineRep::data() const {
1151
0
  return is_tree() ? nullptr : data_.as_chars();
1152
0
}
1153
1154
0
inline const char* Cord::InlineRep::as_chars() const {
1155
0
  assert(!data_.is_tree());
1156
0
  return data_.as_chars();
1157
0
}
1158
1159
0
inline absl::cord_internal::CordRep* Cord::InlineRep::as_tree() const {
1160
0
  assert(data_.is_tree());
1161
0
  return data_.as_tree();
1162
0
}
1163
1164
0
inline absl::cord_internal::CordRep* Cord::InlineRep::tree() const {
1165
0
  if (is_tree()) {
1166
0
    return as_tree();
1167
0
  } else {
1168
0
    return nullptr;
1169
0
  }
1170
0
}
1171
1172
0
inline size_t Cord::InlineRep::size() const {
1173
0
  return is_tree() ? as_tree()->length : inline_size();
1174
0
}
1175
1176
inline cord_internal::CordRepFlat* Cord::InlineRep::MakeFlatWithExtraCapacity(
1177
0
    size_t extra) {
1178
0
  static_assert(cord_internal::kMinFlatLength >= sizeof(data_), "");
1179
0
  size_t len = data_.inline_size();
1180
0
  auto* result = CordRepFlat::New(len + extra);
1181
0
  result->length = len;
1182
0
  data_.copy_max_inline_to(result->Data());
1183
0
  return result;
1184
0
}
1185
1186
inline void Cord::InlineRep::EmplaceTree(CordRep* rep,
1187
0
                                         MethodIdentifier method) {
1188
0
  assert(rep);
1189
0
  data_.make_tree(rep);
1190
0
  CordzInfo::MaybeTrackCord(data_, method);
1191
0
}
1192
1193
inline void Cord::InlineRep::EmplaceTree(CordRep* rep, const InlineData& parent,
1194
0
                                         MethodIdentifier method) {
1195
0
  data_.make_tree(rep);
1196
0
  CordzInfo::MaybeTrackCord(data_, parent, method);
1197
0
}
1198
1199
inline void Cord::InlineRep::SetTree(CordRep* rep,
1200
0
                                     const CordzUpdateScope& scope) {
1201
0
  assert(rep);
1202
0
  assert(data_.is_tree());
1203
0
  data_.set_tree(rep);
1204
0
  scope.SetCordRep(rep);
1205
0
}
1206
1207
inline void Cord::InlineRep::SetTreeOrEmpty(CordRep* rep,
1208
0
                                            const CordzUpdateScope& scope) {
1209
0
  assert(data_.is_tree());
1210
0
  if (rep) {
1211
0
    data_.set_tree(rep);
1212
0
  } else {
1213
0
    data_ = {};
1214
0
  }
1215
0
  scope.SetCordRep(rep);
1216
0
}
1217
1218
inline void Cord::InlineRep::CommitTree(const CordRep* old_rep, CordRep* rep,
1219
                                        const CordzUpdateScope& scope,
1220
0
                                        MethodIdentifier method) {
1221
0
  if (old_rep) {
1222
0
    SetTree(rep, scope);
1223
0
  } else {
1224
0
    EmplaceTree(rep, method);
1225
0
  }
1226
0
}
1227
1228
0
inline absl::cord_internal::CordRep* Cord::InlineRep::clear() {
1229
0
  if (is_tree()) {
1230
0
    CordzInfo::MaybeUntrackCord(cordz_info());
1231
0
  }
1232
0
  absl::cord_internal::CordRep* result = tree();
1233
0
  ResetToEmpty();
1234
0
  return result;
1235
0
}
1236
1237
0
inline void Cord::InlineRep::CopyToArray(char* dst) const {
1238
0
  assert(!is_tree());
1239
0
  size_t n = inline_size();
1240
0
  assert(n != 0);
1241
0
  cord_internal::SmallMemmove(dst, data_.as_chars(), n);
1242
0
}
1243
1244
0
inline void Cord::InlineRep::MaybeRemoveEmptyCrcNode() {
1245
0
  CordRep* rep = tree();
1246
0
  if (rep == nullptr || ABSL_PREDICT_TRUE(rep->length > 0)) {
1247
0
    return;
1248
0
  }
1249
0
  assert(rep->IsCrc());
1250
0
  assert(rep->crc()->child == nullptr);
1251
0
  CordzInfo::MaybeUntrackCord(cordz_info());
1252
0
  CordRep::Unref(rep);
1253
0
  ResetToEmpty();
1254
0
}
1255
1256
constexpr inline Cord::Cord() noexcept {}
1257
1258
inline Cord::Cord(absl::string_view src)
1259
    : Cord(src, CordzUpdateTracker::kConstructorString) {}
1260
1261
template <typename T>
1262
constexpr Cord::Cord(strings_internal::StringConstant<T>)
1263
    : contents_(strings_internal::StringConstant<T>::value,
1264
                strings_internal::StringConstant<T>::value.size() <=
1265
                        cord_internal::kMaxInline
1266
                    ? nullptr
1267
                    : &cord_internal::ConstInitExternalStorage<
1268
                          strings_internal::StringConstant<T>>::value) {}
1269
1270
0
inline Cord& Cord::operator=(const Cord& x) {
1271
0
  contents_ = x.contents_;
1272
0
  return *this;
1273
0
}
1274
1275
template <typename T, Cord::EnableIfString<T>>
1276
Cord& Cord::operator=(T&& src) {
1277
  if (src.size() <= cord_internal::kMaxBytesToCopy) {
1278
    return operator=(absl::string_view(src));
1279
  } else {
1280
    return AssignLargeString(std::forward<T>(src));
1281
  }
1282
}
1283
1284
inline Cord::Cord(const Cord& src) : contents_(src.contents_) {}
1285
1286
inline Cord::Cord(Cord&& src) noexcept : contents_(std::move(src.contents_)) {}
1287
1288
0
inline void Cord::swap(Cord& other) noexcept {
1289
0
  contents_.Swap(&other.contents_);
1290
0
}
1291
1292
0
inline Cord& Cord::operator=(Cord&& x) noexcept {
1293
0
  contents_ = std::move(x.contents_);
1294
0
  return *this;
1295
0
}
1296
1297
extern template Cord::Cord(std::string&& src);
1298
1299
0
inline size_t Cord::size() const {
1300
0
  // Length is 1st field in str.rep_
1301
0
  return contents_.size();
1302
0
}
1303
1304
0
inline bool Cord::empty() const { return size() == 0; }
1305
1306
inline size_t Cord::EstimatedMemoryUsage(
1307
0
    CordMemoryAccounting accounting_method) const {
1308
0
  size_t result = sizeof(Cord);
1309
0
  if (const absl::cord_internal::CordRep* rep = contents_.tree()) {
1310
0
    switch (accounting_method) {
1311
0
      case CordMemoryAccounting::kFairShare:
1312
0
        result += cord_internal::GetEstimatedFairShareMemoryUsage(rep);
1313
0
        break;
1314
0
      case CordMemoryAccounting::kTotalMorePrecise:
1315
0
        result += cord_internal::GetMorePreciseMemoryUsage(rep);
1316
0
        break;
1317
0
      case CordMemoryAccounting::kTotal:
1318
0
        result += cord_internal::GetEstimatedMemoryUsage(rep);
1319
0
        break;
1320
0
    }
1321
0
  }
1322
0
  return result;
1323
0
}
1324
1325
0
inline absl::optional<absl::string_view> Cord::TryFlat() const {
1326
0
  absl::cord_internal::CordRep* rep = contents_.tree();
1327
0
  if (rep == nullptr) {
1328
0
    return absl::string_view(contents_.data(), contents_.size());
1329
0
  }
1330
0
  absl::string_view fragment;
1331
0
  if (GetFlatAux(rep, &fragment)) {
1332
0
    return fragment;
1333
0
  }
1334
0
  return absl::nullopt;
1335
0
}
1336
1337
0
inline absl::string_view Cord::Flatten() {
1338
0
  absl::cord_internal::CordRep* rep = contents_.tree();
1339
0
  if (rep == nullptr) {
1340
0
    return absl::string_view(contents_.data(), contents_.size());
1341
0
  } else {
1342
0
    absl::string_view already_flat_contents;
1343
0
    if (GetFlatAux(rep, &already_flat_contents)) {
1344
0
      return already_flat_contents;
1345
0
    }
1346
0
  }
1347
0
  return FlattenSlowPath();
1348
0
}
1349
1350
0
inline void Cord::Append(absl::string_view src) {
1351
0
  contents_.AppendArray(src, CordzUpdateTracker::kAppendString);
1352
0
}
1353
1354
0
inline void Cord::Prepend(absl::string_view src) {
1355
0
  PrependArray(src, CordzUpdateTracker::kPrependString);
1356
0
}
1357
1358
0
inline void Cord::Append(CordBuffer buffer) {
1359
0
  if (ABSL_PREDICT_FALSE(buffer.length() == 0)) return;
1360
0
  absl::string_view short_value;
1361
0
  if (CordRep* rep = buffer.ConsumeValue(short_value)) {
1362
0
    contents_.AppendTree(rep, CordzUpdateTracker::kAppendCordBuffer);
1363
0
  } else {
1364
0
    AppendPrecise(short_value, CordzUpdateTracker::kAppendCordBuffer);
1365
0
  }
1366
0
}
1367
1368
0
inline void Cord::Prepend(CordBuffer buffer) {
1369
0
  if (ABSL_PREDICT_FALSE(buffer.length() == 0)) return;
1370
0
  absl::string_view short_value;
1371
0
  if (CordRep* rep = buffer.ConsumeValue(short_value)) {
1372
0
    contents_.PrependTree(rep, CordzUpdateTracker::kPrependCordBuffer);
1373
0
  } else {
1374
0
    PrependPrecise(short_value, CordzUpdateTracker::kPrependCordBuffer);
1375
0
  }
1376
0
}
1377
1378
0
inline CordBuffer Cord::GetAppendBuffer(size_t capacity, size_t min_capacity) {
1379
0
  if (empty()) return CordBuffer::CreateWithDefaultLimit(capacity);
1380
0
  return GetAppendBufferSlowPath(0, capacity, min_capacity);
1381
0
}
1382
1383
inline CordBuffer Cord::GetCustomAppendBuffer(size_t block_size,
1384
                                              size_t capacity,
1385
0
                                              size_t min_capacity) {
1386
0
  if (empty()) {
1387
0
    return block_size ? CordBuffer::CreateWithCustomLimit(block_size, capacity)
1388
0
                      : CordBuffer::CreateWithDefaultLimit(capacity);
1389
0
  }
1390
0
  return GetAppendBufferSlowPath(block_size, capacity, min_capacity);
1391
0
}
1392
1393
extern template void Cord::Append(std::string&& src);
1394
extern template void Cord::Prepend(std::string&& src);
1395
1396
0
inline int Cord::Compare(const Cord& rhs) const {
1397
0
  if (!contents_.is_tree() && !rhs.contents_.is_tree()) {
1398
0
    return contents_.data_.Compare(rhs.contents_.data_);
1399
0
  }
1400
0
1401
0
  return CompareImpl(rhs);
1402
0
}
1403
1404
// Does 'this' cord start/end with rhs
1405
0
inline bool Cord::StartsWith(const Cord& rhs) const {
1406
0
  if (contents_.IsSame(rhs.contents_)) return true;
1407
0
  size_t rhs_size = rhs.size();
1408
0
  if (size() < rhs_size) return false;
1409
0
  return EqualsImpl(rhs, rhs_size);
1410
0
}
1411
1412
0
inline bool Cord::StartsWith(absl::string_view rhs) const {
1413
0
  size_t rhs_size = rhs.size();
1414
0
  if (size() < rhs_size) return false;
1415
0
  return EqualsImpl(rhs, rhs_size);
1416
0
}
1417
1418
0
inline void Cord::ChunkIterator::InitTree(cord_internal::CordRep* tree) {
1419
0
  tree = cord_internal::SkipCrcNode(tree);
1420
0
  if (tree->tag == cord_internal::BTREE) {
1421
0
    current_chunk_ = btree_reader_.Init(tree->btree());
1422
0
  } else {
1423
0
    current_leaf_ = tree;
1424
0
    current_chunk_ = cord_internal::EdgeData(tree);
1425
0
  }
1426
0
}
1427
1428
inline Cord::ChunkIterator::ChunkIterator(cord_internal::CordRep* tree) {
1429
  bytes_remaining_ = tree->length;
1430
  InitTree(tree);
1431
}
1432
1433
inline Cord::ChunkIterator::ChunkIterator(const Cord* cord) {
1434
  if (CordRep* tree = cord->contents_.tree()) {
1435
    bytes_remaining_ = tree->length;
1436
    if (ABSL_PREDICT_TRUE(bytes_remaining_ != 0)) {
1437
      InitTree(tree);
1438
    } else {
1439
      current_chunk_ = {};
1440
    }
1441
  } else {
1442
    bytes_remaining_ = cord->contents_.inline_size();
1443
    current_chunk_ = {cord->contents_.data(), bytes_remaining_};
1444
  }
1445
}
1446
1447
0
inline Cord::ChunkIterator& Cord::ChunkIterator::AdvanceBtree() {
1448
0
  current_chunk_ = btree_reader_.Next();
1449
0
  return *this;
1450
0
}
1451
1452
0
inline void Cord::ChunkIterator::AdvanceBytesBtree(size_t n) {
1453
0
  assert(n >= current_chunk_.size());
1454
0
  bytes_remaining_ -= n;
1455
0
  if (bytes_remaining_) {
1456
0
    if (n == current_chunk_.size()) {
1457
0
      current_chunk_ = btree_reader_.Next();
1458
0
    } else {
1459
0
      size_t offset = btree_reader_.length() - bytes_remaining_;
1460
0
      current_chunk_ = btree_reader_.Seek(offset);
1461
0
    }
1462
0
  } else {
1463
0
    current_chunk_ = {};
1464
0
  }
1465
0
}
1466
1467
0
inline Cord::ChunkIterator& Cord::ChunkIterator::operator++() {
1468
0
  ABSL_HARDENING_ASSERT(bytes_remaining_ > 0 &&
1469
0
                        "Attempted to iterate past `end()`");
1470
0
  assert(bytes_remaining_ >= current_chunk_.size());
1471
0
  bytes_remaining_ -= current_chunk_.size();
1472
0
  if (bytes_remaining_ > 0) {
1473
0
    if (btree_reader_) {
1474
0
      return AdvanceBtree();
1475
0
    } else {
1476
0
      assert(!current_chunk_.empty());  // Called on invalid iterator.
1477
0
    }
1478
0
    current_chunk_ = {};
1479
0
  }
1480
0
  return *this;
1481
0
}
1482
1483
0
inline Cord::ChunkIterator Cord::ChunkIterator::operator++(int) {
1484
0
  ChunkIterator tmp(*this);
1485
0
  operator++();
1486
0
  return tmp;
1487
0
}
1488
1489
0
inline bool Cord::ChunkIterator::operator==(const ChunkIterator& other) const {
1490
0
  return bytes_remaining_ == other.bytes_remaining_;
1491
0
}
1492
1493
0
inline bool Cord::ChunkIterator::operator!=(const ChunkIterator& other) const {
1494
0
  return !(*this == other);
1495
0
}
1496
1497
0
inline Cord::ChunkIterator::reference Cord::ChunkIterator::operator*() const {
1498
0
  ABSL_HARDENING_ASSERT(bytes_remaining_ != 0);
1499
0
  return current_chunk_;
1500
0
}
1501
1502
0
inline Cord::ChunkIterator::pointer Cord::ChunkIterator::operator->() const {
1503
0
  ABSL_HARDENING_ASSERT(bytes_remaining_ != 0);
1504
0
  return &current_chunk_;
1505
0
}
1506
1507
0
inline void Cord::ChunkIterator::RemoveChunkPrefix(size_t n) {
1508
0
  assert(n < current_chunk_.size());
1509
0
  current_chunk_.remove_prefix(n);
1510
0
  bytes_remaining_ -= n;
1511
0
}
1512
1513
0
inline void Cord::ChunkIterator::AdvanceBytes(size_t n) {
1514
0
  assert(bytes_remaining_ >= n);
1515
0
  if (ABSL_PREDICT_TRUE(n < current_chunk_.size())) {
1516
0
    RemoveChunkPrefix(n);
1517
0
  } else if (n != 0) {
1518
0
    if (btree_reader_) {
1519
0
      AdvanceBytesBtree(n);
1520
0
    } else {
1521
0
      bytes_remaining_ = 0;
1522
0
    }
1523
0
  }
1524
0
}
1525
1526
0
inline Cord::ChunkIterator Cord::chunk_begin() const {
1527
0
  return ChunkIterator(this);
1528
0
}
1529
1530
0
inline Cord::ChunkIterator Cord::chunk_end() const { return ChunkIterator(); }
1531
1532
0
inline Cord::ChunkIterator Cord::ChunkRange::begin() const {
1533
0
  return cord_->chunk_begin();
1534
0
}
1535
1536
0
inline Cord::ChunkIterator Cord::ChunkRange::end() const {
1537
0
  return cord_->chunk_end();
1538
0
}
1539
1540
0
inline Cord::ChunkRange Cord::Chunks() const { return ChunkRange(this); }
1541
1542
0
inline Cord::CharIterator& Cord::CharIterator::operator++() {
1543
0
  if (ABSL_PREDICT_TRUE(chunk_iterator_->size() > 1)) {
1544
0
    chunk_iterator_.RemoveChunkPrefix(1);
1545
0
  } else {
1546
0
    ++chunk_iterator_;
1547
0
  }
1548
0
  return *this;
1549
0
}
1550
1551
0
inline Cord::CharIterator Cord::CharIterator::operator++(int) {
1552
0
  CharIterator tmp(*this);
1553
0
  operator++();
1554
0
  return tmp;
1555
0
}
1556
1557
0
inline bool Cord::CharIterator::operator==(const CharIterator& other) const {
1558
0
  return chunk_iterator_ == other.chunk_iterator_;
1559
0
}
1560
1561
0
inline bool Cord::CharIterator::operator!=(const CharIterator& other) const {
1562
0
  return !(*this == other);
1563
0
}
1564
1565
0
inline Cord::CharIterator::reference Cord::CharIterator::operator*() const {
1566
0
  return *chunk_iterator_->data();
1567
0
}
1568
1569
0
inline Cord::CharIterator::pointer Cord::CharIterator::operator->() const {
1570
0
  return chunk_iterator_->data();
1571
0
}
1572
1573
0
inline Cord Cord::AdvanceAndRead(CharIterator* it, size_t n_bytes) {
1574
0
  assert(it != nullptr);
1575
0
  return it->chunk_iterator_.AdvanceAndReadBytes(n_bytes);
1576
0
}
1577
1578
0
inline void Cord::Advance(CharIterator* it, size_t n_bytes) {
1579
0
  assert(it != nullptr);
1580
0
  it->chunk_iterator_.AdvanceBytes(n_bytes);
1581
0
}
1582
1583
0
inline absl::string_view Cord::ChunkRemaining(const CharIterator& it) {
1584
0
  return *it.chunk_iterator_;
1585
0
}
1586
1587
0
inline Cord::CharIterator Cord::char_begin() const {
1588
0
  return CharIterator(this);
1589
0
}
1590
1591
0
inline Cord::CharIterator Cord::char_end() const { return CharIterator(); }
1592
1593
0
inline Cord::CharIterator Cord::CharRange::begin() const {
1594
0
  return cord_->char_begin();
1595
0
}
1596
1597
0
inline Cord::CharIterator Cord::CharRange::end() const {
1598
0
  return cord_->char_end();
1599
0
}
1600
1601
0
inline Cord::CharRange Cord::Chars() const { return CharRange(this); }
1602
1603
inline void Cord::ForEachChunk(
1604
0
    absl::FunctionRef<void(absl::string_view)> callback) const {
1605
0
  absl::cord_internal::CordRep* rep = contents_.tree();
1606
0
  if (rep == nullptr) {
1607
0
    callback(absl::string_view(contents_.data(), contents_.size()));
1608
0
  } else {
1609
0
    ForEachChunkAux(rep, callback);
1610
0
  }
1611
0
}
1612
1613
// Nonmember Cord-to-Cord relational operators.
1614
0
inline bool operator==(const Cord& lhs, const Cord& rhs) {
1615
0
  if (lhs.contents_.IsSame(rhs.contents_)) return true;
1616
0
  size_t rhs_size = rhs.size();
1617
0
  if (lhs.size() != rhs_size) return false;
1618
0
  return lhs.EqualsImpl(rhs, rhs_size);
1619
0
}
1620
1621
0
inline bool operator!=(const Cord& x, const Cord& y) { return !(x == y); }
1622
0
inline bool operator<(const Cord& x, const Cord& y) { return x.Compare(y) < 0; }
1623
0
inline bool operator>(const Cord& x, const Cord& y) { return x.Compare(y) > 0; }
1624
0
inline bool operator<=(const Cord& x, const Cord& y) {
1625
0
  return x.Compare(y) <= 0;
1626
0
}
1627
0
inline bool operator>=(const Cord& x, const Cord& y) {
1628
0
  return x.Compare(y) >= 0;
1629
0
}
1630
1631
// Nonmember Cord-to-absl::string_view relational operators.
1632
//
1633
// Due to implicit conversions, these also enable comparisons of Cord with
1634
// std::string and const char*.
1635
0
inline bool operator==(const Cord& lhs, absl::string_view rhs) {
1636
0
  size_t lhs_size = lhs.size();
1637
0
  size_t rhs_size = rhs.size();
1638
0
  if (lhs_size != rhs_size) return false;
1639
0
  return lhs.EqualsImpl(rhs, rhs_size);
1640
0
}
1641
1642
0
inline bool operator==(absl::string_view x, const Cord& y) { return y == x; }
1643
0
inline bool operator!=(const Cord& x, absl::string_view y) { return !(x == y); }
1644
0
inline bool operator!=(absl::string_view x, const Cord& y) { return !(x == y); }
1645
0
inline bool operator<(const Cord& x, absl::string_view y) {
1646
0
  return x.Compare(y) < 0;
1647
0
}
1648
0
inline bool operator<(absl::string_view x, const Cord& y) {
1649
0
  return y.Compare(x) > 0;
1650
0
}
1651
0
inline bool operator>(const Cord& x, absl::string_view y) { return y < x; }
1652
0
inline bool operator>(absl::string_view x, const Cord& y) { return y < x; }
1653
0
inline bool operator<=(const Cord& x, absl::string_view y) { return !(y < x); }
1654
0
inline bool operator<=(absl::string_view x, const Cord& y) { return !(y < x); }
1655
0
inline bool operator>=(const Cord& x, absl::string_view y) { return !(x < y); }
1656
0
inline bool operator>=(absl::string_view x, const Cord& y) { return !(x < y); }
1657
1658
// Some internals exposed to test code.
1659
namespace strings_internal {
1660
class CordTestAccess {
1661
 public:
1662
  static size_t FlatOverhead();
1663
  static size_t MaxFlatLength();
1664
  static size_t SizeofCordRepExternal();
1665
  static size_t SizeofCordRepSubstring();
1666
  static size_t FlatTagToLength(uint8_t tag);
1667
  static uint8_t LengthToTag(size_t s);
1668
};
1669
}  // namespace strings_internal
1670
ABSL_NAMESPACE_END
1671
}  // namespace absl
1672
1673
#endif  // ABSL_STRINGS_CORD_H_