Cleanup: implement PrependArray and PrependPrecise in terms of InlineData

This removes layout specific details from InlineData from cord.cc, making future platform specific internal layout changes easier to land.

PiperOrigin-RevId: 477870559
Change-Id: I26e428ef280d593ad321cf7875e05adcb1cb6438
pull/1284/head
Martijn Vels 2 years ago committed by Copybara-Service
parent 92bc0b6b68
commit 516c626692
  1. 22
      absl/strings/cord.cc

@ -610,11 +610,11 @@ void Cord::PrependArray(absl::string_view src, MethodIdentifier method) {
size_t cur_size = contents_.inline_size();
if (cur_size + src.size() <= InlineRep::kMaxInline) {
// Use embedded storage.
char data[InlineRep::kMaxInline + 1] = {0};
memcpy(data, src.data(), src.size());
memcpy(data + src.size(), contents_.data(), cur_size);
memcpy(contents_.data_.as_chars(), data, InlineRep::kMaxInline + 1);
contents_.set_inline_size(cur_size + src.size());
InlineData data;
memcpy(data.as_chars(), src.data(), src.size());
memcpy(data.as_chars() + src.size(), contents_.data(), cur_size);
data.set_inline_size(cur_size + src.size());
contents_.data_ = data;
return;
}
}
@ -638,12 +638,12 @@ void Cord::PrependPrecise(absl::string_view src, MethodIdentifier method) {
assert(!src.empty());
assert(src.size() <= cord_internal::kMaxFlatLength);
if (contents_.remaining_inline_capacity() >= src.size()) {
const size_t inline_length = contents_.inline_size();
char data[InlineRep::kMaxInline + 1] = {0};
memcpy(data, src.data(), src.size());
memcpy(data + src.size(), contents_.data(), inline_length);
memcpy(contents_.data_.as_chars(), data, InlineRep::kMaxInline + 1);
contents_.set_inline_size(inline_length + src.size());
const size_t cur_size = contents_.inline_size();
InlineData data;
memcpy(data.as_chars(), src.data(), src.size());
memcpy(data.as_chars() + src.size(), contents_.data(), cur_size);
data.set_inline_size(cur_size + src.size());
contents_.data_ = data;
} else {
contents_.PrependTree(CordRepFlat::Create(src), method);
}

Loading…
Cancel
Save