/src/abseil-cpp/absl/strings/cord.cc
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 | | #include "absl/strings/cord.h" |
16 | | |
17 | | #include <algorithm> |
18 | | #include <cassert> |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <cstdio> |
22 | | #include <cstdlib> |
23 | | #include <cstring> |
24 | | #include <iomanip> |
25 | | #include <ios> |
26 | | #include <iostream> |
27 | | #include <limits> |
28 | | #include <memory> |
29 | | #include <optional> |
30 | | #include <ostream> |
31 | | #include <sstream> |
32 | | #include <string> |
33 | | #include <utility> |
34 | | |
35 | | #include "absl/base/attributes.h" |
36 | | #include "absl/base/config.h" |
37 | | #include "absl/base/internal/endian.h" |
38 | | #include "absl/base/internal/hardening.h" |
39 | | #include "absl/base/internal/raw_logging.h" |
40 | | #include "absl/base/macros.h" |
41 | | #include "absl/base/nullability.h" |
42 | | #include "absl/base/optimization.h" |
43 | | #include "absl/container/inlined_vector.h" |
44 | | #include "absl/crc/crc32c.h" |
45 | | #include "absl/crc/internal/crc_cord_state.h" |
46 | | #include "absl/functional/function_ref.h" |
47 | | #include "absl/strings/cord_buffer.h" |
48 | | #include "absl/strings/escaping.h" |
49 | | #include "absl/strings/internal/append_and_overwrite.h" |
50 | | #include "absl/strings/internal/cord_data_edge.h" |
51 | | #include "absl/strings/internal/cord_internal.h" |
52 | | #include "absl/strings/internal/cord_rep_btree.h" |
53 | | #include "absl/strings/internal/cord_rep_crc.h" |
54 | | #include "absl/strings/internal/cord_rep_flat.h" |
55 | | #include "absl/strings/internal/cordz_update_tracker.h" |
56 | | #include "absl/strings/match.h" |
57 | | #include "absl/strings/resize_and_overwrite.h" |
58 | | #include "absl/strings/str_cat.h" |
59 | | #include "absl/strings/string_view.h" |
60 | | #include "absl/strings/strip.h" |
61 | | #include "absl/types/span.h" |
62 | | |
63 | | namespace absl { |
64 | | ABSL_NAMESPACE_BEGIN |
65 | | |
66 | | using ::absl::cord_internal::CordRep; |
67 | | using ::absl::cord_internal::CordRepBtree; |
68 | | using ::absl::cord_internal::CordRepCrc; |
69 | | using ::absl::cord_internal::CordRepExternal; |
70 | | using ::absl::cord_internal::CordRepFlat; |
71 | | using ::absl::cord_internal::CordRepSubstring; |
72 | | using ::absl::cord_internal::CordzUpdateTracker; |
73 | | using ::absl::cord_internal::InlineData; |
74 | | using ::absl::cord_internal::kMaxFlatLength; |
75 | | using ::absl::cord_internal::kMinFlatLength; |
76 | | |
77 | | using ::absl::cord_internal::kInlinedVectorSize; |
78 | | using ::absl::cord_internal::kMaxBytesToCopy; |
79 | | |
80 | | static void DumpNode(CordRep* absl_nonnull nonnull_rep, bool include_data, |
81 | | std::ostream* absl_nonnull os, int indent = 0); |
82 | | static bool VerifyNode(CordRep* absl_nonnull root, |
83 | | CordRep* absl_nonnull start_node); |
84 | | |
85 | 0 | static inline CordRep* absl_nullable VerifyTree(CordRep* absl_nullable node) { |
86 | 0 | assert(node == nullptr || VerifyNode(node, node)); |
87 | 0 | static_cast<void>(&VerifyNode); |
88 | 0 | return node; |
89 | 0 | } |
90 | | |
91 | | static CordRepFlat* absl_nonnull CreateFlat(const char* absl_nonnull data, |
92 | 0 | size_t length, size_t alloc_hint) { |
93 | 0 | CordRepFlat* flat = CordRepFlat::New(length + alloc_hint); |
94 | 0 | flat->length = length; |
95 | 0 | memcpy(flat->Data(), data, length); |
96 | 0 | return flat; |
97 | 0 | } |
98 | | |
99 | | // Creates a new flat or Btree out of the specified array. |
100 | | // The returned node has a refcount of 1. |
101 | | static CordRep* absl_nonnull NewBtree(const char* absl_nonnull data, |
102 | 0 | size_t length, size_t alloc_hint) { |
103 | 0 | if (length <= kMaxFlatLength) { |
104 | 0 | return CreateFlat(data, length, alloc_hint); |
105 | 0 | } |
106 | 0 | CordRepFlat* flat = CreateFlat(data, kMaxFlatLength, 0); |
107 | 0 | data += kMaxFlatLength; |
108 | 0 | length -= kMaxFlatLength; |
109 | 0 | auto* root = CordRepBtree::Create(flat); |
110 | 0 | return CordRepBtree::Append(root, {data, length}, alloc_hint); |
111 | 0 | } |
112 | | |
113 | | // Create a new tree out of the specified array. |
114 | | // The returned node has a refcount of 1. |
115 | | static CordRep* absl_nullable NewTree(const char* absl_nullable data, |
116 | 0 | size_t length, size_t alloc_hint) { |
117 | 0 | if (length == 0) return nullptr; |
118 | 0 | return NewBtree(data, length, alloc_hint); |
119 | 0 | } |
120 | | |
121 | | namespace cord_internal { |
122 | | |
123 | | void InitializeCordRepExternal(absl::string_view data, |
124 | 0 | CordRepExternal* absl_nonnull rep) { |
125 | 0 | assert(!data.empty()); |
126 | 0 | rep->length = data.size(); |
127 | 0 | rep->tag = EXTERNAL; |
128 | 0 | rep->base = data.data(); |
129 | 0 | VerifyTree(rep); |
130 | 0 | } |
131 | | |
132 | | } // namespace cord_internal |
133 | | |
134 | | // Creates a CordRep from the provided string. If the string is large enough, |
135 | | // and not wasteful, we move the string into an external cord rep, preserving |
136 | | // the already allocated string contents. |
137 | | // Requires the provided string length to be larger than `kMaxInline`. |
138 | 0 | static CordRep* absl_nonnull CordRepFromString(std::string&& src) { |
139 | 0 | assert(src.length() > cord_internal::kMaxInline); |
140 | 0 | if ( |
141 | | // String is short: copy data to avoid external block overhead. |
142 | 0 | src.size() <= kMaxBytesToCopy || |
143 | | // String is wasteful: copy data to avoid pinning too much unused memory. |
144 | 0 | src.size() < src.capacity() / 2 |
145 | 0 | ) { |
146 | 0 | return NewTree(src.data(), src.size(), 0); |
147 | 0 | } |
148 | | |
149 | 0 | struct StringReleaser { |
150 | 0 | void operator()(absl::string_view /* data */) {} |
151 | 0 | std::string data; |
152 | 0 | }; |
153 | 0 | const absl::string_view original_data = src; |
154 | 0 | auto* rep = |
155 | 0 | static_cast<::absl::cord_internal::CordRepExternalImpl<StringReleaser>*>( |
156 | 0 | absl::cord_internal::NewExternalRep(original_data, |
157 | 0 | StringReleaser{std::move(src)})); |
158 | | // Moving src may have invalidated its data pointer, so adjust it. |
159 | 0 | rep->base = rep->template get<0>().data.data(); |
160 | 0 | return rep; |
161 | 0 | } |
162 | | |
163 | | // -------------------------------------------------------------------- |
164 | | // Cord::InlineRep functions |
165 | | |
166 | | inline void Cord::InlineRep::set_data(const char* absl_nullable data, |
167 | 0 | size_t n) { |
168 | 0 | static_assert(kMaxInline == 15, "set_data is hard-coded for a length of 15"); |
169 | 0 | assert(data != nullptr || n == 0); |
170 | 0 | data_.set_inline_data(data, n); |
171 | 0 | } |
172 | | |
173 | 0 | inline char* absl_nonnull Cord::InlineRep::set_data(size_t n) { |
174 | 0 | assert(n <= kMaxInline); |
175 | 0 | ResetToEmpty(); |
176 | 0 | set_inline_size(n); |
177 | 0 | return data_.as_chars(); |
178 | 0 | } |
179 | | |
180 | 0 | inline void Cord::InlineRep::reduce_size(size_t n) { |
181 | 0 | size_t tag = inline_size(); |
182 | 0 | assert(tag <= kMaxInline); |
183 | 0 | assert(tag >= n); |
184 | 0 | tag -= n; |
185 | 0 | memset(data_.as_chars() + tag, 0, n); |
186 | 0 | set_inline_size(tag); |
187 | 0 | } |
188 | | |
189 | 0 | inline void Cord::InlineRep::remove_prefix(size_t n) { |
190 | 0 | cord_internal::SmallMemmove(data_.as_chars(), data_.as_chars() + n, |
191 | 0 | inline_size() - n); |
192 | 0 | reduce_size(n); |
193 | 0 | } |
194 | | |
195 | | // Returns `rep` converted into a CordRepBtree. |
196 | | // Directly returns `rep` if `rep` is already a CordRepBtree. |
197 | 0 | static CordRepBtree* absl_nonnull ForceBtree(CordRep* rep) { |
198 | 0 | return rep->IsBtree() |
199 | 0 | ? rep->btree() |
200 | 0 | : CordRepBtree::Create(cord_internal::RemoveCrcNode(rep)); |
201 | 0 | } |
202 | | |
203 | | void Cord::InlineRep::AppendTreeToInlined(CordRep* absl_nonnull tree, |
204 | 0 | MethodIdentifier method) { |
205 | 0 | assert(!is_tree()); |
206 | 0 | if (!data_.is_empty()) { |
207 | 0 | CordRepFlat* flat = MakeFlatWithExtraCapacity(0); |
208 | 0 | tree = CordRepBtree::Append(CordRepBtree::Create(flat), tree); |
209 | 0 | } |
210 | 0 | EmplaceTree(tree, method); |
211 | 0 | } |
212 | | |
213 | | void Cord::InlineRep::AppendTreeToTree(CordRep* absl_nonnull tree, |
214 | 0 | MethodIdentifier method) { |
215 | 0 | assert(is_tree()); |
216 | 0 | const CordzUpdateScope scope(data_.cordz_info(), method); |
217 | 0 | tree = CordRepBtree::Append(ForceBtree(data_.as_tree()), tree); |
218 | 0 | SetTree(tree, scope); |
219 | 0 | } |
220 | | |
221 | | void Cord::InlineRep::AppendTree(CordRep* absl_nonnull tree, |
222 | 0 | MethodIdentifier method) { |
223 | 0 | assert(tree != nullptr); |
224 | 0 | assert(tree->length != 0); |
225 | 0 | assert(!tree->IsCrc()); |
226 | 0 | if (data_.is_tree()) { |
227 | 0 | AppendTreeToTree(tree, method); |
228 | 0 | } else { |
229 | 0 | AppendTreeToInlined(tree, method); |
230 | 0 | } |
231 | 0 | } |
232 | | |
233 | | void Cord::InlineRep::PrependTreeToInlined(CordRep* absl_nonnull tree, |
234 | 0 | MethodIdentifier method) { |
235 | 0 | assert(!is_tree()); |
236 | 0 | if (!data_.is_empty()) { |
237 | 0 | CordRepFlat* flat = MakeFlatWithExtraCapacity(0); |
238 | 0 | tree = CordRepBtree::Prepend(CordRepBtree::Create(flat), tree); |
239 | 0 | } |
240 | 0 | EmplaceTree(tree, method); |
241 | 0 | } |
242 | | |
243 | | void Cord::InlineRep::PrependTreeToTree(CordRep* absl_nonnull tree, |
244 | 0 | MethodIdentifier method) { |
245 | 0 | assert(is_tree()); |
246 | 0 | const CordzUpdateScope scope(data_.cordz_info(), method); |
247 | 0 | tree = CordRepBtree::Prepend(ForceBtree(data_.as_tree()), tree); |
248 | 0 | SetTree(tree, scope); |
249 | 0 | } |
250 | | |
251 | | void Cord::InlineRep::PrependTree(CordRep* absl_nonnull tree, |
252 | 0 | MethodIdentifier method) { |
253 | 0 | assert(tree != nullptr); |
254 | 0 | assert(tree->length != 0); |
255 | 0 | assert(!tree->IsCrc()); |
256 | 0 | if (data_.is_tree()) { |
257 | 0 | PrependTreeToTree(tree, method); |
258 | 0 | } else { |
259 | 0 | PrependTreeToInlined(tree, method); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | // Searches for a non-full flat node at the rightmost leaf of the tree. If a |
264 | | // suitable leaf is found, the function will update the length field for all |
265 | | // nodes to account for the size increase. The append region address will be |
266 | | // written to region and the actual size increase will be written to size. |
267 | | static inline bool PrepareAppendRegion(CordRep* absl_nonnull root, |
268 | | char* absl_nullable* absl_nonnull region, |
269 | | size_t* absl_nonnull size, |
270 | 0 | size_t max_length) { |
271 | 0 | if (root->IsBtree() && root->refcount.IsOne()) { |
272 | 0 | Span<char> span = root->btree()->GetAppendBuffer(max_length); |
273 | 0 | if (!span.empty()) { |
274 | 0 | *region = span.data(); |
275 | 0 | *size = span.size(); |
276 | 0 | return true; |
277 | 0 | } |
278 | 0 | } |
279 | | |
280 | 0 | CordRep* dst = root; |
281 | 0 | if (!dst->IsFlat() || !dst->refcount.IsOne()) { |
282 | 0 | *region = nullptr; |
283 | 0 | *size = 0; |
284 | 0 | return false; |
285 | 0 | } |
286 | | |
287 | 0 | const size_t in_use = dst->length; |
288 | 0 | const size_t capacity = dst->flat()->Capacity(); |
289 | 0 | if (in_use == capacity) { |
290 | 0 | *region = nullptr; |
291 | 0 | *size = 0; |
292 | 0 | return false; |
293 | 0 | } |
294 | | |
295 | 0 | const size_t size_increase = std::min(capacity - in_use, max_length); |
296 | 0 | dst->length += size_increase; |
297 | |
|
298 | 0 | *region = dst->flat()->Data() + in_use; |
299 | 0 | *size = size_increase; |
300 | 0 | return true; |
301 | 0 | } |
302 | | |
303 | 0 | void Cord::InlineRep::AssignSlow(const Cord::InlineRep& src) { |
304 | 0 | assert(&src != this); |
305 | 0 | assert(is_tree() || src.is_tree()); |
306 | 0 | auto constexpr method = CordzUpdateTracker::kAssignCord; |
307 | 0 | if (ABSL_PREDICT_TRUE(!is_tree())) { |
308 | 0 | EmplaceTree(CordRep::Ref(src.as_tree()), src.data_, method); |
309 | 0 | return; |
310 | 0 | } |
311 | | |
312 | 0 | CordRep* tree = as_tree(); |
313 | 0 | if (CordRep* src_tree = src.tree()) { |
314 | | // Leave any existing `cordz_info` in place, and let MaybeTrackCord() |
315 | | // decide if this cord should be (or remains to be) sampled or not. |
316 | 0 | data_.set_tree(CordRep::Ref(src_tree)); |
317 | 0 | CordzInfo::MaybeTrackCord(data_, src.data_, method); |
318 | 0 | } else { |
319 | 0 | CordzInfo::MaybeUntrackCord(data_.cordz_info()); |
320 | 0 | data_ = src.data_; |
321 | 0 | } |
322 | 0 | CordRep::Unref(tree); |
323 | 0 | } |
324 | | |
325 | 0 | void Cord::InlineRep::UnrefTree() { |
326 | 0 | if (is_tree()) { |
327 | 0 | CordzInfo::MaybeUntrackCord(data_.cordz_info()); |
328 | 0 | CordRep::Unref(tree()); |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | | // -------------------------------------------------------------------- |
333 | | // Constructors and destructors |
334 | | |
335 | | Cord::Cord(absl::string_view src, MethodIdentifier method) |
336 | 0 | : contents_(InlineData::kDefaultInit) { |
337 | 0 | const size_t n = src.size(); |
338 | 0 | if (n <= InlineRep::kMaxInline) { |
339 | 0 | contents_.set_data(src.data(), n); |
340 | 0 | } else { |
341 | 0 | CordRep* rep = NewTree(src.data(), n, 0); |
342 | 0 | contents_.EmplaceTree(rep, method); |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | template <typename T, Cord::EnableIfString<T>> |
347 | 0 | Cord::Cord(T&& src) : contents_(InlineData::kDefaultInit) { |
348 | 0 | if (src.size() <= InlineRep::kMaxInline) { |
349 | 0 | contents_.set_data(src.data(), src.size()); |
350 | 0 | } else { |
351 | 0 | CordRep* rep = CordRepFromString(std::forward<T>(src)); |
352 | 0 | contents_.EmplaceTree(rep, CordzUpdateTracker::kConstructorString); |
353 | 0 | } |
354 | 0 | } |
355 | | |
356 | | template Cord::Cord(std::string&& src); |
357 | | |
358 | | // The destruction code is separate so that the compiler can determine |
359 | | // that it does not need to call the destructor on a moved-from Cord. |
360 | 0 | void Cord::DestroyCordSlow() { |
361 | 0 | assert(contents_.is_tree()); |
362 | 0 | CordzInfo::MaybeUntrackCord(contents_.cordz_info()); |
363 | 0 | CordRep::Unref(VerifyTree(contents_.as_tree())); |
364 | 0 | } |
365 | | |
366 | | // -------------------------------------------------------------------- |
367 | | // Mutators |
368 | | |
369 | 0 | void Cord::Clear() { |
370 | 0 | if (CordRep* tree = contents_.clear()) { |
371 | 0 | CordRep::Unref(tree); |
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | Cord& Cord::AssignLargeString(std::string&& src) { |
376 | 0 | auto constexpr method = CordzUpdateTracker::kAssignString; |
377 | 0 | assert(src.size() > kMaxBytesToCopy); |
378 | 0 | CordRep* rep = CordRepFromString(std::move(src)); |
379 | 0 | if (CordRep* tree = contents_.tree()) { |
380 | 0 | CordzUpdateScope scope(contents_.cordz_info(), method); |
381 | 0 | contents_.SetTree(rep, scope); |
382 | 0 | CordRep::Unref(tree); |
383 | 0 | } else { |
384 | 0 | contents_.EmplaceTree(rep, method); |
385 | 0 | } |
386 | 0 | return *this; |
387 | 0 | } |
388 | | |
389 | 0 | Cord& Cord::operator=(absl::string_view src) { |
390 | 0 | auto constexpr method = CordzUpdateTracker::kAssignString; |
391 | 0 | const char* data = src.data(); |
392 | 0 | size_t length = src.size(); |
393 | 0 | CordRep* tree = contents_.tree(); |
394 | 0 | if (length <= InlineRep::kMaxInline) { |
395 | | // Embed into this->contents_, which is somewhat subtle: |
396 | | // - MaybeUntrackCord must be called before Unref(tree). |
397 | | // - MaybeUntrackCord must be called before set_data() clobbers cordz_info. |
398 | | // - set_data() must be called before Unref(tree) as it may reference tree. |
399 | 0 | if (tree != nullptr) CordzInfo::MaybeUntrackCord(contents_.cordz_info()); |
400 | 0 | contents_.set_data(data, length); |
401 | 0 | if (tree != nullptr) CordRep::Unref(tree); |
402 | 0 | return *this; |
403 | 0 | } |
404 | 0 | if (tree != nullptr) { |
405 | 0 | CordzUpdateScope scope(contents_.cordz_info(), method); |
406 | 0 | if (tree->IsFlat() && tree->flat()->Capacity() >= length && |
407 | 0 | tree->refcount.IsOne()) { |
408 | | // Copy in place if the existing FLAT node is reusable. |
409 | 0 | memmove(tree->flat()->Data(), data, length); |
410 | 0 | tree->length = length; |
411 | 0 | VerifyTree(tree); |
412 | 0 | return *this; |
413 | 0 | } |
414 | 0 | contents_.SetTree(NewTree(data, length, 0), scope); |
415 | 0 | CordRep::Unref(tree); |
416 | 0 | } else { |
417 | 0 | contents_.EmplaceTree(NewTree(data, length, 0), method); |
418 | 0 | } |
419 | 0 | return *this; |
420 | 0 | } |
421 | | |
422 | | // TODO(sanjay): Move to Cord::InlineRep section of file. For now, |
423 | | // we keep it here to make diffs easier. |
424 | | void Cord::InlineRep::AppendArray(absl::string_view src, |
425 | 0 | MethodIdentifier method) { |
426 | 0 | if (src.empty()) return; // memcpy(_, nullptr, 0) is undefined. |
427 | 0 | MaybeRemoveEmptyCrcNode(); |
428 | |
|
429 | 0 | size_t appended = 0; |
430 | 0 | CordRep* rep = tree(); |
431 | 0 | const CordRep* const root = rep; |
432 | 0 | CordzUpdateScope scope(root ? cordz_info() : nullptr, method); |
433 | 0 | if (root != nullptr) { |
434 | 0 | rep = cord_internal::RemoveCrcNode(rep); |
435 | 0 | char* region; |
436 | 0 | if (PrepareAppendRegion(rep, ®ion, &appended, src.size())) { |
437 | 0 | memcpy(region, src.data(), appended); |
438 | 0 | } |
439 | 0 | } else { |
440 | | // Try to fit in the inline buffer if possible. |
441 | 0 | size_t inline_length = inline_size(); |
442 | 0 | if (src.size() <= kMaxInline - inline_length) { |
443 | | // Append new data to embedded array |
444 | 0 | set_inline_size(inline_length + src.size()); |
445 | 0 | memcpy(data_.as_chars() + inline_length, src.data(), src.size()); |
446 | 0 | return; |
447 | 0 | } |
448 | | |
449 | | // Allocate flat to be a perfect fit on first append exceeding inlined size. |
450 | | // Subsequent growth will use amortized growth until we reach maximum flat |
451 | | // size. |
452 | 0 | rep = CordRepFlat::New(inline_length + src.size()); |
453 | 0 | appended = std::min(src.size(), rep->flat()->Capacity() - inline_length); |
454 | 0 | memcpy(rep->flat()->Data(), data_.as_chars(), inline_length); |
455 | 0 | memcpy(rep->flat()->Data() + inline_length, src.data(), appended); |
456 | 0 | rep->length = inline_length + appended; |
457 | 0 | } |
458 | | |
459 | 0 | src.remove_prefix(appended); |
460 | 0 | if (src.empty()) { |
461 | 0 | CommitTree(root, rep, scope, method); |
462 | 0 | return; |
463 | 0 | } |
464 | | |
465 | | // TODO(b/192061034): keep legacy 10% growth rate: consider other rates. |
466 | 0 | rep = ForceBtree(rep); |
467 | 0 | const size_t min_growth = std::max<size_t>(rep->length / 10, src.size()); |
468 | 0 | rep = CordRepBtree::Append(rep->btree(), src, min_growth - src.size()); |
469 | |
|
470 | 0 | CommitTree(root, rep, scope, method); |
471 | 0 | } |
472 | | |
473 | 0 | inline CordRep* absl_nonnull Cord::TakeRep() const& { |
474 | 0 | return CordRep::Ref(contents_.tree()); |
475 | 0 | } |
476 | | |
477 | 0 | inline CordRep* absl_nonnull Cord::TakeRep() && { |
478 | 0 | CordRep* rep = contents_.tree(); |
479 | 0 | contents_.clear(); |
480 | 0 | return rep; |
481 | 0 | } |
482 | | |
483 | | template <typename C> |
484 | 0 | inline void Cord::AppendImpl(C&& src) { |
485 | 0 | auto constexpr method = CordzUpdateTracker::kAppendCord; |
486 | |
|
487 | 0 | contents_.MaybeRemoveEmptyCrcNode(); |
488 | 0 | if (src.empty()) return; |
489 | | |
490 | 0 | if (empty()) { |
491 | | // Since destination is empty, we can avoid allocating a node, |
492 | 0 | if (src.contents_.is_tree()) { |
493 | | // by taking the tree directly |
494 | 0 | CordRep* rep = |
495 | 0 | cord_internal::RemoveCrcNode(std::forward<C>(src).TakeRep()); |
496 | 0 | contents_.EmplaceTree(rep, method); |
497 | 0 | } else { |
498 | | // or copying over inline data |
499 | 0 | contents_.data_ = src.contents_.data_; |
500 | 0 | } |
501 | 0 | return; |
502 | 0 | } |
503 | | |
504 | | // For short cords, it is faster to copy data if there is room in dst. |
505 | 0 | const size_t src_size = src.contents_.size(); |
506 | 0 | if (src_size <= kMaxBytesToCopy) { |
507 | 0 | CordRep* src_tree = src.contents_.tree(); |
508 | 0 | if (src_tree == nullptr) { |
509 | | // src has embedded data. |
510 | 0 | contents_.AppendArray({src.contents_.data(), src_size}, method); |
511 | 0 | return; |
512 | 0 | } |
513 | 0 | if (src_tree->IsFlat()) { |
514 | | // src tree just has one flat node. |
515 | 0 | contents_.AppendArray({src_tree->flat()->Data(), src_size}, method); |
516 | 0 | return; |
517 | 0 | } |
518 | 0 | if (&src == this) { |
519 | | // ChunkIterator below assumes that src is not modified during traversal. |
520 | 0 | Append(Cord(src)); |
521 | 0 | return; |
522 | 0 | } |
523 | | // TODO(mec): Should we only do this if "dst" has space? |
524 | 0 | for (absl::string_view chunk : src.Chunks()) { |
525 | 0 | Append(chunk); |
526 | 0 | } |
527 | 0 | return; |
528 | 0 | } |
529 | | |
530 | | // Guaranteed to be a tree (kMaxBytesToCopy > kInlinedSize) |
531 | 0 | CordRep* rep = cord_internal::RemoveCrcNode(std::forward<C>(src).TakeRep()); |
532 | 0 | contents_.AppendTree(rep, CordzUpdateTracker::kAppendCord); |
533 | 0 | } Unexecuted instantiation: void absl::lts_20260526::Cord::AppendImpl<absl::lts_20260526::Cord const&>(absl::lts_20260526::Cord const&) Unexecuted instantiation: void absl::lts_20260526::Cord::AppendImpl<absl::lts_20260526::Cord>(absl::lts_20260526::Cord&&) |
534 | | |
535 | | static CordRep::ExtractResult ExtractAppendBuffer(CordRep* absl_nonnull rep, |
536 | 0 | size_t min_capacity) { |
537 | 0 | switch (rep->tag) { |
538 | 0 | case cord_internal::BTREE: |
539 | 0 | return CordRepBtree::ExtractAppendBuffer(rep->btree(), min_capacity); |
540 | 0 | default: |
541 | 0 | if (rep->IsFlat() && rep->refcount.IsOne() && |
542 | 0 | rep->flat()->Capacity() - rep->length >= min_capacity) { |
543 | 0 | return {nullptr, rep}; |
544 | 0 | } |
545 | 0 | return {rep, nullptr}; |
546 | 0 | } |
547 | 0 | } |
548 | | |
549 | | static CordBuffer CreateAppendBuffer(InlineData& data, size_t block_size, |
550 | 0 | size_t capacity) { |
551 | | // Watch out for overflow, people can ask for size_t::max(). |
552 | 0 | const size_t size = data.inline_size(); |
553 | 0 | const size_t max_capacity = std::numeric_limits<size_t>::max() - size; |
554 | 0 | capacity = (std::min)(max_capacity, capacity) + size; |
555 | 0 | CordBuffer buffer = |
556 | 0 | block_size ? CordBuffer::CreateWithCustomLimit(block_size, capacity) |
557 | 0 | : CordBuffer::CreateWithDefaultLimit(capacity); |
558 | 0 | cord_internal::SmallMemmove(buffer.data(), data.as_chars(), size); |
559 | 0 | buffer.SetLength(size); |
560 | 0 | data = {}; |
561 | 0 | return buffer; |
562 | 0 | } |
563 | | |
564 | | CordBuffer Cord::GetAppendBufferSlowPath(size_t block_size, size_t capacity, |
565 | 0 | size_t min_capacity) { |
566 | 0 | auto constexpr method = CordzUpdateTracker::kGetAppendBuffer; |
567 | 0 | CordRep* tree = contents_.tree(); |
568 | 0 | if (tree != nullptr) { |
569 | 0 | CordzUpdateScope scope(contents_.cordz_info(), method); |
570 | 0 | CordRep::ExtractResult result = ExtractAppendBuffer(tree, min_capacity); |
571 | 0 | if (result.extracted != nullptr) { |
572 | 0 | contents_.SetTreeOrEmpty(result.tree, scope); |
573 | 0 | return CordBuffer(result.extracted->flat()); |
574 | 0 | } |
575 | 0 | return block_size ? CordBuffer::CreateWithCustomLimit(block_size, capacity) |
576 | 0 | : CordBuffer::CreateWithDefaultLimit(capacity); |
577 | 0 | } |
578 | 0 | return CreateAppendBuffer(contents_.data_, block_size, capacity); |
579 | 0 | } |
580 | | |
581 | 0 | void Cord::Append(const Cord& src) { AppendImpl(src); } |
582 | | |
583 | 0 | void Cord::Append(Cord&& src) { AppendImpl(std::move(src)); } |
584 | | |
585 | | template <typename T, Cord::EnableIfString<T>> |
586 | 0 | void Cord::Append(T&& src) { |
587 | 0 | if (src.size() <= kMaxBytesToCopy) { |
588 | 0 | Append(absl::string_view(src)); |
589 | 0 | } else { |
590 | 0 | CordRep* rep = CordRepFromString(std::forward<T>(src)); |
591 | 0 | contents_.AppendTree(rep, CordzUpdateTracker::kAppendString); |
592 | 0 | } |
593 | 0 | } |
594 | | |
595 | | template void Cord::Append(std::string&& src); |
596 | | |
597 | 0 | void Cord::Prepend(const Cord& src) { |
598 | 0 | contents_.MaybeRemoveEmptyCrcNode(); |
599 | 0 | if (src.empty()) return; |
600 | | |
601 | 0 | CordRep* src_tree = src.contents_.tree(); |
602 | 0 | if (src_tree != nullptr) { |
603 | 0 | CordRep::Ref(src_tree); |
604 | 0 | contents_.PrependTree(cord_internal::RemoveCrcNode(src_tree), |
605 | 0 | CordzUpdateTracker::kPrependCord); |
606 | 0 | return; |
607 | 0 | } |
608 | | |
609 | | // `src` cord is inlined. |
610 | 0 | absl::string_view src_contents(src.contents_.data(), src.contents_.size()); |
611 | 0 | return Prepend(src_contents); |
612 | 0 | } |
613 | | |
614 | 0 | void Cord::PrependArray(absl::string_view src, MethodIdentifier method) { |
615 | 0 | contents_.MaybeRemoveEmptyCrcNode(); |
616 | 0 | if (src.empty()) return; // memcpy(_, nullptr, 0) is undefined. |
617 | | |
618 | 0 | if (!contents_.is_tree()) { |
619 | 0 | size_t cur_size = contents_.inline_size(); |
620 | 0 | if (cur_size + src.size() <= InlineRep::kMaxInline) { |
621 | | // Use embedded storage. |
622 | 0 | InlineData data; |
623 | 0 | data.set_inline_size(cur_size + src.size()); |
624 | 0 | memcpy(data.as_chars(), src.data(), src.size()); |
625 | 0 | memcpy(data.as_chars() + src.size(), contents_.data(), cur_size); |
626 | 0 | contents_.data_ = data; |
627 | 0 | return; |
628 | 0 | } |
629 | 0 | } |
630 | 0 | CordRep* rep = NewTree(src.data(), src.size(), 0); |
631 | 0 | contents_.PrependTree(rep, method); |
632 | 0 | } |
633 | | |
634 | 0 | void Cord::AppendPrecise(absl::string_view src, MethodIdentifier method) { |
635 | 0 | assert(!src.empty()); |
636 | 0 | assert(src.size() <= cord_internal::kMaxFlatLength); |
637 | 0 | if (contents_.remaining_inline_capacity() >= src.size()) { |
638 | 0 | const size_t inline_length = contents_.inline_size(); |
639 | 0 | contents_.set_inline_size(inline_length + src.size()); |
640 | 0 | memcpy(contents_.data_.as_chars() + inline_length, src.data(), src.size()); |
641 | 0 | } else { |
642 | 0 | contents_.AppendTree(CordRepFlat::Create(src), method); |
643 | 0 | } |
644 | 0 | } |
645 | | |
646 | 0 | void Cord::PrependPrecise(absl::string_view src, MethodIdentifier method) { |
647 | 0 | assert(!src.empty()); |
648 | 0 | assert(src.size() <= cord_internal::kMaxFlatLength); |
649 | 0 | if (contents_.remaining_inline_capacity() >= src.size()) { |
650 | 0 | const size_t cur_size = contents_.inline_size(); |
651 | 0 | InlineData data; |
652 | 0 | data.set_inline_size(cur_size + src.size()); |
653 | 0 | memcpy(data.as_chars(), src.data(), src.size()); |
654 | 0 | memcpy(data.as_chars() + src.size(), contents_.data(), cur_size); |
655 | 0 | contents_.data_ = data; |
656 | 0 | } else { |
657 | 0 | contents_.PrependTree(CordRepFlat::Create(src), method); |
658 | 0 | } |
659 | 0 | } |
660 | | |
661 | | template <typename T, Cord::EnableIfString<T>> |
662 | 0 | inline void Cord::Prepend(T&& src) { |
663 | 0 | if (src.size() <= kMaxBytesToCopy) { |
664 | 0 | Prepend(absl::string_view(src)); |
665 | 0 | } else { |
666 | 0 | CordRep* rep = CordRepFromString(std::forward<T>(src)); |
667 | 0 | contents_.PrependTree(rep, CordzUpdateTracker::kPrependString); |
668 | 0 | } |
669 | 0 | } |
670 | | |
671 | | template void Cord::Prepend(std::string&& src); |
672 | | |
673 | 0 | void Cord::RemovePrefix(size_t n) { |
674 | 0 | ABSL_INTERNAL_CHECK(n <= size(), |
675 | 0 | absl::StrCat("Requested prefix size ", n, |
676 | 0 | " exceeds Cord's size ", size())); |
677 | 0 | contents_.MaybeRemoveEmptyCrcNode(); |
678 | 0 | CordRep* tree = contents_.tree(); |
679 | 0 | if (tree == nullptr) { |
680 | 0 | contents_.remove_prefix(n); |
681 | 0 | } else { |
682 | 0 | auto constexpr method = CordzUpdateTracker::kRemovePrefix; |
683 | 0 | CordzUpdateScope scope(contents_.cordz_info(), method); |
684 | 0 | tree = cord_internal::RemoveCrcNode(tree); |
685 | 0 | if (n >= tree->length) { |
686 | 0 | CordRep::Unref(tree); |
687 | 0 | tree = nullptr; |
688 | 0 | } else if (tree->IsBtree()) { |
689 | 0 | CordRep* old = tree; |
690 | 0 | tree = tree->btree()->SubTree(n, tree->length - n); |
691 | 0 | CordRep::Unref(old); |
692 | 0 | } else if (tree->IsSubstring() && tree->refcount.IsOne()) { |
693 | 0 | tree->substring()->start += n; |
694 | 0 | tree->length -= n; |
695 | 0 | } else { |
696 | 0 | CordRep* rep = CordRepSubstring::Substring(tree, n, tree->length - n); |
697 | 0 | CordRep::Unref(tree); |
698 | 0 | tree = rep; |
699 | 0 | } |
700 | 0 | contents_.SetTreeOrEmpty(tree, scope); |
701 | 0 | } |
702 | 0 | } |
703 | | |
704 | 0 | void Cord::RemoveSuffix(size_t n) { |
705 | 0 | ABSL_INTERNAL_CHECK(n <= size(), |
706 | 0 | absl::StrCat("Requested suffix size ", n, |
707 | 0 | " exceeds Cord's size ", size())); |
708 | 0 | contents_.MaybeRemoveEmptyCrcNode(); |
709 | 0 | CordRep* tree = contents_.tree(); |
710 | 0 | if (tree == nullptr) { |
711 | 0 | contents_.reduce_size(n); |
712 | 0 | } else { |
713 | 0 | auto constexpr method = CordzUpdateTracker::kRemoveSuffix; |
714 | 0 | CordzUpdateScope scope(contents_.cordz_info(), method); |
715 | 0 | tree = cord_internal::RemoveCrcNode(tree); |
716 | 0 | if (n >= tree->length) { |
717 | 0 | CordRep::Unref(tree); |
718 | 0 | tree = nullptr; |
719 | 0 | } else if (tree->IsBtree()) { |
720 | 0 | tree = CordRepBtree::RemoveSuffix(tree->btree(), n); |
721 | 0 | } else if (!tree->IsExternal() && tree->refcount.IsOne()) { |
722 | 0 | assert(tree->IsFlat() || tree->IsSubstring()); |
723 | 0 | tree->length -= n; |
724 | 0 | } else { |
725 | 0 | CordRep* rep = CordRepSubstring::Substring(tree, 0, tree->length - n); |
726 | 0 | CordRep::Unref(tree); |
727 | 0 | tree = rep; |
728 | 0 | } |
729 | 0 | contents_.SetTreeOrEmpty(tree, scope); |
730 | 0 | } |
731 | 0 | } |
732 | | |
733 | 0 | Cord Cord::Subcord(size_t pos, size_t new_size) const { |
734 | 0 | Cord sub_cord; |
735 | 0 | size_t length = size(); |
736 | 0 | if (pos > length) pos = length; |
737 | 0 | if (new_size > length - pos) new_size = length - pos; |
738 | 0 | if (new_size == 0) return sub_cord; |
739 | | |
740 | 0 | CordRep* tree = contents_.tree(); |
741 | 0 | if (tree == nullptr) { |
742 | 0 | sub_cord.contents_.set_data(contents_.data() + pos, new_size); |
743 | 0 | return sub_cord; |
744 | 0 | } |
745 | | |
746 | 0 | if (new_size <= InlineRep::kMaxInline) { |
747 | 0 | sub_cord.contents_.set_inline_size(new_size); |
748 | 0 | char* dest = sub_cord.contents_.data_.as_chars(); |
749 | 0 | Cord::ChunkIterator it = chunk_begin(); |
750 | 0 | it.AdvanceBytes(pos); |
751 | 0 | size_t remaining_size = new_size; |
752 | 0 | while (remaining_size > it->size()) { |
753 | 0 | cord_internal::SmallMemmove(dest, it->data(), it->size()); |
754 | 0 | remaining_size -= it->size(); |
755 | 0 | dest += it->size(); |
756 | 0 | ++it; |
757 | 0 | } |
758 | 0 | cord_internal::SmallMemmove(dest, it->data(), remaining_size); |
759 | 0 | return sub_cord; |
760 | 0 | } |
761 | | |
762 | 0 | tree = cord_internal::SkipCrcNode(tree); |
763 | 0 | if (tree->IsBtree()) { |
764 | 0 | tree = tree->btree()->SubTree(pos, new_size); |
765 | 0 | } else { |
766 | 0 | tree = CordRepSubstring::Substring(tree, pos, new_size); |
767 | 0 | } |
768 | 0 | sub_cord.contents_.EmplaceTree(tree, contents_.data_, |
769 | 0 | CordzUpdateTracker::kSubCord); |
770 | 0 | return sub_cord; |
771 | 0 | } |
772 | | |
773 | | // -------------------------------------------------------------------- |
774 | | // Comparators |
775 | | |
776 | | namespace { |
777 | | |
778 | 0 | int ClampResult(int memcmp_res) { |
779 | 0 | return static_cast<int>(memcmp_res > 0) - static_cast<int>(memcmp_res < 0); |
780 | 0 | } |
781 | | |
782 | | int CompareChunks(absl::string_view* absl_nonnull lhs, |
783 | | absl::string_view* absl_nonnull rhs, |
784 | 0 | size_t* absl_nonnull size_to_compare) { |
785 | 0 | size_t compared_size = std::min(lhs->size(), rhs->size()); |
786 | 0 | assert(*size_to_compare >= compared_size); |
787 | 0 | *size_to_compare -= compared_size; |
788 | |
|
789 | 0 | int memcmp_res = ::memcmp(lhs->data(), rhs->data(), compared_size); |
790 | 0 | if (memcmp_res != 0) return memcmp_res; |
791 | | |
792 | 0 | lhs->remove_prefix(compared_size); |
793 | 0 | rhs->remove_prefix(compared_size); |
794 | |
|
795 | 0 | return 0; |
796 | 0 | } |
797 | | |
798 | | // This overload set computes comparison results from memcmp result. This |
799 | | // interface is used inside GenericCompare below. Different implementations |
800 | | // are specialized for int and bool. For int we clamp result to {-1, 0, 1} |
801 | | // set. For bool we just interested in "value == 0". |
802 | | template <typename ResultType> |
803 | 0 | ResultType ComputeCompareResult(int memcmp_res) { |
804 | 0 | return ClampResult(memcmp_res); |
805 | 0 | } |
806 | | template <> |
807 | 0 | bool ComputeCompareResult<bool>(int memcmp_res) { |
808 | 0 | return memcmp_res == 0; |
809 | 0 | } |
810 | | |
811 | | } // namespace |
812 | | |
813 | | // Helper routine. Locates the first flat or external chunk of the Cord without |
814 | | // initializing the iterator, and returns a string_view referencing the data. |
815 | 0 | inline absl::string_view Cord::InlineRep::FindFlatStartPiece() const { |
816 | 0 | if (!is_tree()) { |
817 | 0 | return absl::string_view(data_.as_chars(), data_.inline_size()); |
818 | 0 | } |
819 | | |
820 | 0 | CordRep* node = cord_internal::SkipCrcNode(tree()); |
821 | 0 | if (node->IsFlat()) { |
822 | 0 | return absl::string_view(node->flat()->Data(), node->length); |
823 | 0 | } |
824 | | |
825 | 0 | if (node->IsExternal()) { |
826 | 0 | return absl::string_view(node->external()->base, node->length); |
827 | 0 | } |
828 | | |
829 | 0 | if (node->IsBtree()) { |
830 | 0 | CordRepBtree* tree = node->btree(); |
831 | 0 | int height = tree->height(); |
832 | 0 | while (--height >= 0) { |
833 | 0 | tree = tree->Edge(CordRepBtree::kFront)->btree(); |
834 | 0 | } |
835 | 0 | return tree->Data(tree->begin()); |
836 | 0 | } |
837 | | |
838 | | // Get the child node if we encounter a SUBSTRING. |
839 | 0 | size_t offset = 0; |
840 | 0 | size_t length = node->length; |
841 | 0 | assert(length != 0); |
842 | | |
843 | 0 | if (node->IsSubstring()) { |
844 | 0 | offset = node->substring()->start; |
845 | 0 | node = node->substring()->child; |
846 | 0 | } |
847 | |
|
848 | 0 | if (node->IsFlat()) { |
849 | 0 | return absl::string_view(node->flat()->Data() + offset, length); |
850 | 0 | } |
851 | | |
852 | 0 | assert(node->IsExternal() && "Expect FLAT or EXTERNAL node here"); |
853 | | |
854 | 0 | return absl::string_view(node->external()->base + offset, length); |
855 | 0 | } |
856 | | |
857 | 0 | void Cord::SetCrcCordState(crc_internal::CrcCordState state) { |
858 | 0 | auto constexpr method = CordzUpdateTracker::kSetExpectedChecksum; |
859 | 0 | if (empty()) { |
860 | 0 | contents_.MaybeRemoveEmptyCrcNode(); |
861 | 0 | CordRep* rep = CordRepCrc::New(nullptr, std::move(state)); |
862 | 0 | contents_.EmplaceTree(rep, method); |
863 | 0 | } else if (!contents_.is_tree()) { |
864 | 0 | CordRep* rep = contents_.MakeFlatWithExtraCapacity(0); |
865 | 0 | rep = CordRepCrc::New(rep, std::move(state)); |
866 | 0 | contents_.EmplaceTree(rep, method); |
867 | 0 | } else { |
868 | 0 | const CordzUpdateScope scope(contents_.data_.cordz_info(), method); |
869 | 0 | CordRep* rep = CordRepCrc::New(contents_.data_.as_tree(), std::move(state)); |
870 | 0 | contents_.SetTree(rep, scope); |
871 | 0 | } |
872 | 0 | } |
873 | | |
874 | 0 | void Cord::SetExpectedChecksum(uint32_t crc) { |
875 | | // Construct a CrcCordState with a single chunk. |
876 | 0 | crc_internal::CrcCordState state; |
877 | 0 | state.mutable_rep()->prefix_crc.push_back( |
878 | 0 | crc_internal::CrcCordState::PrefixCrc(size(), absl::crc32c_t{crc})); |
879 | 0 | SetCrcCordState(std::move(state)); |
880 | 0 | } |
881 | | |
882 | | const crc_internal::CrcCordState* absl_nullable Cord::MaybeGetCrcCordState() |
883 | 0 | const { |
884 | 0 | if (!contents_.is_tree() || !contents_.tree()->IsCrc()) { |
885 | 0 | return nullptr; |
886 | 0 | } |
887 | 0 | return &contents_.tree()->crc()->crc_cord_state; |
888 | 0 | } |
889 | | |
890 | 0 | std::optional<uint32_t> Cord::ExpectedChecksum() const { |
891 | 0 | if (!contents_.is_tree() || !contents_.tree()->IsCrc()) { |
892 | 0 | return std::nullopt; |
893 | 0 | } |
894 | 0 | return static_cast<uint32_t>( |
895 | 0 | contents_.tree()->crc()->crc_cord_state.Checksum()); |
896 | 0 | } |
897 | | |
898 | | inline int Cord::CompareSlowPath(absl::string_view rhs, size_t compared_size, |
899 | 0 | size_t size_to_compare) const { |
900 | 0 | auto advance = [](Cord::ChunkIterator* absl_nonnull it, |
901 | 0 | absl::string_view* absl_nonnull chunk) { |
902 | 0 | if (!chunk->empty()) return true; |
903 | 0 | ++*it; |
904 | 0 | if (it->bytes_remaining_ == 0) return false; |
905 | 0 | *chunk = **it; |
906 | 0 | return true; |
907 | 0 | }; |
908 | |
|
909 | 0 | Cord::ChunkIterator lhs_it = chunk_begin(); |
910 | | |
911 | | // compared_size is inside first chunk. |
912 | 0 | absl::string_view lhs_chunk = |
913 | 0 | (lhs_it.bytes_remaining_ != 0) ? *lhs_it : absl::string_view(); |
914 | 0 | assert(compared_size <= lhs_chunk.size()); |
915 | 0 | assert(compared_size <= rhs.size()); |
916 | 0 | lhs_chunk.remove_prefix(compared_size); |
917 | 0 | rhs.remove_prefix(compared_size); |
918 | 0 | size_to_compare -= compared_size; // skip already compared size. |
919 | |
|
920 | 0 | while (advance(&lhs_it, &lhs_chunk) && !rhs.empty()) { |
921 | 0 | int comparison_result = CompareChunks(&lhs_chunk, &rhs, &size_to_compare); |
922 | 0 | if (comparison_result != 0) return comparison_result; |
923 | 0 | if (size_to_compare == 0) return 0; |
924 | 0 | } |
925 | | |
926 | 0 | return static_cast<int>(rhs.empty()) - static_cast<int>(lhs_chunk.empty()); |
927 | 0 | } |
928 | | |
929 | | inline int Cord::CompareSlowPath(const Cord& rhs, size_t compared_size, |
930 | 0 | size_t size_to_compare) const { |
931 | 0 | auto advance = [](Cord::ChunkIterator* absl_nonnull it, |
932 | 0 | absl::string_view* absl_nonnull chunk) { |
933 | 0 | if (!chunk->empty()) return true; |
934 | 0 | ++*it; |
935 | 0 | if (it->bytes_remaining_ == 0) return false; |
936 | 0 | *chunk = **it; |
937 | 0 | return true; |
938 | 0 | }; |
939 | |
|
940 | 0 | Cord::ChunkIterator lhs_it = chunk_begin(); |
941 | 0 | Cord::ChunkIterator rhs_it = rhs.chunk_begin(); |
942 | | |
943 | | // compared_size is inside both first chunks. |
944 | 0 | absl::string_view lhs_chunk = |
945 | 0 | (lhs_it.bytes_remaining_ != 0) ? *lhs_it : absl::string_view(); |
946 | 0 | absl::string_view rhs_chunk = |
947 | 0 | (rhs_it.bytes_remaining_ != 0) ? *rhs_it : absl::string_view(); |
948 | 0 | assert(compared_size <= lhs_chunk.size()); |
949 | 0 | assert(compared_size <= rhs_chunk.size()); |
950 | 0 | lhs_chunk.remove_prefix(compared_size); |
951 | 0 | rhs_chunk.remove_prefix(compared_size); |
952 | 0 | size_to_compare -= compared_size; // skip already compared size. |
953 | |
|
954 | 0 | while (advance(&lhs_it, &lhs_chunk) && advance(&rhs_it, &rhs_chunk)) { |
955 | 0 | int memcmp_res = CompareChunks(&lhs_chunk, &rhs_chunk, &size_to_compare); |
956 | 0 | if (memcmp_res != 0) return memcmp_res; |
957 | 0 | if (size_to_compare == 0) return 0; |
958 | 0 | } |
959 | | |
960 | 0 | return static_cast<int>(rhs_chunk.empty()) - |
961 | 0 | static_cast<int>(lhs_chunk.empty()); |
962 | 0 | } |
963 | | |
964 | 0 | inline absl::string_view Cord::GetFirstChunk(const Cord& c) { |
965 | 0 | if (c.empty()) return {}; |
966 | 0 | return c.contents_.FindFlatStartPiece(); |
967 | 0 | } |
968 | 0 | inline absl::string_view Cord::GetFirstChunk(absl::string_view sv) { |
969 | 0 | return sv; |
970 | 0 | } |
971 | | |
972 | | // Compares up to 'size_to_compare' bytes of 'lhs' with 'rhs'. It is assumed |
973 | | // that 'size_to_compare' is greater that size of smallest of first chunks. |
974 | | template <typename ResultType, typename RHS> |
975 | | ResultType GenericCompare(const Cord& lhs, const RHS& rhs, |
976 | 0 | size_t size_to_compare) { |
977 | 0 | absl::string_view lhs_chunk = Cord::GetFirstChunk(lhs); |
978 | 0 | absl::string_view rhs_chunk = Cord::GetFirstChunk(rhs); |
979 | |
|
980 | 0 | size_t compared_size = std::min(lhs_chunk.size(), rhs_chunk.size()); |
981 | 0 | assert(size_to_compare >= compared_size); |
982 | 0 | int memcmp_res = compared_size > 0 ? ::memcmp(lhs_chunk.data(), |
983 | 0 | rhs_chunk.data(), compared_size) |
984 | 0 | : 0; |
985 | 0 | if (compared_size == size_to_compare || memcmp_res != 0) { |
986 | 0 | return ComputeCompareResult<ResultType>(memcmp_res); |
987 | 0 | } |
988 | | |
989 | 0 | return ComputeCompareResult<ResultType>( |
990 | 0 | lhs.CompareSlowPath(rhs, compared_size, size_to_compare)); |
991 | 0 | } Unexecuted instantiation: bool absl::lts_20260526::GenericCompare<bool, std::__1::basic_string_view<char, std::__1::char_traits<char> > >(absl::lts_20260526::Cord const&, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, unsigned long) Unexecuted instantiation: bool absl::lts_20260526::GenericCompare<bool, absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&, absl::lts_20260526::Cord const&, unsigned long) Unexecuted instantiation: int absl::lts_20260526::GenericCompare<int, std::__1::basic_string_view<char, std::__1::char_traits<char> > >(absl::lts_20260526::Cord const&, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&, unsigned long) Unexecuted instantiation: int absl::lts_20260526::GenericCompare<int, absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&, absl::lts_20260526::Cord const&, unsigned long) |
992 | | |
993 | 0 | bool Cord::EqualsImpl(absl::string_view rhs, size_t size_to_compare) const { |
994 | 0 | return GenericCompare<bool>(*this, rhs, size_to_compare); |
995 | 0 | } |
996 | | |
997 | 0 | bool Cord::EqualsImpl(const Cord& rhs, size_t size_to_compare) const { |
998 | 0 | return GenericCompare<bool>(*this, rhs, size_to_compare); |
999 | 0 | } |
1000 | | |
1001 | | template <typename RHS> |
1002 | 0 | inline int SharedCompareImpl(const Cord& lhs, const RHS& rhs) { |
1003 | 0 | size_t lhs_size = lhs.size(); |
1004 | 0 | size_t rhs_size = rhs.size(); |
1005 | 0 | if (lhs_size == rhs_size) { |
1006 | 0 | return GenericCompare<int>(lhs, rhs, lhs_size); |
1007 | 0 | } |
1008 | 0 | if (lhs_size < rhs_size) { |
1009 | 0 | auto data_comp_res = GenericCompare<int>(lhs, rhs, lhs_size); |
1010 | 0 | return data_comp_res == 0 ? -1 : data_comp_res; |
1011 | 0 | } |
1012 | | |
1013 | 0 | auto data_comp_res = GenericCompare<int>(lhs, rhs, rhs_size); |
1014 | 0 | return data_comp_res == 0 ? +1 : data_comp_res; |
1015 | 0 | } Unexecuted instantiation: int absl::lts_20260526::SharedCompareImpl<std::__1::basic_string_view<char, std::__1::char_traits<char> > >(absl::lts_20260526::Cord const&, std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) Unexecuted instantiation: int absl::lts_20260526::SharedCompareImpl<absl::lts_20260526::Cord>(absl::lts_20260526::Cord const&, absl::lts_20260526::Cord const&) |
1016 | | |
1017 | 0 | int Cord::Compare(absl::string_view rhs) const { |
1018 | 0 | return SharedCompareImpl(*this, rhs); |
1019 | 0 | } |
1020 | | |
1021 | 0 | int Cord::CompareImpl(const Cord& rhs) const { |
1022 | 0 | return SharedCompareImpl(*this, rhs); |
1023 | 0 | } |
1024 | | |
1025 | 0 | bool Cord::EndsWith(absl::string_view rhs) const { |
1026 | 0 | size_t my_size = size(); |
1027 | 0 | size_t rhs_size = rhs.size(); |
1028 | |
|
1029 | 0 | if (my_size < rhs_size) return false; |
1030 | | |
1031 | 0 | Cord tmp(*this); |
1032 | 0 | tmp.RemovePrefix(my_size - rhs_size); |
1033 | 0 | return tmp.EqualsImpl(rhs, rhs_size); |
1034 | 0 | } |
1035 | | |
1036 | 0 | bool Cord::EndsWith(const Cord& rhs) const { |
1037 | 0 | size_t my_size = size(); |
1038 | 0 | size_t rhs_size = rhs.size(); |
1039 | |
|
1040 | 0 | if (my_size < rhs_size) return false; |
1041 | | |
1042 | 0 | Cord tmp(*this); |
1043 | 0 | tmp.RemovePrefix(my_size - rhs_size); |
1044 | 0 | return tmp.EqualsImpl(rhs, rhs_size); |
1045 | 0 | } |
1046 | | |
1047 | | // -------------------------------------------------------------------- |
1048 | | // Misc. |
1049 | | |
1050 | 0 | Cord::operator std::string() const { |
1051 | 0 | std::string s; |
1052 | 0 | absl::CopyCordToString(*this, &s); |
1053 | 0 | return s; |
1054 | 0 | } |
1055 | | |
1056 | 0 | void CopyCordToString(const Cord& src, std::string* absl_nonnull dst) { |
1057 | 0 | if (!src.contents_.is_tree()) { |
1058 | 0 | src.contents_.CopyTo(dst); |
1059 | 0 | } else { |
1060 | 0 | StringResizeAndOverwrite(*dst, src.size(), |
1061 | 0 | [&src](char* buf, size_t buf_size) { |
1062 | 0 | src.CopyToArraySlowPath(buf); |
1063 | 0 | return buf_size; |
1064 | 0 | }); |
1065 | 0 | } |
1066 | 0 | } |
1067 | | |
1068 | 0 | void AppendCordToString(const Cord& src, std::string* absl_nonnull dst) { |
1069 | 0 | strings_internal::StringAppendAndOverwrite( |
1070 | 0 | *dst, src.size(), [&src](char* buf, size_t buf_size) { |
1071 | 0 | src.CopyToArrayImpl(buf); |
1072 | 0 | return buf_size; |
1073 | 0 | }); |
1074 | 0 | } |
1075 | | |
1076 | 0 | void Cord::CopyToArraySlowPath(char* absl_nonnull dst) const { |
1077 | 0 | assert(contents_.is_tree()); |
1078 | 0 | absl::string_view fragment; |
1079 | 0 | if (GetFlatAux(contents_.tree(), &fragment) && !fragment.empty()) { |
1080 | 0 | memcpy(dst, fragment.data(), fragment.size()); |
1081 | 0 | return; |
1082 | 0 | } |
1083 | 0 | for (absl::string_view chunk : Chunks()) { |
1084 | 0 | memcpy(dst, chunk.data(), chunk.size()); |
1085 | 0 | dst += chunk.size(); |
1086 | 0 | } |
1087 | 0 | } |
1088 | | |
1089 | 0 | size_t CopyCordToSpan(const Cord& src, absl::Span<char> dst) { |
1090 | 0 | if (src.size() <= dst.size()) { |
1091 | 0 | src.CopyToArrayImpl(dst.data()); |
1092 | 0 | return src.size(); |
1093 | 0 | } |
1094 | | |
1095 | 0 | const size_t result = dst.size(); |
1096 | 0 | for (absl::string_view chunk : src.Chunks()) { |
1097 | 0 | size_t n = std::min(chunk.size(), dst.size()); |
1098 | 0 | if (n == 0) { |
1099 | 0 | break; |
1100 | 0 | } |
1101 | 0 | memcpy(dst.data(), chunk.data(), n); |
1102 | 0 | dst.remove_prefix(n); |
1103 | 0 | } |
1104 | 0 | return result; |
1105 | 0 | } |
1106 | | |
1107 | 0 | Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) { |
1108 | | // Failure of this assertion indicates an attempt to iterate past `end()`. |
1109 | 0 | absl::base_internal::HardeningAssertGE(bytes_remaining_, n); |
1110 | 0 | Cord subcord; |
1111 | 0 | auto constexpr method = CordzUpdateTracker::kCordReader; |
1112 | |
|
1113 | 0 | if (n <= InlineRep::kMaxInline) { |
1114 | | // Range to read fits in inline data. Flatten it. |
1115 | 0 | char* data = subcord.contents_.set_data(n); |
1116 | 0 | while (n > current_chunk_.size()) { |
1117 | 0 | memcpy(data, current_chunk_.data(), current_chunk_.size()); |
1118 | 0 | data += current_chunk_.size(); |
1119 | 0 | n -= current_chunk_.size(); |
1120 | 0 | ++*this; |
1121 | 0 | } |
1122 | 0 | memcpy(data, current_chunk_.data(), n); |
1123 | 0 | if (n < current_chunk_.size()) { |
1124 | 0 | RemoveChunkPrefix(n); |
1125 | 0 | } else if (n > 0) { |
1126 | 0 | ++*this; |
1127 | 0 | } |
1128 | 0 | return subcord; |
1129 | 0 | } |
1130 | | |
1131 | 0 | if (btree_reader_) { |
1132 | 0 | size_t chunk_size = current_chunk_.size(); |
1133 | 0 | if (n <= chunk_size && n <= kMaxBytesToCopy) { |
1134 | 0 | subcord = Cord(current_chunk_.substr(0, n), method); |
1135 | 0 | if (n < chunk_size) { |
1136 | 0 | current_chunk_.remove_prefix(n); |
1137 | 0 | } else { |
1138 | 0 | current_chunk_ = btree_reader_.Next(); |
1139 | 0 | } |
1140 | 0 | } else { |
1141 | 0 | CordRep* rep; |
1142 | 0 | current_chunk_ = btree_reader_.Read(n, chunk_size, rep); |
1143 | 0 | subcord.contents_.EmplaceTree(rep, method); |
1144 | 0 | } |
1145 | 0 | bytes_remaining_ -= n; |
1146 | 0 | return subcord; |
1147 | 0 | } |
1148 | | |
1149 | | // Short circuit if reading the entire data edge. |
1150 | 0 | assert(current_leaf_ != nullptr); |
1151 | 0 | if (n == current_leaf_->length) { |
1152 | 0 | bytes_remaining_ = 0; |
1153 | 0 | current_chunk_ = {}; |
1154 | 0 | CordRep* tree = CordRep::Ref(current_leaf_); |
1155 | 0 | subcord.contents_.EmplaceTree(VerifyTree(tree), method); |
1156 | 0 | return subcord; |
1157 | 0 | } |
1158 | | |
1159 | | // From this point on, we need a partial substring node. |
1160 | | // Get pointer to the underlying flat or external data payload and |
1161 | | // compute data pointer and offset into current flat or external. |
1162 | 0 | CordRep* payload = current_leaf_->IsSubstring() |
1163 | 0 | ? current_leaf_->substring()->child |
1164 | 0 | : current_leaf_; |
1165 | 0 | const char* data = payload->IsExternal() ? payload->external()->base |
1166 | 0 | : payload->flat()->Data(); |
1167 | 0 | const size_t offset = static_cast<size_t>(current_chunk_.data() - data); |
1168 | |
|
1169 | 0 | auto* tree = CordRepSubstring::Substring(payload, offset, n); |
1170 | 0 | subcord.contents_.EmplaceTree(VerifyTree(tree), method); |
1171 | 0 | bytes_remaining_ -= n; |
1172 | 0 | current_chunk_.remove_prefix(n); |
1173 | 0 | return subcord; |
1174 | 0 | } |
1175 | | |
1176 | 0 | char Cord::operator[](size_t i) const { |
1177 | 0 | absl::base_internal::HardeningAssertLT(i, size()); |
1178 | 0 | size_t offset = i; |
1179 | 0 | const CordRep* rep = contents_.tree(); |
1180 | 0 | if (rep == nullptr) { |
1181 | 0 | return contents_.data()[i]; |
1182 | 0 | } |
1183 | 0 | rep = cord_internal::SkipCrcNode(rep); |
1184 | 0 | while (true) { |
1185 | 0 | assert(rep != nullptr); |
1186 | 0 | assert(offset < rep->length); |
1187 | 0 | if (rep->IsFlat()) { |
1188 | | // Get the "i"th character directly from the flat array. |
1189 | 0 | return rep->flat()->Data()[offset]; |
1190 | 0 | } else if (rep->IsBtree()) { |
1191 | 0 | return rep->btree()->GetCharacter(offset); |
1192 | 0 | } else if (rep->IsExternal()) { |
1193 | | // Get the "i"th character from the external array. |
1194 | 0 | return rep->external()->base[offset]; |
1195 | 0 | } else { |
1196 | | // This must be a substring a node, so bypass it to get to the child. |
1197 | 0 | assert(rep->IsSubstring()); |
1198 | 0 | offset += rep->substring()->start; |
1199 | 0 | rep = rep->substring()->child; |
1200 | 0 | } |
1201 | 0 | } |
1202 | 0 | } |
1203 | | |
1204 | | namespace { |
1205 | | |
1206 | | // Tests whether the sequence of chunks beginning at `position` starts with |
1207 | | // `needle`. |
1208 | | // |
1209 | | // REQUIRES: remaining `absl::Cord` starting at `position` is greater than or |
1210 | | // equal to `needle.size()`. |
1211 | | bool IsSubstringInCordAt(absl::Cord::CharIterator position, |
1212 | 0 | absl::string_view needle) { |
1213 | 0 | auto haystack_chunk = absl::Cord::ChunkRemaining(position); |
1214 | 0 | while (true) { |
1215 | | // Precondition is that `absl::Cord::ChunkRemaining(position)` is not |
1216 | | // empty. This assert will trigger if that is not true. |
1217 | 0 | assert(!haystack_chunk.empty()); |
1218 | 0 | auto min_length = std::min(haystack_chunk.size(), needle.size()); |
1219 | 0 | if (!absl::ConsumePrefix(&needle, haystack_chunk.substr(0, min_length))) { |
1220 | 0 | return false; |
1221 | 0 | } |
1222 | 0 | if (needle.empty()) { |
1223 | 0 | return true; |
1224 | 0 | } |
1225 | 0 | absl::Cord::Advance(&position, min_length); |
1226 | 0 | haystack_chunk = absl::Cord::ChunkRemaining(position); |
1227 | 0 | } |
1228 | 0 | } |
1229 | | |
1230 | | } // namespace |
1231 | | |
1232 | | // A few options how this could be implemented: |
1233 | | // (a) Flatten the Cord and find, i.e. |
1234 | | // haystack.Flatten().find(needle) |
1235 | | // For large 'haystack' (where Cord makes sense to be used), this copies |
1236 | | // the whole 'haystack' and can be slow. |
1237 | | // (b) Use std::search, i.e. |
1238 | | // std::search(haystack.char_begin(), haystack.char_end(), |
1239 | | // needle.begin(), needle.end()) |
1240 | | // This avoids the copy, but compares one byte at a time, and branches a |
1241 | | // lot every time it has to advance. It is also not possible to use |
1242 | | // std::search as is, because CharIterator is only an input iterator, not a |
1243 | | // forward iterator. |
1244 | | // (c) Use string_view::find in each fragment, and specifically handle fragment |
1245 | | // boundaries. |
1246 | | // |
1247 | | // This currently implements option (b). |
1248 | | absl::Cord::CharIterator absl::Cord::FindImpl(CharIterator it, |
1249 | 0 | absl::string_view needle) const { |
1250 | | // Ensure preconditions are met by callers first. |
1251 | | |
1252 | | // Needle must not be empty. |
1253 | 0 | assert(!needle.empty()); |
1254 | | // Haystack must be at least as large as needle. |
1255 | 0 | assert(it.chunk_iterator_.bytes_remaining_ >= needle.size()); |
1256 | | |
1257 | | // Cord is a sequence of chunks. To find `needle` we go chunk by chunk looking |
1258 | | // for the first char of needle, up until we have advanced `N` defined as |
1259 | | // `haystack.size() - needle.size()`. If we find the first char of needle at |
1260 | | // `P` and `P` is less than `N`, we then call `IsSubstringInCordAt` to |
1261 | | // see if this is the needle. If not, we advance to `P + 1` and try again. |
1262 | 0 | while (it.chunk_iterator_.bytes_remaining_ >= needle.size()) { |
1263 | 0 | auto haystack_chunk = Cord::ChunkRemaining(it); |
1264 | 0 | assert(!haystack_chunk.empty()); |
1265 | | // Look for the first char of `needle` in the current chunk. |
1266 | 0 | auto idx = haystack_chunk.find(needle.front()); |
1267 | 0 | if (idx == absl::string_view::npos) { |
1268 | | // No potential match in this chunk, advance past it. |
1269 | 0 | Cord::Advance(&it, haystack_chunk.size()); |
1270 | 0 | continue; |
1271 | 0 | } |
1272 | | // We found the start of a potential match in the chunk. Advance the |
1273 | | // iterator and haystack chunk to the match the position. |
1274 | 0 | Cord::Advance(&it, idx); |
1275 | | // Check if there is enough haystack remaining to actually have a match. |
1276 | 0 | if (it.chunk_iterator_.bytes_remaining_ < needle.size()) { |
1277 | 0 | break; |
1278 | 0 | } |
1279 | | // Check if this is `needle`. |
1280 | 0 | if (IsSubstringInCordAt(it, needle)) { |
1281 | 0 | return it; |
1282 | 0 | } |
1283 | | // No match, increment the iterator for the next attempt. |
1284 | 0 | Cord::Advance(&it, 1); |
1285 | 0 | } |
1286 | | // If we got here, we did not find `needle`. |
1287 | 0 | return char_end(); |
1288 | 0 | } |
1289 | | |
1290 | 0 | absl::Cord::CharIterator absl::Cord::Find(absl::string_view needle) const { |
1291 | 0 | if (needle.empty()) { |
1292 | 0 | return char_begin(); |
1293 | 0 | } |
1294 | 0 | if (needle.size() > size()) { |
1295 | 0 | return char_end(); |
1296 | 0 | } |
1297 | 0 | if (needle.size() == size()) { |
1298 | 0 | return *this == needle ? char_begin() : char_end(); |
1299 | 0 | } |
1300 | 0 | return FindImpl(char_begin(), needle); |
1301 | 0 | } |
1302 | | |
1303 | | namespace { |
1304 | | |
1305 | | // Tests whether the sequence of chunks beginning at `haystack` starts with the |
1306 | | // sequence of chunks beginning at `needle_begin` and extending to `needle_end`. |
1307 | | // |
1308 | | // REQUIRES: remaining `absl::Cord` starting at `position` is greater than or |
1309 | | // equal to `needle_end - needle_begin` and `advance`. |
1310 | | bool IsSubcordInCordAt(absl::Cord::CharIterator haystack, |
1311 | | absl::Cord::CharIterator needle_begin, |
1312 | 0 | absl::Cord::CharIterator needle_end) { |
1313 | 0 | while (needle_begin != needle_end) { |
1314 | 0 | auto haystack_chunk = absl::Cord::ChunkRemaining(haystack); |
1315 | 0 | assert(!haystack_chunk.empty()); |
1316 | 0 | auto needle_chunk = absl::Cord::ChunkRemaining(needle_begin); |
1317 | 0 | auto min_length = std::min(haystack_chunk.size(), needle_chunk.size()); |
1318 | 0 | if (haystack_chunk.substr(0, min_length) != |
1319 | 0 | needle_chunk.substr(0, min_length)) { |
1320 | 0 | return false; |
1321 | 0 | } |
1322 | 0 | absl::Cord::Advance(&haystack, min_length); |
1323 | 0 | absl::Cord::Advance(&needle_begin, min_length); |
1324 | 0 | } |
1325 | 0 | return true; |
1326 | 0 | } |
1327 | | |
1328 | | // Tests whether the sequence of chunks beginning at `position` starts with the |
1329 | | // cord `needle`. |
1330 | | // |
1331 | | // REQUIRES: remaining `absl::Cord` starting at `position` is greater than or |
1332 | | // equal to `needle.size()`. |
1333 | | bool IsSubcordInCordAt(absl::Cord::CharIterator position, |
1334 | 0 | const absl::Cord& needle) { |
1335 | 0 | return IsSubcordInCordAt(position, needle.char_begin(), needle.char_end()); |
1336 | 0 | } |
1337 | | |
1338 | | } // namespace |
1339 | | |
1340 | 0 | absl::Cord::CharIterator absl::Cord::Find(const absl::Cord& needle) const { |
1341 | 0 | if (needle.empty()) { |
1342 | 0 | return char_begin(); |
1343 | 0 | } |
1344 | 0 | const auto needle_size = needle.size(); |
1345 | 0 | if (needle_size > size()) { |
1346 | 0 | return char_end(); |
1347 | 0 | } |
1348 | 0 | if (needle_size == size()) { |
1349 | 0 | return *this == needle ? char_begin() : char_end(); |
1350 | 0 | } |
1351 | 0 | const auto needle_chunk = Cord::ChunkRemaining(needle.char_begin()); |
1352 | 0 | auto haystack_it = char_begin(); |
1353 | 0 | while (true) { |
1354 | 0 | haystack_it = FindImpl(haystack_it, needle_chunk); |
1355 | 0 | if (haystack_it == char_end() || |
1356 | 0 | haystack_it.chunk_iterator_.bytes_remaining_ < needle_size) { |
1357 | 0 | break; |
1358 | 0 | } |
1359 | | // We found the first chunk of `needle` at `haystack_it` but not the entire |
1360 | | // subcord. Advance past the first chunk and check for the remainder. |
1361 | 0 | auto haystack_advanced_it = haystack_it; |
1362 | 0 | auto needle_it = needle.char_begin(); |
1363 | 0 | Cord::Advance(&haystack_advanced_it, needle_chunk.size()); |
1364 | 0 | Cord::Advance(&needle_it, needle_chunk.size()); |
1365 | 0 | if (IsSubcordInCordAt(haystack_advanced_it, needle_it, needle.char_end())) { |
1366 | 0 | return haystack_it; |
1367 | 0 | } |
1368 | 0 | Cord::Advance(&haystack_it, 1); |
1369 | 0 | if (haystack_it.chunk_iterator_.bytes_remaining_ < needle_size) { |
1370 | 0 | break; |
1371 | 0 | } |
1372 | 0 | if (haystack_it.chunk_iterator_.bytes_remaining_ == needle_size) { |
1373 | | // Special case, if there is exactly `needle_size` bytes remaining, the |
1374 | | // subcord is either at `haystack_it` or not at all. |
1375 | 0 | if (IsSubcordInCordAt(haystack_it, needle)) { |
1376 | 0 | return haystack_it; |
1377 | 0 | } |
1378 | 0 | break; |
1379 | 0 | } |
1380 | 0 | } |
1381 | 0 | return char_end(); |
1382 | 0 | } |
1383 | | |
1384 | 0 | bool Cord::Contains(absl::string_view rhs) const { |
1385 | 0 | return rhs.empty() || Find(rhs) != char_end(); |
1386 | 0 | } |
1387 | | |
1388 | 0 | bool Cord::Contains(const absl::Cord& rhs) const { |
1389 | 0 | return rhs.empty() || Find(rhs) != char_end(); |
1390 | 0 | } |
1391 | | |
1392 | 0 | absl::string_view Cord::FlattenSlowPath() { |
1393 | 0 | assert(contents_.is_tree()); |
1394 | 0 | size_t total_size = size(); |
1395 | 0 | CordRep* new_rep; |
1396 | 0 | char* new_buffer; |
1397 | | |
1398 | | // Try to put the contents into a new flat rep. If they won't fit in the |
1399 | | // biggest possible flat node, use an external rep instead. |
1400 | 0 | if (total_size <= kMaxFlatLength) { |
1401 | 0 | new_rep = CordRepFlat::New(total_size); |
1402 | 0 | new_rep->length = total_size; |
1403 | 0 | new_buffer = new_rep->flat()->Data(); |
1404 | 0 | CopyToArraySlowPath(new_buffer); |
1405 | 0 | } else { |
1406 | 0 | new_buffer = std::allocator<char>().allocate(total_size); |
1407 | 0 | CopyToArraySlowPath(new_buffer); |
1408 | 0 | new_rep = absl::cord_internal::NewExternalRep( |
1409 | 0 | absl::string_view(new_buffer, total_size), [](absl::string_view s) { |
1410 | 0 | std::allocator<char>().deallocate(const_cast<char*>(s.data()), |
1411 | 0 | s.size()); |
1412 | 0 | }); |
1413 | 0 | } |
1414 | 0 | CordzUpdateScope scope(contents_.cordz_info(), CordzUpdateTracker::kFlatten); |
1415 | 0 | CordRep::Unref(contents_.as_tree()); |
1416 | 0 | contents_.SetTree(new_rep, scope); |
1417 | 0 | return absl::string_view(new_buffer, total_size); |
1418 | 0 | } |
1419 | | |
1420 | | /* static */ bool Cord::GetFlatAux(CordRep* absl_nonnull rep, |
1421 | 0 | absl::string_view* absl_nonnull fragment) { |
1422 | 0 | assert(rep != nullptr); |
1423 | 0 | if (rep->length == 0) { |
1424 | 0 | *fragment = absl::string_view(); |
1425 | 0 | return true; |
1426 | 0 | } |
1427 | 0 | rep = cord_internal::SkipCrcNode(rep); |
1428 | 0 | if (rep->IsFlat()) { |
1429 | 0 | *fragment = absl::string_view(rep->flat()->Data(), rep->length); |
1430 | 0 | return true; |
1431 | 0 | } else if (rep->IsExternal()) { |
1432 | 0 | *fragment = absl::string_view(rep->external()->base, rep->length); |
1433 | 0 | return true; |
1434 | 0 | } else if (rep->IsBtree()) { |
1435 | 0 | return rep->btree()->IsFlat(fragment); |
1436 | 0 | } else if (rep->IsSubstring()) { |
1437 | 0 | CordRep* child = rep->substring()->child; |
1438 | 0 | if (child->IsFlat()) { |
1439 | 0 | *fragment = absl::string_view( |
1440 | 0 | child->flat()->Data() + rep->substring()->start, rep->length); |
1441 | 0 | return true; |
1442 | 0 | } else if (child->IsExternal()) { |
1443 | 0 | *fragment = absl::string_view( |
1444 | 0 | child->external()->base + rep->substring()->start, rep->length); |
1445 | 0 | return true; |
1446 | 0 | } else if (child->IsBtree()) { |
1447 | 0 | return child->btree()->IsFlat(rep->substring()->start, rep->length, |
1448 | 0 | fragment); |
1449 | 0 | } |
1450 | 0 | } |
1451 | 0 | return false; |
1452 | 0 | } |
1453 | | |
1454 | | /* static */ void Cord::ForEachChunkAux( |
1455 | | absl::cord_internal::CordRep* absl_nonnull rep, |
1456 | 0 | absl::FunctionRef<void(absl::string_view)> callback) { |
1457 | 0 | assert(rep != nullptr); |
1458 | 0 | if (rep->length == 0) return; |
1459 | 0 | rep = cord_internal::SkipCrcNode(rep); |
1460 | |
|
1461 | 0 | if (rep->IsBtree()) { |
1462 | 0 | ChunkIterator it(rep), end; |
1463 | 0 | while (it != end) { |
1464 | 0 | callback(*it); |
1465 | 0 | ++it; |
1466 | 0 | } |
1467 | 0 | return; |
1468 | 0 | } |
1469 | | |
1470 | | // This is a leaf node, so invoke our callback. |
1471 | 0 | absl::cord_internal::CordRep* current_node = cord_internal::SkipCrcNode(rep); |
1472 | 0 | absl::string_view chunk; |
1473 | 0 | bool success = GetFlatAux(current_node, &chunk); |
1474 | 0 | assert(success); |
1475 | 0 | if (success) { |
1476 | 0 | callback(chunk); |
1477 | 0 | } |
1478 | 0 | } |
1479 | | |
1480 | | static void DumpNode(CordRep* absl_nonnull nonnull_rep, bool include_data, |
1481 | 0 | std::ostream* absl_nonnull os, int indent) { |
1482 | 0 | CordRep* rep = nonnull_rep; |
1483 | 0 | const int kIndentStep = 1; |
1484 | 0 | for (;;) { |
1485 | 0 | *os << std::setw(3) << (rep == nullptr ? 0 : rep->refcount.Get()); |
1486 | 0 | *os << " " << std::setw(7) << (rep == nullptr ? 0 : rep->length); |
1487 | 0 | *os << " ["; |
1488 | 0 | if (include_data) *os << static_cast<void*>(rep); |
1489 | 0 | *os << "]"; |
1490 | 0 | *os << " " << std::setw(indent) << ""; |
1491 | 0 | bool leaf = false; |
1492 | 0 | if (rep == nullptr) { |
1493 | 0 | *os << "NULL\n"; |
1494 | 0 | leaf = true; |
1495 | 0 | } else if (rep->IsCrc()) { |
1496 | 0 | *os << "CRC crc=" << rep->crc()->crc_cord_state.Checksum() << "\n"; |
1497 | 0 | indent += kIndentStep; |
1498 | 0 | rep = rep->crc()->child; |
1499 | 0 | } else if (rep->IsSubstring()) { |
1500 | 0 | *os << "SUBSTRING @ " << rep->substring()->start << "\n"; |
1501 | 0 | indent += kIndentStep; |
1502 | 0 | rep = rep->substring()->child; |
1503 | 0 | } else { // Leaf or ring |
1504 | 0 | leaf = true; |
1505 | 0 | if (rep->IsExternal()) { |
1506 | 0 | *os << "EXTERNAL ["; |
1507 | 0 | if (include_data) |
1508 | 0 | *os << absl::CEscape( |
1509 | 0 | absl::string_view(rep->external()->base, rep->length)); |
1510 | 0 | *os << "]\n"; |
1511 | 0 | } else if (rep->IsFlat()) { |
1512 | 0 | *os << "FLAT cap=" << rep->flat()->Capacity() << " ["; |
1513 | 0 | if (include_data) |
1514 | 0 | *os << absl::CEscape( |
1515 | 0 | absl::string_view(rep->flat()->Data(), rep->length)); |
1516 | 0 | *os << "]\n"; |
1517 | 0 | } else { |
1518 | 0 | CordRepBtree::Dump(rep, /*label=*/"", include_data, *os); |
1519 | 0 | } |
1520 | 0 | } |
1521 | 0 | if (leaf) { |
1522 | 0 | break; |
1523 | 0 | } |
1524 | 0 | } |
1525 | 0 | } |
1526 | | |
1527 | | static std::string ReportError(CordRep* absl_nonnull root, |
1528 | 0 | CordRep* absl_nonnull node) { |
1529 | 0 | std::ostringstream buf; |
1530 | 0 | buf << "Error at node " << node << " in:"; |
1531 | 0 | DumpNode(root, true, &buf); |
1532 | 0 | return buf.str(); |
1533 | 0 | } |
1534 | | |
1535 | | static bool VerifyNode(CordRep* absl_nonnull root, |
1536 | 0 | CordRep* absl_nonnull start_node) { |
1537 | 0 | absl::InlinedVector<CordRep* absl_nonnull, 2> worklist; |
1538 | 0 | worklist.push_back(start_node); |
1539 | 0 | do { |
1540 | 0 | CordRep* node = worklist.back(); |
1541 | 0 | worklist.pop_back(); |
1542 | |
|
1543 | 0 | ABSL_INTERNAL_CHECK(node != nullptr, ReportError(root, node)); |
1544 | 0 | if (node != root) { |
1545 | 0 | ABSL_INTERNAL_CHECK(node->length != 0, ReportError(root, node)); |
1546 | 0 | ABSL_INTERNAL_CHECK(!node->IsCrc(), ReportError(root, node)); |
1547 | 0 | } |
1548 | | |
1549 | 0 | if (node->IsFlat()) { |
1550 | 0 | ABSL_INTERNAL_CHECK(node->length <= node->flat()->Capacity(), |
1551 | 0 | ReportError(root, node)); |
1552 | 0 | } else if (node->IsExternal()) { |
1553 | 0 | ABSL_INTERNAL_CHECK(node->external()->base != nullptr, |
1554 | 0 | ReportError(root, node)); |
1555 | 0 | } else if (node->IsSubstring()) { |
1556 | 0 | ABSL_INTERNAL_CHECK( |
1557 | 0 | node->substring()->start < node->substring()->child->length, |
1558 | 0 | ReportError(root, node)); |
1559 | 0 | ABSL_INTERNAL_CHECK(node->substring()->start + node->length <= |
1560 | 0 | node->substring()->child->length, |
1561 | 0 | ReportError(root, node)); |
1562 | 0 | } else if (node->IsCrc()) { |
1563 | 0 | ABSL_INTERNAL_CHECK( |
1564 | 0 | node->crc()->child != nullptr || node->crc()->length == 0, |
1565 | 0 | ReportError(root, node)); |
1566 | 0 | if (node->crc()->child != nullptr) { |
1567 | 0 | ABSL_INTERNAL_CHECK(node->crc()->length == node->crc()->child->length, |
1568 | 0 | ReportError(root, node)); |
1569 | 0 | worklist.push_back(node->crc()->child); |
1570 | 0 | } |
1571 | 0 | } |
1572 | 0 | } while (!worklist.empty()); |
1573 | 0 | return true; |
1574 | 0 | } |
1575 | | |
1576 | 0 | std::ostream& operator<<(std::ostream& out, const Cord& cord) { |
1577 | 0 | for (absl::string_view chunk : cord.Chunks()) { |
1578 | 0 | out.write(chunk.data(), static_cast<std::streamsize>(chunk.size())); |
1579 | 0 | } |
1580 | 0 | return out; |
1581 | 0 | } |
1582 | | |
1583 | | namespace strings_internal { |
1584 | 0 | size_t CordTestAccess::FlatOverhead() { return cord_internal::kFlatOverhead; } |
1585 | 0 | size_t CordTestAccess::MaxFlatLength() { return cord_internal::kMaxFlatLength; } |
1586 | 0 | size_t CordTestAccess::FlatTagToLength(uint8_t tag) { |
1587 | 0 | return cord_internal::TagToLength(tag); |
1588 | 0 | } |
1589 | 0 | uint8_t CordTestAccess::LengthToTag(size_t s) { |
1590 | 0 | ABSL_INTERNAL_CHECK(s <= kMaxFlatLength, absl::StrCat("Invalid length ", s)); |
1591 | 0 | return cord_internal::AllocatedSizeToTag(s + cord_internal::kFlatOverhead); |
1592 | 0 | } |
1593 | 0 | size_t CordTestAccess::SizeofCordRepExternal() { |
1594 | 0 | return sizeof(CordRepExternal); |
1595 | 0 | } |
1596 | 0 | size_t CordTestAccess::SizeofCordRepSubstring() { |
1597 | 0 | return sizeof(CordRepSubstring); |
1598 | 0 | } |
1599 | | } // namespace strings_internal |
1600 | | ABSL_NAMESPACE_END |
1601 | | } // namespace absl |