Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:15

0001 // Copyright 2020 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: cord.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // This file defines the `absl::Cord` data structure and operations on that data
0020 // structure. A Cord is a string-like sequence of characters optimized for
0021 // specific use cases. Unlike a `std::string`, which stores an array of
0022 // contiguous characters, Cord data is stored in a structure consisting of
0023 // separate, reference-counted "chunks."
0024 //
0025 // Because a Cord consists of these chunks, data can be added to or removed from
0026 // a Cord during its lifetime. Chunks may also be shared between Cords. Unlike a
0027 // `std::string`, a Cord can therefore accommodate data that changes over its
0028 // lifetime, though it's not quite "mutable"; it can change only in the
0029 // attachment, detachment, or rearrangement of chunks of its constituent data.
0030 //
0031 // A Cord provides some benefit over `std::string` under the following (albeit
0032 // narrow) circumstances:
0033 //
0034 //   * Cord data is designed to grow and shrink over a Cord's lifetime. Cord
0035 //     provides efficient insertions and deletions at the start and end of the
0036 //     character sequences, avoiding copies in those cases. Static data should
0037 //     generally be stored as strings.
0038 //   * External memory consisting of string-like data can be directly added to
0039 //     a Cord without requiring copies or allocations.
0040 //   * Cord data may be shared and copied cheaply. Cord provides a copy-on-write
0041 //     implementation and cheap sub-Cord operations. Copying a Cord is an O(1)
0042 //     operation.
0043 //
0044 // As a consequence to the above, Cord data is generally large. Small data
0045 // should generally use strings, as construction of a Cord requires some
0046 // overhead. Small Cords (<= 15 bytes) are represented inline, but most small
0047 // Cords are expected to grow over their lifetimes.
0048 //
0049 // Note that because a Cord is made up of separate chunked data, random access
0050 // to character data within a Cord is slower than within a `std::string`.
0051 //
0052 // Thread Safety
0053 //
0054 // Cord has the same thread-safety properties as many other types like
0055 // std::string, std::vector<>, int, etc -- it is thread-compatible. In
0056 // particular, if threads do not call non-const methods, then it is safe to call
0057 // const methods without synchronization. Copying a Cord produces a new instance
0058 // that can be used concurrently with the original in arbitrary ways.
0059 
0060 #ifndef ABSL_STRINGS_CORD_H_
0061 #define ABSL_STRINGS_CORD_H_
0062 
0063 #include <algorithm>
0064 #include <cstddef>
0065 #include <cstdint>
0066 #include <cstring>
0067 #include <iosfwd>
0068 #include <iterator>
0069 #include <string>
0070 #include <type_traits>
0071 
0072 #include "absl/base/attributes.h"
0073 #include "absl/base/config.h"
0074 #include "absl/base/internal/endian.h"
0075 #include "absl/base/internal/per_thread_tls.h"
0076 #include "absl/base/macros.h"
0077 #include "absl/base/nullability.h"
0078 #include "absl/base/port.h"
0079 #include "absl/container/inlined_vector.h"
0080 #include "absl/crc/internal/crc_cord_state.h"
0081 #include "absl/functional/function_ref.h"
0082 #include "absl/meta/type_traits.h"
0083 #include "absl/strings/cord_analysis.h"
0084 #include "absl/strings/cord_buffer.h"
0085 #include "absl/strings/internal/cord_data_edge.h"
0086 #include "absl/strings/internal/cord_internal.h"
0087 #include "absl/strings/internal/cord_rep_btree.h"
0088 #include "absl/strings/internal/cord_rep_btree_reader.h"
0089 #include "absl/strings/internal/cord_rep_crc.h"
0090 #include "absl/strings/internal/cordz_functions.h"
0091 #include "absl/strings/internal/cordz_info.h"
0092 #include "absl/strings/internal/cordz_statistics.h"
0093 #include "absl/strings/internal/cordz_update_scope.h"
0094 #include "absl/strings/internal/cordz_update_tracker.h"
0095 #include "absl/strings/internal/resize_uninitialized.h"
0096 #include "absl/strings/internal/string_constant.h"
0097 #include "absl/strings/string_view.h"
0098 #include "absl/types/optional.h"
0099 
0100 namespace absl {
0101 ABSL_NAMESPACE_BEGIN
0102 class Cord;
0103 class CordTestPeer;
0104 template <typename Releaser>
0105 Cord MakeCordFromExternal(absl::string_view, Releaser&&);
0106 void CopyCordToString(const Cord& src, absl::Nonnull<std::string*> dst);
0107 
0108 // Cord memory accounting modes
0109 enum class CordMemoryAccounting {
0110   // Counts the *approximate* number of bytes held in full or in part by this
0111   // Cord (which may not remain the same between invocations). Cords that share
0112   // memory could each be "charged" independently for the same shared memory.
0113   // See also comment on `kTotalMorePrecise` on internally shared memory.
0114   kTotal,
0115 
0116   // Counts the *approximate* number of bytes held in full or in part by this
0117   // Cord for the distinct memory held by this cord. This option is similar
0118   // to `kTotal`, except that if the cord has multiple references to the same
0119   // memory, that memory is only counted once.
0120   //
0121   // For example:
0122   //   absl::Cord cord;
0123   //   cord.Append(some_other_cord);
0124   //   cord.Append(some_other_cord);
0125   //   // Counts `some_other_cord` twice:
0126   //   cord.EstimatedMemoryUsage(kTotal);
0127   //   // Counts `some_other_cord` once:
0128   //   cord.EstimatedMemoryUsage(kTotalMorePrecise);
0129   //
0130   // The `kTotalMorePrecise` number is more expensive to compute as it requires
0131   // deduplicating all memory references. Applications should prefer to use
0132   // `kFairShare` or `kTotal` unless they really need a more precise estimate
0133   // on "how much memory is potentially held / kept alive by this cord?"
0134   kTotalMorePrecise,
0135 
0136   // Counts the *approximate* number of bytes held in full or in part by this
0137   // Cord weighted by the sharing ratio of that data. For example, if some data
0138   // edge is shared by 4 different Cords, then each cord is attributed 1/4th of
0139   // the total memory usage as a 'fair share' of the total memory usage.
0140   kFairShare,
0141 };
0142 
0143 // Cord
0144 //
0145 // A Cord is a sequence of characters, designed to be more efficient than a
0146 // `std::string` in certain circumstances: namely, large string data that needs
0147 // to change over its lifetime or shared, especially when such data is shared
0148 // across API boundaries.
0149 //
0150 // A Cord stores its character data in a structure that allows efficient prepend
0151 // and append operations. This makes a Cord useful for large string data sent
0152 // over in a wire format that may need to be prepended or appended at some point
0153 // during the data exchange (e.g. HTTP, protocol buffers). For example, a
0154 // Cord is useful for storing an HTTP request, and prepending an HTTP header to
0155 // such a request.
0156 //
0157 // Cords should not be used for storing general string data, however. They
0158 // require overhead to construct and are slower than strings for random access.
0159 //
0160 // The Cord API provides the following common API operations:
0161 //
0162 // * Create or assign Cords out of existing string data, memory, or other Cords
0163 // * Append and prepend data to an existing Cord
0164 // * Create new Sub-Cords from existing Cord data
0165 // * Swap Cord data and compare Cord equality
0166 // * Write out Cord data by constructing a `std::string`
0167 //
0168 // Additionally, the API provides iterator utilities to iterate through Cord
0169 // data via chunks or character bytes.
0170 //
0171 class Cord {
0172  private:
0173   template <typename T>
0174   using EnableIfString =
0175       absl::enable_if_t<std::is_same<T, std::string>::value, int>;
0176 
0177  public:
0178   // Cord::Cord() Constructors.
0179 
0180   // Creates an empty Cord.
0181   constexpr Cord() noexcept;
0182 
0183   // Creates a Cord from an existing Cord. Cord is copyable and efficiently
0184   // movable. The moved-from state is valid but unspecified.
0185   Cord(const Cord& src);
0186   Cord(Cord&& src) noexcept;
0187   Cord& operator=(const Cord& x);
0188   Cord& operator=(Cord&& x) noexcept;
0189 
0190   // Creates a Cord from a `src` string. This constructor is marked explicit to
0191   // prevent implicit Cord constructions from arguments convertible to an
0192   // `absl::string_view`.
0193   explicit Cord(absl::string_view src);
0194   Cord& operator=(absl::string_view src);
0195 
0196   // Creates a Cord from a `std::string&&` rvalue. These constructors are
0197   // templated to avoid ambiguities for types that are convertible to both
0198   // `absl::string_view` and `std::string`, such as `const char*`.
0199   template <typename T, EnableIfString<T> = 0>
0200   explicit Cord(T&& src);
0201   template <typename T, EnableIfString<T> = 0>
0202   Cord& operator=(T&& src);
0203 
0204   // Cord::~Cord()
0205   //
0206   // Destructs the Cord.
0207   ~Cord() {
0208     if (contents_.is_tree()) DestroyCordSlow();
0209   }
0210 
0211   // MakeCordFromExternal()
0212   //
0213   // Creates a Cord that takes ownership of external string memory. The
0214   // contents of `data` are not copied to the Cord; instead, the external
0215   // memory is added to the Cord and reference-counted. This data may not be
0216   // changed for the life of the Cord, though it may be prepended or appended
0217   // to.
0218   //
0219   // `MakeCordFromExternal()` takes a callable "releaser" that is invoked when
0220   // the reference count for `data` reaches zero. As noted above, this data must
0221   // remain live until the releaser is invoked. The callable releaser also must:
0222   //
0223   //   * be move constructible
0224   //   * support `void operator()(absl::string_view) const` or `void operator()`
0225   //
0226   // Example:
0227   //
0228   // Cord MakeCord(BlockPool* pool) {
0229   //   Block* block = pool->NewBlock();
0230   //   FillBlock(block);
0231   //   return absl::MakeCordFromExternal(
0232   //       block->ToStringView(),
0233   //       [pool, block](absl::string_view v) {
0234   //         pool->FreeBlock(block, v);
0235   //       });
0236   // }
0237   //
0238   // WARNING: Because a Cord can be reference-counted, it's likely a bug if your
0239   // releaser doesn't do anything. For example, consider the following:
0240   //
0241   // void Foo(const char* buffer, int len) {
0242   //   auto c = absl::MakeCordFromExternal(absl::string_view(buffer, len),
0243   //                                       [](absl::string_view) {});
0244   //
0245   //   // BUG: If Bar() copies its cord for any reason, including keeping a
0246   //   // substring of it, the lifetime of buffer might be extended beyond
0247   //   // when Foo() returns.
0248   //   Bar(c);
0249   // }
0250   template <typename Releaser>
0251   friend Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser);
0252 
0253   // Cord::Clear()
0254   //
0255   // Releases the Cord data. Any nodes that share data with other Cords, if
0256   // applicable, will have their reference counts reduced by 1.
0257   ABSL_ATTRIBUTE_REINITIALIZES void Clear();
0258 
0259   // Cord::Append()
0260   //
0261   // Appends data to the Cord, which may come from another Cord or other string
0262   // data.
0263   void Append(const Cord& src);
0264   void Append(Cord&& src);
0265   void Append(absl::string_view src);
0266   template <typename T, EnableIfString<T> = 0>
0267   void Append(T&& src);
0268 
0269   // Appends `buffer` to this cord, unless `buffer` has a zero length in which
0270   // case this method has no effect on this cord instance.
0271   // This method is guaranteed to consume `buffer`.
0272   void Append(CordBuffer buffer);
0273 
0274   // Returns a CordBuffer, re-using potential existing capacity in this cord.
0275   //
0276   // Cord instances may have additional unused capacity in the last (or first)
0277   // nodes of the underlying tree to facilitate amortized growth. This method
0278   // allows applications to explicitly use this spare capacity if available,
0279   // or create a new CordBuffer instance otherwise.
0280   // If this cord has a final non-shared node with at least `min_capacity`
0281   // available, then this method will return that buffer including its data
0282   // contents. I.e.; the returned buffer will have a non-zero length, and
0283   // a capacity of at least `buffer.length + min_capacity`. Otherwise, this
0284   // method will return `CordBuffer::CreateWithDefaultLimit(capacity)`.
0285   //
0286   // Below an example of using GetAppendBuffer. Notice that in this example we
0287   // use `GetAppendBuffer()` only on the first iteration. As we know nothing
0288   // about any initial extra capacity in `cord`, we may be able to use the extra
0289   // capacity. But as we add new buffers with fully utilized contents after that
0290   // we avoid calling `GetAppendBuffer()` on subsequent iterations: while this
0291   // works fine, it results in an unnecessary inspection of cord contents:
0292   //
0293   //   void AppendRandomDataToCord(absl::Cord &cord, size_t n) {
0294   //     bool first = true;
0295   //     while (n > 0) {
0296   //       CordBuffer buffer = first ? cord.GetAppendBuffer(n)
0297   //                                 : CordBuffer::CreateWithDefaultLimit(n);
0298   //       absl::Span<char> data = buffer.available_up_to(n);
0299   //       FillRandomValues(data.data(), data.size());
0300   //       buffer.IncreaseLengthBy(data.size());
0301   //       cord.Append(std::move(buffer));
0302   //       n -= data.size();
0303   //       first = false;
0304   //     }
0305   //   }
0306   CordBuffer GetAppendBuffer(size_t capacity, size_t min_capacity = 16);
0307 
0308   // Returns a CordBuffer, re-using potential existing capacity in this cord.
0309   //
0310   // This function is identical to `GetAppendBuffer`, except that in the case
0311   // where a new `CordBuffer` is allocated, it is allocated using the provided
0312   // custom limit instead of the default limit. `GetAppendBuffer` will default
0313   // to `CordBuffer::CreateWithDefaultLimit(capacity)` whereas this method
0314   // will default to `CordBuffer::CreateWithCustomLimit(block_size, capacity)`.
0315   // This method is equivalent to `GetAppendBuffer` if `block_size` is zero.
0316   // See the documentation for `CreateWithCustomLimit` for more details on the
0317   // restrictions and legal values for `block_size`.
0318   CordBuffer GetCustomAppendBuffer(size_t block_size, size_t capacity,
0319                                    size_t min_capacity = 16);
0320 
0321   // Cord::Prepend()
0322   //
0323   // Prepends data to the Cord, which may come from another Cord or other string
0324   // data.
0325   void Prepend(const Cord& src);
0326   void Prepend(absl::string_view src);
0327   template <typename T, EnableIfString<T> = 0>
0328   void Prepend(T&& src);
0329 
0330   // Prepends `buffer` to this cord, unless `buffer` has a zero length in which
0331   // case this method has no effect on this cord instance.
0332   // This method is guaranteed to consume `buffer`.
0333   void Prepend(CordBuffer buffer);
0334 
0335   // Cord::RemovePrefix()
0336   //
0337   // Removes the first `n` bytes of a Cord.
0338   void RemovePrefix(size_t n);
0339   void RemoveSuffix(size_t n);
0340 
0341   // Cord::Subcord()
0342   //
0343   // Returns a new Cord representing the subrange [pos, pos + new_size) of
0344   // *this. If pos >= size(), the result is empty(). If
0345   // (pos + new_size) >= size(), the result is the subrange [pos, size()).
0346   Cord Subcord(size_t pos, size_t new_size) const;
0347 
0348   // Cord::swap()
0349   //
0350   // Swaps the contents of the Cord with `other`.
0351   void swap(Cord& other) noexcept;
0352 
0353   // swap()
0354   //
0355   // Swaps the contents of two Cords.
0356   friend void swap(Cord& x, Cord& y) noexcept { x.swap(y); }
0357 
0358   // Cord::size()
0359   //
0360   // Returns the size of the Cord.
0361   size_t size() const;
0362 
0363   // Cord::empty()
0364   //
0365   // Determines whether the given Cord is empty, returning `true` if so.
0366   bool empty() const;
0367 
0368   // Cord::EstimatedMemoryUsage()
0369   //
0370   // Returns the *approximate* number of bytes held by this cord.
0371   // See CordMemoryAccounting for more information on the accounting method.
0372   size_t EstimatedMemoryUsage(CordMemoryAccounting accounting_method =
0373                                   CordMemoryAccounting::kTotal) const;
0374 
0375   // Cord::Compare()
0376   //
0377   // Compares 'this' Cord with rhs. This function and its relatives treat Cords
0378   // as sequences of unsigned bytes. The comparison is a straightforward
0379   // lexicographic comparison. `Cord::Compare()` returns values as follows:
0380   //
0381   //   -1  'this' Cord is smaller
0382   //    0  two Cords are equal
0383   //    1  'this' Cord is larger
0384   int Compare(absl::string_view rhs) const;
0385   int Compare(const Cord& rhs) const;
0386 
0387   // Cord::StartsWith()
0388   //
0389   // Determines whether the Cord starts with the passed string data `rhs`.
0390   bool StartsWith(const Cord& rhs) const;
0391   bool StartsWith(absl::string_view rhs) const;
0392 
0393   // Cord::EndsWith()
0394   //
0395   // Determines whether the Cord ends with the passed string data `rhs`.
0396   bool EndsWith(absl::string_view rhs) const;
0397   bool EndsWith(const Cord& rhs) const;
0398 
0399   // Cord::Contains()
0400   //
0401   // Determines whether the Cord contains the passed string data `rhs`.
0402   bool Contains(absl::string_view rhs) const;
0403   bool Contains(const Cord& rhs) const;
0404 
0405   // Cord::operator std::string()
0406   //
0407   // Converts a Cord into a `std::string()`. This operator is marked explicit to
0408   // prevent unintended Cord usage in functions that take a string.
0409   explicit operator std::string() const;
0410 
0411   // CopyCordToString()
0412   //
0413   // Copies the contents of a `src` Cord into a `*dst` string.
0414   //
0415   // This function optimizes the case of reusing the destination string since it
0416   // can reuse previously allocated capacity. However, this function does not
0417   // guarantee that pointers previously returned by `dst->data()` remain valid
0418   // even if `*dst` had enough capacity to hold `src`. If `*dst` is a new
0419   // object, prefer to simply use the conversion operator to `std::string`.
0420   friend void CopyCordToString(const Cord& src,
0421                                absl::Nonnull<std::string*> dst);
0422 
0423   class CharIterator;
0424 
0425   //----------------------------------------------------------------------------
0426   // Cord::ChunkIterator
0427   //----------------------------------------------------------------------------
0428   //
0429   // A `Cord::ChunkIterator` allows iteration over the constituent chunks of its
0430   // Cord. Such iteration allows you to perform non-const operations on the data
0431   // of a Cord without modifying it.
0432   //
0433   // Generally, you do not instantiate a `Cord::ChunkIterator` directly;
0434   // instead, you create one implicitly through use of the `Cord::Chunks()`
0435   // member function.
0436   //
0437   // The `Cord::ChunkIterator` has the following properties:
0438   //
0439   //   * The iterator is invalidated after any non-const operation on the
0440   //     Cord object over which it iterates.
0441   //   * The `string_view` returned by dereferencing a valid, non-`end()`
0442   //     iterator is guaranteed to be non-empty.
0443   //   * Two `ChunkIterator` objects can be compared equal if and only if they
0444   //     remain valid and iterate over the same Cord.
0445   //   * The iterator in this case is a proxy iterator; the `string_view`
0446   //     returned by the iterator does not live inside the Cord, and its
0447   //     lifetime is limited to the lifetime of the iterator itself. To help
0448   //     prevent lifetime issues, `ChunkIterator::reference` is not a true
0449   //     reference type and is equivalent to `value_type`.
0450   //   * The iterator keeps state that can grow for Cords that contain many
0451   //     nodes and are imbalanced due to sharing. Prefer to pass this type by
0452   //     const reference instead of by value.
0453   class ChunkIterator {
0454    public:
0455     using iterator_category = std::input_iterator_tag;
0456     using value_type = absl::string_view;
0457     using difference_type = ptrdiff_t;
0458     using pointer = absl::Nonnull<const value_type*>;
0459     using reference = value_type;
0460 
0461     ChunkIterator() = default;
0462 
0463     ChunkIterator& operator++();
0464     ChunkIterator operator++(int);
0465     bool operator==(const ChunkIterator& other) const;
0466     bool operator!=(const ChunkIterator& other) const;
0467     reference operator*() const;
0468     pointer operator->() const;
0469 
0470     friend class Cord;
0471     friend class CharIterator;
0472 
0473    private:
0474     using CordRep = absl::cord_internal::CordRep;
0475     using CordRepBtree = absl::cord_internal::CordRepBtree;
0476     using CordRepBtreeReader = absl::cord_internal::CordRepBtreeReader;
0477 
0478     // Constructs a `begin()` iterator from `tree`.
0479     explicit ChunkIterator(absl::Nonnull<cord_internal::CordRep*> tree);
0480 
0481     // Constructs a `begin()` iterator from `cord`.
0482     explicit ChunkIterator(absl::Nonnull<const Cord*> cord);
0483 
0484     // Initializes this instance from a tree. Invoked by constructors.
0485     void InitTree(absl::Nonnull<cord_internal::CordRep*> tree);
0486 
0487     // Removes `n` bytes from `current_chunk_`. Expects `n` to be smaller than
0488     // `current_chunk_.size()`.
0489     void RemoveChunkPrefix(size_t n);
0490     Cord AdvanceAndReadBytes(size_t n);
0491     void AdvanceBytes(size_t n);
0492 
0493     // Btree specific operator++
0494     ChunkIterator& AdvanceBtree();
0495     void AdvanceBytesBtree(size_t n);
0496 
0497     // A view into bytes of the current `CordRep`. It may only be a view to a
0498     // suffix of bytes if this is being used by `CharIterator`.
0499     absl::string_view current_chunk_;
0500     // The current leaf, or `nullptr` if the iterator points to short data.
0501     // If the current chunk is a substring node, current_leaf_ points to the
0502     // underlying flat or external node.
0503     absl::Nullable<absl::cord_internal::CordRep*> current_leaf_ = nullptr;
0504     // The number of bytes left in the `Cord` over which we are iterating.
0505     size_t bytes_remaining_ = 0;
0506 
0507     // Cord reader for cord btrees. Empty if not traversing a btree.
0508     CordRepBtreeReader btree_reader_;
0509   };
0510 
0511   // Cord::chunk_begin()
0512   //
0513   // Returns an iterator to the first chunk of the `Cord`.
0514   //
0515   // Generally, prefer using `Cord::Chunks()` within a range-based for loop for
0516   // iterating over the chunks of a Cord. This method may be useful for getting
0517   // a `ChunkIterator` where range-based for-loops are not useful.
0518   //
0519   // Example:
0520   //
0521   //   absl::Cord::ChunkIterator FindAsChunk(const absl::Cord& c,
0522   //                                         absl::string_view s) {
0523   //     return std::find(c.chunk_begin(), c.chunk_end(), s);
0524   //   }
0525   ChunkIterator chunk_begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
0526 
0527   // Cord::chunk_end()
0528   //
0529   // Returns an iterator one increment past the last chunk of the `Cord`.
0530   //
0531   // Generally, prefer using `Cord::Chunks()` within a range-based for loop for
0532   // iterating over the chunks of a Cord. This method may be useful for getting
0533   // a `ChunkIterator` where range-based for-loops may not be available.
0534   ChunkIterator chunk_end() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
0535 
0536   //----------------------------------------------------------------------------
0537   // Cord::ChunkRange
0538   //----------------------------------------------------------------------------
0539   //
0540   // `ChunkRange` is a helper class for iterating over the chunks of the `Cord`,
0541   // producing an iterator which can be used within a range-based for loop.
0542   // Construction of a `ChunkRange` will return an iterator pointing to the
0543   // first chunk of the Cord. Generally, do not construct a `ChunkRange`
0544   // directly; instead, prefer to use the `Cord::Chunks()` method.
0545   //
0546   // Implementation note: `ChunkRange` is simply a convenience wrapper over
0547   // `Cord::chunk_begin()` and `Cord::chunk_end()`.
0548   class ChunkRange {
0549    public:
0550     // Fulfill minimum c++ container requirements [container.requirements]
0551     // These (partial) container type definitions allow ChunkRange to be used
0552     // in various utilities expecting a subset of [container.requirements].
0553     // For example, the below enables using `::testing::ElementsAre(...)`
0554     using value_type = absl::string_view;
0555     using reference = value_type&;
0556     using const_reference = const value_type&;
0557     using iterator = ChunkIterator;
0558     using const_iterator = ChunkIterator;
0559 
0560     explicit ChunkRange(absl::Nonnull<const Cord*> cord) : cord_(cord) {}
0561 
0562     ChunkIterator begin() const;
0563     ChunkIterator end() const;
0564 
0565    private:
0566     absl::Nonnull<const Cord*> cord_;
0567   };
0568 
0569   // Cord::Chunks()
0570   //
0571   // Returns a `Cord::ChunkRange` for iterating over the chunks of a `Cord` with
0572   // a range-based for-loop. For most iteration tasks on a Cord, use
0573   // `Cord::Chunks()` to retrieve this iterator.
0574   //
0575   // Example:
0576   //
0577   //   void ProcessChunks(const Cord& cord) {
0578   //     for (absl::string_view chunk : cord.Chunks()) { ... }
0579   //   }
0580   //
0581   // Note that the ordinary caveats of temporary lifetime extension apply:
0582   //
0583   //   void Process() {
0584   //     for (absl::string_view chunk : CordFactory().Chunks()) {
0585   //       // The temporary Cord returned by CordFactory has been destroyed!
0586   //     }
0587   //   }
0588   ChunkRange Chunks() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
0589 
0590   //----------------------------------------------------------------------------
0591   // Cord::CharIterator
0592   //----------------------------------------------------------------------------
0593   //
0594   // A `Cord::CharIterator` allows iteration over the constituent characters of
0595   // a `Cord`.
0596   //
0597   // Generally, you do not instantiate a `Cord::CharIterator` directly; instead,
0598   // you create one implicitly through use of the `Cord::Chars()` member
0599   // function.
0600   //
0601   // A `Cord::CharIterator` has the following properties:
0602   //
0603   //   * The iterator is invalidated after any non-const operation on the
0604   //     Cord object over which it iterates.
0605   //   * Two `CharIterator` objects can be compared equal if and only if they
0606   //     remain valid and iterate over the same Cord.
0607   //   * The iterator keeps state that can grow for Cords that contain many
0608   //     nodes and are imbalanced due to sharing. Prefer to pass this type by
0609   //     const reference instead of by value.
0610   //   * This type cannot act as a forward iterator because a `Cord` can reuse
0611   //     sections of memory. This fact violates the requirement for forward
0612   //     iterators to compare equal if dereferencing them returns the same
0613   //     object.
0614   class CharIterator {
0615    public:
0616     using iterator_category = std::input_iterator_tag;
0617     using value_type = char;
0618     using difference_type = ptrdiff_t;
0619     using pointer = absl::Nonnull<const char*>;
0620     using reference = const char&;
0621 
0622     CharIterator() = default;
0623 
0624     CharIterator& operator++();
0625     CharIterator operator++(int);
0626     bool operator==(const CharIterator& other) const;
0627     bool operator!=(const CharIterator& other) const;
0628     reference operator*() const;
0629     pointer operator->() const;
0630 
0631     friend Cord;
0632 
0633    private:
0634     explicit CharIterator(absl::Nonnull<const Cord*> cord)
0635         : chunk_iterator_(cord) {}
0636 
0637     ChunkIterator chunk_iterator_;
0638   };
0639 
0640   // Cord::AdvanceAndRead()
0641   //
0642   // Advances the `Cord::CharIterator` by `n_bytes` and returns the bytes
0643   // advanced as a separate `Cord`. `n_bytes` must be less than or equal to the
0644   // number of bytes within the Cord; otherwise, behavior is undefined. It is
0645   // valid to pass `char_end()` and `0`.
0646   static Cord AdvanceAndRead(absl::Nonnull<CharIterator*> it, size_t n_bytes);
0647 
0648   // Cord::Advance()
0649   //
0650   // Advances the `Cord::CharIterator` by `n_bytes`. `n_bytes` must be less than
0651   // or equal to the number of bytes remaining within the Cord; otherwise,
0652   // behavior is undefined. It is valid to pass `char_end()` and `0`.
0653   static void Advance(absl::Nonnull<CharIterator*> it, size_t n_bytes);
0654 
0655   // Cord::ChunkRemaining()
0656   //
0657   // Returns the longest contiguous view starting at the iterator's position.
0658   //
0659   // `it` must be dereferenceable.
0660   static absl::string_view ChunkRemaining(const CharIterator& it);
0661 
0662   // Cord::char_begin()
0663   //
0664   // Returns an iterator to the first character of the `Cord`.
0665   //
0666   // Generally, prefer using `Cord::Chars()` within a range-based for loop for
0667   // iterating over the chunks of a Cord. This method may be useful for getting
0668   // a `CharIterator` where range-based for-loops may not be available.
0669   CharIterator char_begin() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
0670 
0671   // Cord::char_end()
0672   //
0673   // Returns an iterator to one past the last character of the `Cord`.
0674   //
0675   // Generally, prefer using `Cord::Chars()` within a range-based for loop for
0676   // iterating over the chunks of a Cord. This method may be useful for getting
0677   // a `CharIterator` where range-based for-loops are not useful.
0678   CharIterator char_end() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
0679 
0680   // Cord::CharRange
0681   //
0682   // `CharRange` is a helper class for iterating over the characters of a
0683   // producing an iterator which can be used within a range-based for loop.
0684   // Construction of a `CharRange` will return an iterator pointing to the first
0685   // character of the Cord. Generally, do not construct a `CharRange` directly;
0686   // instead, prefer to use the `Cord::Chars()` method shown below.
0687   //
0688   // Implementation note: `CharRange` is simply a convenience wrapper over
0689   // `Cord::char_begin()` and `Cord::char_end()`.
0690   class CharRange {
0691    public:
0692     // Fulfill minimum c++ container requirements [container.requirements]
0693     // These (partial) container type definitions allow CharRange to be used
0694     // in various utilities expecting a subset of [container.requirements].
0695     // For example, the below enables using `::testing::ElementsAre(...)`
0696     using value_type = char;
0697     using reference = value_type&;
0698     using const_reference = const value_type&;
0699     using iterator = CharIterator;
0700     using const_iterator = CharIterator;
0701 
0702     explicit CharRange(absl::Nonnull<const Cord*> cord) : cord_(cord) {}
0703 
0704     CharIterator begin() const;
0705     CharIterator end() const;
0706 
0707    private:
0708     absl::Nonnull<const Cord*> cord_;
0709   };
0710 
0711   // Cord::Chars()
0712   //
0713   // Returns a `Cord::CharRange` for iterating over the characters of a `Cord`
0714   // with a range-based for-loop. For most character-based iteration tasks on a
0715   // Cord, use `Cord::Chars()` to retrieve this iterator.
0716   //
0717   // Example:
0718   //
0719   //   void ProcessCord(const Cord& cord) {
0720   //     for (char c : cord.Chars()) { ... }
0721   //   }
0722   //
0723   // Note that the ordinary caveats of temporary lifetime extension apply:
0724   //
0725   //   void Process() {
0726   //     for (char c : CordFactory().Chars()) {
0727   //       // The temporary Cord returned by CordFactory has been destroyed!
0728   //     }
0729   //   }
0730   CharRange Chars() const ABSL_ATTRIBUTE_LIFETIME_BOUND;
0731 
0732   // Cord::operator[]
0733   //
0734   // Gets the "i"th character of the Cord and returns it, provided that
0735   // 0 <= i < Cord.size().
0736   //
0737   // NOTE: This routine is reasonably efficient. It is roughly
0738   // logarithmic based on the number of chunks that make up the cord. Still,
0739   // if you need to iterate over the contents of a cord, you should
0740   // use a CharIterator/ChunkIterator rather than call operator[] or Get()
0741   // repeatedly in a loop.
0742   char operator[](size_t i) const;
0743 
0744   // Cord::TryFlat()
0745   //
0746   // If this cord's representation is a single flat array, returns a
0747   // string_view referencing that array.  Otherwise returns nullopt.
0748   absl::optional<absl::string_view> TryFlat() const
0749       ABSL_ATTRIBUTE_LIFETIME_BOUND;
0750 
0751   // Cord::Flatten()
0752   //
0753   // Flattens the cord into a single array and returns a view of the data.
0754   //
0755   // If the cord was already flat, the contents are not modified.
0756   absl::string_view Flatten() ABSL_ATTRIBUTE_LIFETIME_BOUND;
0757 
0758   // Cord::Find()
0759   //
0760   // Returns an iterator to the first occurrance of the substring `needle`.
0761   //
0762   // If the substring `needle` does not occur, `Cord::char_end()` is returned.
0763   CharIterator Find(absl::string_view needle) const;
0764   CharIterator Find(const absl::Cord& needle) const;
0765 
0766   // Supports absl::Cord as a sink object for absl::Format().
0767   friend void AbslFormatFlush(absl::Nonnull<absl::Cord*> cord,
0768                               absl::string_view part) {
0769     cord->Append(part);
0770   }
0771 
0772   // Support automatic stringification with absl::StrCat and absl::StrFormat.
0773   template <typename Sink>
0774   friend void AbslStringify(Sink& sink, const absl::Cord& cord) {
0775     for (absl::string_view chunk : cord.Chunks()) {
0776       sink.Append(chunk);
0777     }
0778   }
0779 
0780   // Cord::SetExpectedChecksum()
0781   //
0782   // Stores a checksum value with this non-empty cord instance, for later
0783   // retrieval.
0784   //
0785   // The expected checksum is a number stored out-of-band, alongside the data.
0786   // It is preserved across copies and assignments, but any mutations to a cord
0787   // will cause it to lose its expected checksum.
0788   //
0789   // The expected checksum is not part of a Cord's value, and does not affect
0790   // operations such as equality or hashing.
0791   //
0792   // This field is intended to store a CRC32C checksum for later validation, to
0793   // help support end-to-end checksum workflows.  However, the Cord API itself
0794   // does no CRC validation, and assigns no meaning to this number.
0795   //
0796   // This call has no effect if this cord is empty.
0797   void SetExpectedChecksum(uint32_t crc);
0798 
0799   // Returns this cord's expected checksum, if it has one.  Otherwise, returns
0800   // nullopt.
0801   absl::optional<uint32_t> ExpectedChecksum() const;
0802 
0803   template <typename H>
0804   friend H AbslHashValue(H hash_state, const absl::Cord& c) {
0805     absl::optional<absl::string_view> maybe_flat = c.TryFlat();
0806     if (maybe_flat.has_value()) {
0807       return H::combine(std::move(hash_state), *maybe_flat);
0808     }
0809     return c.HashFragmented(std::move(hash_state));
0810   }
0811 
0812   // Create a Cord with the contents of StringConstant<T>::value.
0813   // No allocations will be done and no data will be copied.
0814   // This is an INTERNAL API and subject to change or removal. This API can only
0815   // be used by spelling absl::strings_internal::MakeStringConstant, which is
0816   // also an internal API.
0817   template <typename T>
0818   // NOLINTNEXTLINE(google-explicit-constructor)
0819   constexpr Cord(strings_internal::StringConstant<T>);
0820 
0821  private:
0822   using CordRep = absl::cord_internal::CordRep;
0823   using CordRepFlat = absl::cord_internal::CordRepFlat;
0824   using CordzInfo = cord_internal::CordzInfo;
0825   using CordzUpdateScope = cord_internal::CordzUpdateScope;
0826   using CordzUpdateTracker = cord_internal::CordzUpdateTracker;
0827   using InlineData = cord_internal::InlineData;
0828   using MethodIdentifier = CordzUpdateTracker::MethodIdentifier;
0829 
0830   // Creates a cord instance with `method` representing the originating
0831   // public API call causing the cord to be created.
0832   explicit Cord(absl::string_view src, MethodIdentifier method);
0833 
0834   friend class CordTestPeer;
0835   friend bool operator==(const Cord& lhs, const Cord& rhs);
0836   friend bool operator==(const Cord& lhs, absl::string_view rhs);
0837 
0838   friend absl::Nullable<const CordzInfo*> GetCordzInfoForTesting(
0839       const Cord& cord);
0840 
0841   // Calls the provided function once for each cord chunk, in order.  Unlike
0842   // Chunks(), this API will not allocate memory.
0843   void ForEachChunk(absl::FunctionRef<void(absl::string_view)>) const;
0844 
0845   // Allocates new contiguous storage for the contents of the cord. This is
0846   // called by Flatten() when the cord was not already flat.
0847   absl::string_view FlattenSlowPath();
0848 
0849   // Actual cord contents are hidden inside the following simple
0850   // class so that we can isolate the bulk of cord.cc from changes
0851   // to the representation.
0852   //
0853   // InlineRep holds either a tree pointer, or an array of kMaxInline bytes.
0854   class InlineRep {
0855    public:
0856     static constexpr unsigned char kMaxInline = cord_internal::kMaxInline;
0857     static_assert(kMaxInline >= sizeof(absl::cord_internal::CordRep*), "");
0858 
0859     constexpr InlineRep() : data_() {}
0860     explicit InlineRep(InlineData::DefaultInitType init) : data_(init) {}
0861     InlineRep(const InlineRep& src);
0862     InlineRep(InlineRep&& src);
0863     InlineRep& operator=(const InlineRep& src);
0864     InlineRep& operator=(InlineRep&& src) noexcept;
0865 
0866     explicit constexpr InlineRep(absl::string_view sv,
0867                                  absl::Nullable<CordRep*> rep);
0868 
0869     void Swap(absl::Nonnull<InlineRep*> rhs);
0870     size_t size() const;
0871     // Returns nullptr if holding pointer
0872     absl::Nullable<const char*> data() const;
0873     // Discards pointer, if any
0874     void set_data(absl::Nonnull<const char*> data, size_t n);
0875     absl::Nonnull<char*> set_data(size_t n);  // Write data to the result
0876     // Returns nullptr if holding bytes
0877     absl::Nullable<absl::cord_internal::CordRep*> tree() const;
0878     absl::Nonnull<absl::cord_internal::CordRep*> as_tree() const;
0879     absl::Nonnull<const char*> as_chars() const;
0880     // Returns non-null iff was holding a pointer
0881     absl::Nullable<absl::cord_internal::CordRep*> clear();
0882     // Converts to pointer if necessary.
0883     void reduce_size(size_t n);    // REQUIRES: holding data
0884     void remove_prefix(size_t n);  // REQUIRES: holding data
0885     void AppendArray(absl::string_view src, MethodIdentifier method);
0886     absl::string_view FindFlatStartPiece() const;
0887 
0888     // Creates a CordRepFlat instance from the current inlined data with `extra'
0889     // bytes of desired additional capacity.
0890     absl::Nonnull<CordRepFlat*> MakeFlatWithExtraCapacity(size_t extra);
0891 
0892     // Sets the tree value for this instance. `rep` must not be null.
0893     // Requires the current instance to hold a tree, and a lock to be held on
0894     // any CordzInfo referenced by this instance. The latter is enforced through
0895     // the CordzUpdateScope argument. If the current instance is sampled, then
0896     // the CordzInfo instance is updated to reference the new `rep` value.
0897     void SetTree(absl::Nonnull<CordRep*> rep, const CordzUpdateScope& scope);
0898 
0899     // Identical to SetTree(), except that `rep` is allowed to be null, in
0900     // which case the current instance is reset to an empty value.
0901     void SetTreeOrEmpty(absl::Nullable<CordRep*> rep,
0902                         const CordzUpdateScope& scope);
0903 
0904     // Sets the tree value for this instance, and randomly samples this cord.
0905     // This function disregards existing contents in `data_`, and should be
0906     // called when a Cord is 'promoted' from an 'uninitialized' or 'inlined'
0907     // value to a non-inlined (tree / ring) value.
0908     void EmplaceTree(absl::Nonnull<CordRep*> rep, MethodIdentifier method);
0909 
0910     // Identical to EmplaceTree, except that it copies the parent stack from
0911     // the provided `parent` data if the parent is sampled.
0912     void EmplaceTree(absl::Nonnull<CordRep*> rep, const InlineData& parent,
0913                      MethodIdentifier method);
0914 
0915     // Commits the change of a newly created, or updated `rep` root value into
0916     // this cord. `old_rep` indicates the old (inlined or tree) value of the
0917     // cord, and determines if the commit invokes SetTree() or EmplaceTree().
0918     void CommitTree(absl::Nullable<const CordRep*> old_rep,
0919                     absl::Nonnull<CordRep*> rep, const CordzUpdateScope& scope,
0920                     MethodIdentifier method);
0921 
0922     void AppendTreeToInlined(absl::Nonnull<CordRep*> tree,
0923                              MethodIdentifier method);
0924     void AppendTreeToTree(absl::Nonnull<CordRep*> tree,
0925                           MethodIdentifier method);
0926     void AppendTree(absl::Nonnull<CordRep*> tree, MethodIdentifier method);
0927     void PrependTreeToInlined(absl::Nonnull<CordRep*> tree,
0928                               MethodIdentifier method);
0929     void PrependTreeToTree(absl::Nonnull<CordRep*> tree,
0930                            MethodIdentifier method);
0931     void PrependTree(absl::Nonnull<CordRep*> tree, MethodIdentifier method);
0932 
0933     bool IsSame(const InlineRep& other) const { return data_ == other.data_; }
0934 
0935     void CopyTo(absl::Nonnull<std::string*> dst) const {
0936       // memcpy is much faster when operating on a known size. On most supported
0937       // platforms, the small string optimization is large enough that resizing
0938       // to 15 bytes does not cause a memory allocation.
0939       absl::strings_internal::STLStringResizeUninitialized(dst, kMaxInline);
0940       data_.copy_max_inline_to(&(*dst)[0]);
0941       // erase is faster than resize because the logic for memory allocation is
0942       // not needed.
0943       dst->erase(inline_size());
0944     }
0945 
0946     // Copies the inline contents into `dst`. Assumes the cord is not empty.
0947     void CopyToArray(absl::Nonnull<char*> dst) const;
0948 
0949     bool is_tree() const { return data_.is_tree(); }
0950 
0951     // Returns true if the Cord is being profiled by cordz.
0952     bool is_profiled() const { return data_.is_tree() && data_.is_profiled(); }
0953 
0954     // Returns the available inlined capacity, or 0 if is_tree() == true.
0955     size_t remaining_inline_capacity() const {
0956       return data_.is_tree() ? 0 : kMaxInline - data_.inline_size();
0957     }
0958 
0959     // Returns the profiled CordzInfo, or nullptr if not sampled.
0960     absl::Nullable<absl::cord_internal::CordzInfo*> cordz_info() const {
0961       return data_.cordz_info();
0962     }
0963 
0964     // Sets the profiled CordzInfo.
0965     void set_cordz_info(absl::Nonnull<cord_internal::CordzInfo*> cordz_info) {
0966       assert(cordz_info != nullptr);
0967       data_.set_cordz_info(cordz_info);
0968     }
0969 
0970     // Resets the current cordz_info to null / empty.
0971     void clear_cordz_info() { data_.clear_cordz_info(); }
0972 
0973    private:
0974     friend class Cord;
0975 
0976     void AssignSlow(const InlineRep& src);
0977     // Unrefs the tree and stops profiling.
0978     void UnrefTree();
0979 
0980     void ResetToEmpty() { data_ = {}; }
0981 
0982     void set_inline_size(size_t size) { data_.set_inline_size(size); }
0983     size_t inline_size() const { return data_.inline_size(); }
0984 
0985     // Empty cords that carry a checksum have a CordRepCrc node with a null
0986     // child node. The code can avoid lots of special cases where it would
0987     // otherwise transition from tree to inline storage if we just remove the
0988     // CordRepCrc node before mutations. Must never be called inside a
0989     // CordzUpdateScope since it untracks the cordz info.
0990     void MaybeRemoveEmptyCrcNode();
0991 
0992     cord_internal::InlineData data_;
0993   };
0994   InlineRep contents_;
0995 
0996   // Helper for GetFlat() and TryFlat().
0997   static bool GetFlatAux(absl::Nonnull<absl::cord_internal::CordRep*> rep,
0998                          absl::Nonnull<absl::string_view*> fragment);
0999 
1000   // Helper for ForEachChunk().
1001   static void ForEachChunkAux(
1002       absl::Nonnull<absl::cord_internal::CordRep*> rep,
1003       absl::FunctionRef<void(absl::string_view)> callback);
1004 
1005   // The destructor for non-empty Cords.
1006   void DestroyCordSlow();
1007 
1008   // Out-of-line implementation of slower parts of logic.
1009   void CopyToArraySlowPath(absl::Nonnull<char*> dst) const;
1010   int CompareSlowPath(absl::string_view rhs, size_t compared_size,
1011                       size_t size_to_compare) const;
1012   int CompareSlowPath(const Cord& rhs, size_t compared_size,
1013                       size_t size_to_compare) const;
1014   bool EqualsImpl(absl::string_view rhs, size_t size_to_compare) const;
1015   bool EqualsImpl(const Cord& rhs, size_t size_to_compare) const;
1016   int CompareImpl(const Cord& rhs) const;
1017 
1018   template <typename ResultType, typename RHS>
1019   friend ResultType GenericCompare(const Cord& lhs, const RHS& rhs,
1020                                    size_t size_to_compare);
1021   static absl::string_view GetFirstChunk(const Cord& c);
1022   static absl::string_view GetFirstChunk(absl::string_view sv);
1023 
1024   // Returns a new reference to contents_.tree(), or steals an existing
1025   // reference if called on an rvalue.
1026   absl::Nonnull<absl::cord_internal::CordRep*> TakeRep() const&;
1027   absl::Nonnull<absl::cord_internal::CordRep*> TakeRep() &&;
1028 
1029   // Helper for Append().
1030   template <typename C>
1031   void AppendImpl(C&& src);
1032 
1033   // Appends / Prepends `src` to this instance, using precise sizing.
1034   // This method does explicitly not attempt to use any spare capacity
1035   // in any pending last added private owned flat.
1036   // Requires `src` to be <= kMaxFlatLength.
1037   void AppendPrecise(absl::string_view src, MethodIdentifier method);
1038   void PrependPrecise(absl::string_view src, MethodIdentifier method);
1039 
1040   CordBuffer GetAppendBufferSlowPath(size_t block_size, size_t capacity,
1041                                      size_t min_capacity);
1042 
1043   // Prepends the provided data to this instance. `method` contains the public
1044   // API method for this action which is tracked for Cordz sampling purposes.
1045   void PrependArray(absl::string_view src, MethodIdentifier method);
1046 
1047   // Assigns the value in 'src' to this instance, 'stealing' its contents.
1048   // Requires src.length() > kMaxBytesToCopy.
1049   Cord& AssignLargeString(std::string&& src);
1050 
1051   // Helper for AbslHashValue().
1052   template <typename H>
1053   H HashFragmented(H hash_state) const {
1054     typename H::AbslInternalPiecewiseCombiner combiner;
1055     ForEachChunk([&combiner, &hash_state](absl::string_view chunk) {
1056       hash_state = combiner.add_buffer(std::move(hash_state), chunk.data(),
1057                                        chunk.size());
1058     });
1059     return H::combine(combiner.finalize(std::move(hash_state)), size());
1060   }
1061 
1062   friend class CrcCord;
1063   void SetCrcCordState(crc_internal::CrcCordState state);
1064   absl::Nullable<const crc_internal::CrcCordState*> MaybeGetCrcCordState()
1065       const;
1066 
1067   CharIterator FindImpl(CharIterator it, absl::string_view needle) const;
1068 };
1069 
1070 ABSL_NAMESPACE_END
1071 }  // namespace absl
1072 
1073 namespace absl {
1074 ABSL_NAMESPACE_BEGIN
1075 
1076 // allow a Cord to be logged
1077 extern std::ostream& operator<<(std::ostream& out, const Cord& cord);
1078 
1079 // ------------------------------------------------------------------
1080 // Internal details follow.  Clients should ignore.
1081 
1082 namespace cord_internal {
1083 
1084 // Does non-template-specific `CordRepExternal` initialization.
1085 // Requires `data` to be non-empty.
1086 void InitializeCordRepExternal(absl::string_view data,
1087                                absl::Nonnull<CordRepExternal*> rep);
1088 
1089 // Creates a new `CordRep` that owns `data` and `releaser` and returns a pointer
1090 // to it. Requires `data` to be non-empty.
1091 template <typename Releaser>
1092 // NOLINTNEXTLINE - suppress clang-tidy raw pointer return.
1093 absl::Nonnull<CordRep*> NewExternalRep(absl::string_view data,
1094                                        Releaser&& releaser) {
1095   assert(!data.empty());
1096   using ReleaserType = absl::decay_t<Releaser>;
1097   CordRepExternal* rep = new CordRepExternalImpl<ReleaserType>(
1098       std::forward<Releaser>(releaser), 0);
1099   InitializeCordRepExternal(data, rep);
1100   return rep;
1101 }
1102 
1103 // Overload for function reference types that dispatches using a function
1104 // pointer because there are no `alignof()` or `sizeof()` a function reference.
1105 // NOLINTNEXTLINE - suppress clang-tidy raw pointer return.
1106 inline absl::Nonnull<CordRep*> NewExternalRep(absl::string_view data,
1107                                void (&releaser)(absl::string_view)) {
1108   return NewExternalRep(data, &releaser);
1109 }
1110 
1111 }  // namespace cord_internal
1112 
1113 template <typename Releaser>
1114 Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser) {
1115   Cord cord;
1116   if (ABSL_PREDICT_TRUE(!data.empty())) {
1117     cord.contents_.EmplaceTree(::absl::cord_internal::NewExternalRep(
1118                                    data, std::forward<Releaser>(releaser)),
1119                                Cord::MethodIdentifier::kMakeCordFromExternal);
1120   } else {
1121     using ReleaserType = absl::decay_t<Releaser>;
1122     cord_internal::InvokeReleaser(
1123         cord_internal::Rank0{}, ReleaserType(std::forward<Releaser>(releaser)),
1124         data);
1125   }
1126   return cord;
1127 }
1128 
1129 constexpr Cord::InlineRep::InlineRep(absl::string_view sv,
1130                                      absl::Nullable<CordRep*> rep)
1131     : data_(sv, rep) {}
1132 
1133 inline Cord::InlineRep::InlineRep(const Cord::InlineRep& src)
1134     : data_(InlineData::kDefaultInit) {
1135   if (CordRep* tree = src.tree()) {
1136     EmplaceTree(CordRep::Ref(tree), src.data_,
1137                 CordzUpdateTracker::kConstructorCord);
1138   } else {
1139     data_ = src.data_;
1140   }
1141 }
1142 
1143 inline Cord::InlineRep::InlineRep(Cord::InlineRep&& src) : data_(src.data_) {
1144   src.ResetToEmpty();
1145 }
1146 
1147 inline Cord::InlineRep& Cord::InlineRep::operator=(const Cord::InlineRep& src) {
1148   if (this == &src) {
1149     return *this;
1150   }
1151   if (!is_tree() && !src.is_tree()) {
1152     data_ = src.data_;
1153     return *this;
1154   }
1155   AssignSlow(src);
1156   return *this;
1157 }
1158 
1159 inline Cord::InlineRep& Cord::InlineRep::operator=(
1160     Cord::InlineRep&& src) noexcept {
1161   if (is_tree()) {
1162     UnrefTree();
1163   }
1164   data_ = src.data_;
1165   src.ResetToEmpty();
1166   return *this;
1167 }
1168 
1169 inline void Cord::InlineRep::Swap(absl::Nonnull<Cord::InlineRep*> rhs) {
1170   if (rhs == this) {
1171     return;
1172   }
1173   std::swap(data_, rhs->data_);
1174 }
1175 
1176 inline absl::Nullable<const char*> Cord::InlineRep::data() const {
1177   return is_tree() ? nullptr : data_.as_chars();
1178 }
1179 
1180 inline absl::Nonnull<const char*> Cord::InlineRep::as_chars() const {
1181   assert(!data_.is_tree());
1182   return data_.as_chars();
1183 }
1184 
1185 inline absl::Nonnull<absl::cord_internal::CordRep*> Cord::InlineRep::as_tree()
1186     const {
1187   assert(data_.is_tree());
1188   return data_.as_tree();
1189 }
1190 
1191 inline absl::Nullable<absl::cord_internal::CordRep*> Cord::InlineRep::tree()
1192     const {
1193   if (is_tree()) {
1194     return as_tree();
1195   } else {
1196     return nullptr;
1197   }
1198 }
1199 
1200 inline size_t Cord::InlineRep::size() const {
1201   return is_tree() ? as_tree()->length : inline_size();
1202 }
1203 
1204 inline absl::Nonnull<cord_internal::CordRepFlat*>
1205 Cord::InlineRep::MakeFlatWithExtraCapacity(size_t extra) {
1206   static_assert(cord_internal::kMinFlatLength >= sizeof(data_), "");
1207   size_t len = data_.inline_size();
1208   auto* result = CordRepFlat::New(len + extra);
1209   result->length = len;
1210   data_.copy_max_inline_to(result->Data());
1211   return result;
1212 }
1213 
1214 inline void Cord::InlineRep::EmplaceTree(absl::Nonnull<CordRep*> rep,
1215                                          MethodIdentifier method) {
1216   assert(rep);
1217   data_.make_tree(rep);
1218   CordzInfo::MaybeTrackCord(data_, method);
1219 }
1220 
1221 inline void Cord::InlineRep::EmplaceTree(absl::Nonnull<CordRep*> rep,
1222                                          const InlineData& parent,
1223                                          MethodIdentifier method) {
1224   data_.make_tree(rep);
1225   CordzInfo::MaybeTrackCord(data_, parent, method);
1226 }
1227 
1228 inline void Cord::InlineRep::SetTree(absl::Nonnull<CordRep*> rep,
1229                                      const CordzUpdateScope& scope) {
1230   assert(rep);
1231   assert(data_.is_tree());
1232   data_.set_tree(rep);
1233   scope.SetCordRep(rep);
1234 }
1235 
1236 inline void Cord::InlineRep::SetTreeOrEmpty(absl::Nullable<CordRep*> rep,
1237                                             const CordzUpdateScope& scope) {
1238   assert(data_.is_tree());
1239   if (rep) {
1240     data_.set_tree(rep);
1241   } else {
1242     data_ = {};
1243   }
1244   scope.SetCordRep(rep);
1245 }
1246 
1247 inline void Cord::InlineRep::CommitTree(absl::Nullable<const CordRep*> old_rep,
1248                                         absl::Nonnull<CordRep*> rep,
1249                                         const CordzUpdateScope& scope,
1250                                         MethodIdentifier method) {
1251   if (old_rep) {
1252     SetTree(rep, scope);
1253   } else {
1254     EmplaceTree(rep, method);
1255   }
1256 }
1257 
1258 inline absl::Nullable<absl::cord_internal::CordRep*> Cord::InlineRep::clear() {
1259   if (is_tree()) {
1260     CordzInfo::MaybeUntrackCord(cordz_info());
1261   }
1262   absl::cord_internal::CordRep* result = tree();
1263   ResetToEmpty();
1264   return result;
1265 }
1266 
1267 inline void Cord::InlineRep::CopyToArray(absl::Nonnull<char*> dst) const {
1268   assert(!is_tree());
1269   size_t n = inline_size();
1270   assert(n != 0);
1271   cord_internal::SmallMemmove(dst, data_.as_chars(), n);
1272 }
1273 
1274 inline void Cord::InlineRep::MaybeRemoveEmptyCrcNode() {
1275   CordRep* rep = tree();
1276   if (rep == nullptr || ABSL_PREDICT_TRUE(rep->length > 0)) {
1277     return;
1278   }
1279   assert(rep->IsCrc());
1280   assert(rep->crc()->child == nullptr);
1281   CordzInfo::MaybeUntrackCord(cordz_info());
1282   CordRep::Unref(rep);
1283   ResetToEmpty();
1284 }
1285 
1286 constexpr inline Cord::Cord() noexcept {}
1287 
1288 inline Cord::Cord(absl::string_view src)
1289     : Cord(src, CordzUpdateTracker::kConstructorString) {}
1290 
1291 template <typename T>
1292 constexpr Cord::Cord(strings_internal::StringConstant<T>)
1293     : contents_(strings_internal::StringConstant<T>::value,
1294                 strings_internal::StringConstant<T>::value.size() <=
1295                         cord_internal::kMaxInline
1296                     ? nullptr
1297                     : &cord_internal::ConstInitExternalStorage<
1298                           strings_internal::StringConstant<T>>::value) {}
1299 
1300 inline Cord& Cord::operator=(const Cord& x) {
1301   contents_ = x.contents_;
1302   return *this;
1303 }
1304 
1305 template <typename T, Cord::EnableIfString<T>>
1306 Cord& Cord::operator=(T&& src) {
1307   if (src.size() <= cord_internal::kMaxBytesToCopy) {
1308     return operator=(absl::string_view(src));
1309   } else {
1310     return AssignLargeString(std::forward<T>(src));
1311   }
1312 }
1313 
1314 inline Cord::Cord(const Cord& src) : contents_(src.contents_) {}
1315 
1316 inline Cord::Cord(Cord&& src) noexcept : contents_(std::move(src.contents_)) {}
1317 
1318 inline void Cord::swap(Cord& other) noexcept {
1319   contents_.Swap(&other.contents_);
1320 }
1321 
1322 inline Cord& Cord::operator=(Cord&& x) noexcept {
1323   contents_ = std::move(x.contents_);
1324   return *this;
1325 }
1326 
1327 extern template Cord::Cord(std::string&& src);
1328 
1329 inline size_t Cord::size() const {
1330   // Length is 1st field in str.rep_
1331   return contents_.size();
1332 }
1333 
1334 inline bool Cord::empty() const { return size() == 0; }
1335 
1336 inline size_t Cord::EstimatedMemoryUsage(
1337     CordMemoryAccounting accounting_method) const {
1338   size_t result = sizeof(Cord);
1339   if (const absl::cord_internal::CordRep* rep = contents_.tree()) {
1340     switch (accounting_method) {
1341       case CordMemoryAccounting::kFairShare:
1342         result += cord_internal::GetEstimatedFairShareMemoryUsage(rep);
1343         break;
1344       case CordMemoryAccounting::kTotalMorePrecise:
1345         result += cord_internal::GetMorePreciseMemoryUsage(rep);
1346         break;
1347       case CordMemoryAccounting::kTotal:
1348         result += cord_internal::GetEstimatedMemoryUsage(rep);
1349         break;
1350     }
1351   }
1352   return result;
1353 }
1354 
1355 inline absl::optional<absl::string_view> Cord::TryFlat() const {
1356   absl::cord_internal::CordRep* rep = contents_.tree();
1357   if (rep == nullptr) {
1358     return absl::string_view(contents_.data(), contents_.size());
1359   }
1360   absl::string_view fragment;
1361   if (GetFlatAux(rep, &fragment)) {
1362     return fragment;
1363   }
1364   return absl::nullopt;
1365 }
1366 
1367 inline absl::string_view Cord::Flatten() {
1368   absl::cord_internal::CordRep* rep = contents_.tree();
1369   if (rep == nullptr) {
1370     return absl::string_view(contents_.data(), contents_.size());
1371   } else {
1372     absl::string_view already_flat_contents;
1373     if (GetFlatAux(rep, &already_flat_contents)) {
1374       return already_flat_contents;
1375     }
1376   }
1377   return FlattenSlowPath();
1378 }
1379 
1380 inline void Cord::Append(absl::string_view src) {
1381   contents_.AppendArray(src, CordzUpdateTracker::kAppendString);
1382 }
1383 
1384 inline void Cord::Prepend(absl::string_view src) {
1385   PrependArray(src, CordzUpdateTracker::kPrependString);
1386 }
1387 
1388 inline void Cord::Append(CordBuffer buffer) {
1389   if (ABSL_PREDICT_FALSE(buffer.length() == 0)) return;
1390   absl::string_view short_value;
1391   if (CordRep* rep = buffer.ConsumeValue(short_value)) {
1392     contents_.AppendTree(rep, CordzUpdateTracker::kAppendCordBuffer);
1393   } else {
1394     AppendPrecise(short_value, CordzUpdateTracker::kAppendCordBuffer);
1395   }
1396 }
1397 
1398 inline void Cord::Prepend(CordBuffer buffer) {
1399   if (ABSL_PREDICT_FALSE(buffer.length() == 0)) return;
1400   absl::string_view short_value;
1401   if (CordRep* rep = buffer.ConsumeValue(short_value)) {
1402     contents_.PrependTree(rep, CordzUpdateTracker::kPrependCordBuffer);
1403   } else {
1404     PrependPrecise(short_value, CordzUpdateTracker::kPrependCordBuffer);
1405   }
1406 }
1407 
1408 inline CordBuffer Cord::GetAppendBuffer(size_t capacity, size_t min_capacity) {
1409   if (empty()) return CordBuffer::CreateWithDefaultLimit(capacity);
1410   return GetAppendBufferSlowPath(0, capacity, min_capacity);
1411 }
1412 
1413 inline CordBuffer Cord::GetCustomAppendBuffer(size_t block_size,
1414                                               size_t capacity,
1415                                               size_t min_capacity) {
1416   if (empty()) {
1417     return block_size ? CordBuffer::CreateWithCustomLimit(block_size, capacity)
1418                       : CordBuffer::CreateWithDefaultLimit(capacity);
1419   }
1420   return GetAppendBufferSlowPath(block_size, capacity, min_capacity);
1421 }
1422 
1423 extern template void Cord::Append(std::string&& src);
1424 extern template void Cord::Prepend(std::string&& src);
1425 
1426 inline int Cord::Compare(const Cord& rhs) const {
1427   if (!contents_.is_tree() && !rhs.contents_.is_tree()) {
1428     return contents_.data_.Compare(rhs.contents_.data_);
1429   }
1430 
1431   return CompareImpl(rhs);
1432 }
1433 
1434 // Does 'this' cord start/end with rhs
1435 inline bool Cord::StartsWith(const Cord& rhs) const {
1436   if (contents_.IsSame(rhs.contents_)) return true;
1437   size_t rhs_size = rhs.size();
1438   if (size() < rhs_size) return false;
1439   return EqualsImpl(rhs, rhs_size);
1440 }
1441 
1442 inline bool Cord::StartsWith(absl::string_view rhs) const {
1443   size_t rhs_size = rhs.size();
1444   if (size() < rhs_size) return false;
1445   return EqualsImpl(rhs, rhs_size);
1446 }
1447 
1448 inline void Cord::ChunkIterator::InitTree(
1449     absl::Nonnull<cord_internal::CordRep*> tree) {
1450   tree = cord_internal::SkipCrcNode(tree);
1451   if (tree->tag == cord_internal::BTREE) {
1452     current_chunk_ = btree_reader_.Init(tree->btree());
1453   } else {
1454     current_leaf_ = tree;
1455     current_chunk_ = cord_internal::EdgeData(tree);
1456   }
1457 }
1458 
1459 inline Cord::ChunkIterator::ChunkIterator(
1460     absl::Nonnull<cord_internal::CordRep*> tree) {
1461   bytes_remaining_ = tree->length;
1462   InitTree(tree);
1463 }
1464 
1465 inline Cord::ChunkIterator::ChunkIterator(absl::Nonnull<const Cord*> cord) {
1466   if (CordRep* tree = cord->contents_.tree()) {
1467     bytes_remaining_ = tree->length;
1468     if (ABSL_PREDICT_TRUE(bytes_remaining_ != 0)) {
1469       InitTree(tree);
1470     } else {
1471       current_chunk_ = {};
1472     }
1473   } else {
1474     bytes_remaining_ = cord->contents_.inline_size();
1475     current_chunk_ = {cord->contents_.data(), bytes_remaining_};
1476   }
1477 }
1478 
1479 inline Cord::ChunkIterator& Cord::ChunkIterator::AdvanceBtree() {
1480   current_chunk_ = btree_reader_.Next();
1481   return *this;
1482 }
1483 
1484 inline void Cord::ChunkIterator::AdvanceBytesBtree(size_t n) {
1485   assert(n >= current_chunk_.size());
1486   bytes_remaining_ -= n;
1487   if (bytes_remaining_) {
1488     if (n == current_chunk_.size()) {
1489       current_chunk_ = btree_reader_.Next();
1490     } else {
1491       size_t offset = btree_reader_.length() - bytes_remaining_;
1492       current_chunk_ = btree_reader_.Seek(offset);
1493     }
1494   } else {
1495     current_chunk_ = {};
1496   }
1497 }
1498 
1499 inline Cord::ChunkIterator& Cord::ChunkIterator::operator++() {
1500   ABSL_HARDENING_ASSERT(bytes_remaining_ > 0 &&
1501                         "Attempted to iterate past `end()`");
1502   assert(bytes_remaining_ >= current_chunk_.size());
1503   bytes_remaining_ -= current_chunk_.size();
1504   if (bytes_remaining_ > 0) {
1505     if (btree_reader_) {
1506       return AdvanceBtree();
1507     } else {
1508       assert(!current_chunk_.empty());  // Called on invalid iterator.
1509     }
1510     current_chunk_ = {};
1511   }
1512   return *this;
1513 }
1514 
1515 inline Cord::ChunkIterator Cord::ChunkIterator::operator++(int) {
1516   ChunkIterator tmp(*this);
1517   operator++();
1518   return tmp;
1519 }
1520 
1521 inline bool Cord::ChunkIterator::operator==(const ChunkIterator& other) const {
1522   return bytes_remaining_ == other.bytes_remaining_;
1523 }
1524 
1525 inline bool Cord::ChunkIterator::operator!=(const ChunkIterator& other) const {
1526   return !(*this == other);
1527 }
1528 
1529 inline Cord::ChunkIterator::reference Cord::ChunkIterator::operator*() const {
1530   ABSL_HARDENING_ASSERT(bytes_remaining_ != 0);
1531   return current_chunk_;
1532 }
1533 
1534 inline Cord::ChunkIterator::pointer Cord::ChunkIterator::operator->() const {
1535   ABSL_HARDENING_ASSERT(bytes_remaining_ != 0);
1536   return &current_chunk_;
1537 }
1538 
1539 inline void Cord::ChunkIterator::RemoveChunkPrefix(size_t n) {
1540   assert(n < current_chunk_.size());
1541   current_chunk_.remove_prefix(n);
1542   bytes_remaining_ -= n;
1543 }
1544 
1545 inline void Cord::ChunkIterator::AdvanceBytes(size_t n) {
1546   assert(bytes_remaining_ >= n);
1547   if (ABSL_PREDICT_TRUE(n < current_chunk_.size())) {
1548     RemoveChunkPrefix(n);
1549   } else if (n != 0) {
1550     if (btree_reader_) {
1551       AdvanceBytesBtree(n);
1552     } else {
1553       bytes_remaining_ = 0;
1554     }
1555   }
1556 }
1557 
1558 inline Cord::ChunkIterator Cord::chunk_begin() const {
1559   return ChunkIterator(this);
1560 }
1561 
1562 inline Cord::ChunkIterator Cord::chunk_end() const { return ChunkIterator(); }
1563 
1564 inline Cord::ChunkIterator Cord::ChunkRange::begin() const {
1565   return cord_->chunk_begin();
1566 }
1567 
1568 inline Cord::ChunkIterator Cord::ChunkRange::end() const {
1569   return cord_->chunk_end();
1570 }
1571 
1572 inline Cord::ChunkRange Cord::Chunks() const { return ChunkRange(this); }
1573 
1574 inline Cord::CharIterator& Cord::CharIterator::operator++() {
1575   if (ABSL_PREDICT_TRUE(chunk_iterator_->size() > 1)) {
1576     chunk_iterator_.RemoveChunkPrefix(1);
1577   } else {
1578     ++chunk_iterator_;
1579   }
1580   return *this;
1581 }
1582 
1583 inline Cord::CharIterator Cord::CharIterator::operator++(int) {
1584   CharIterator tmp(*this);
1585   operator++();
1586   return tmp;
1587 }
1588 
1589 inline bool Cord::CharIterator::operator==(const CharIterator& other) const {
1590   return chunk_iterator_ == other.chunk_iterator_;
1591 }
1592 
1593 inline bool Cord::CharIterator::operator!=(const CharIterator& other) const {
1594   return !(*this == other);
1595 }
1596 
1597 inline Cord::CharIterator::reference Cord::CharIterator::operator*() const {
1598   return *chunk_iterator_->data();
1599 }
1600 
1601 inline Cord::CharIterator::pointer Cord::CharIterator::operator->() const {
1602   return chunk_iterator_->data();
1603 }
1604 
1605 inline Cord Cord::AdvanceAndRead(absl::Nonnull<CharIterator*> it,
1606                                  size_t n_bytes) {
1607   assert(it != nullptr);
1608   return it->chunk_iterator_.AdvanceAndReadBytes(n_bytes);
1609 }
1610 
1611 inline void Cord::Advance(absl::Nonnull<CharIterator*> it, size_t n_bytes) {
1612   assert(it != nullptr);
1613   it->chunk_iterator_.AdvanceBytes(n_bytes);
1614 }
1615 
1616 inline absl::string_view Cord::ChunkRemaining(const CharIterator& it) {
1617   return *it.chunk_iterator_;
1618 }
1619 
1620 inline Cord::CharIterator Cord::char_begin() const {
1621   return CharIterator(this);
1622 }
1623 
1624 inline Cord::CharIterator Cord::char_end() const { return CharIterator(); }
1625 
1626 inline Cord::CharIterator Cord::CharRange::begin() const {
1627   return cord_->char_begin();
1628 }
1629 
1630 inline Cord::CharIterator Cord::CharRange::end() const {
1631   return cord_->char_end();
1632 }
1633 
1634 inline Cord::CharRange Cord::Chars() const { return CharRange(this); }
1635 
1636 inline void Cord::ForEachChunk(
1637     absl::FunctionRef<void(absl::string_view)> callback) const {
1638   absl::cord_internal::CordRep* rep = contents_.tree();
1639   if (rep == nullptr) {
1640     callback(absl::string_view(contents_.data(), contents_.size()));
1641   } else {
1642     ForEachChunkAux(rep, callback);
1643   }
1644 }
1645 
1646 // Nonmember Cord-to-Cord relational operators.
1647 inline bool operator==(const Cord& lhs, const Cord& rhs) {
1648   if (lhs.contents_.IsSame(rhs.contents_)) return true;
1649   size_t rhs_size = rhs.size();
1650   if (lhs.size() != rhs_size) return false;
1651   return lhs.EqualsImpl(rhs, rhs_size);
1652 }
1653 
1654 inline bool operator!=(const Cord& x, const Cord& y) { return !(x == y); }
1655 inline bool operator<(const Cord& x, const Cord& y) { return x.Compare(y) < 0; }
1656 inline bool operator>(const Cord& x, const Cord& y) { return x.Compare(y) > 0; }
1657 inline bool operator<=(const Cord& x, const Cord& y) {
1658   return x.Compare(y) <= 0;
1659 }
1660 inline bool operator>=(const Cord& x, const Cord& y) {
1661   return x.Compare(y) >= 0;
1662 }
1663 
1664 // Nonmember Cord-to-absl::string_view relational operators.
1665 //
1666 // Due to implicit conversions, these also enable comparisons of Cord with
1667 // std::string and const char*.
1668 inline bool operator==(const Cord& lhs, absl::string_view rhs) {
1669   size_t lhs_size = lhs.size();
1670   size_t rhs_size = rhs.size();
1671   if (lhs_size != rhs_size) return false;
1672   return lhs.EqualsImpl(rhs, rhs_size);
1673 }
1674 
1675 inline bool operator==(absl::string_view x, const Cord& y) { return y == x; }
1676 inline bool operator!=(const Cord& x, absl::string_view y) { return !(x == y); }
1677 inline bool operator!=(absl::string_view x, const Cord& y) { return !(x == y); }
1678 inline bool operator<(const Cord& x, absl::string_view y) {
1679   return x.Compare(y) < 0;
1680 }
1681 inline bool operator<(absl::string_view x, const Cord& y) {
1682   return y.Compare(x) > 0;
1683 }
1684 inline bool operator>(const Cord& x, absl::string_view y) { return y < x; }
1685 inline bool operator>(absl::string_view x, const Cord& y) { return y < x; }
1686 inline bool operator<=(const Cord& x, absl::string_view y) { return !(y < x); }
1687 inline bool operator<=(absl::string_view x, const Cord& y) { return !(y < x); }
1688 inline bool operator>=(const Cord& x, absl::string_view y) { return !(x < y); }
1689 inline bool operator>=(absl::string_view x, const Cord& y) { return !(x < y); }
1690 
1691 // Some internals exposed to test code.
1692 namespace strings_internal {
1693 class CordTestAccess {
1694  public:
1695   static size_t FlatOverhead();
1696   static size_t MaxFlatLength();
1697   static size_t SizeofCordRepExternal();
1698   static size_t SizeofCordRepSubstring();
1699   static size_t FlatTagToLength(uint8_t tag);
1700   static uint8_t LengthToTag(size_t s);
1701 };
1702 }  // namespace strings_internal
1703 ABSL_NAMESPACE_END
1704 }  // namespace absl
1705 
1706 #endif  // ABSL_STRINGS_CORD_H_