Coverage Report

Created: 2026-09-14 06:45

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