From c30a6232df03e1efbd9f3b226777b07e087a1122 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Mon, 12 Oct 2020 14:27:29 +0200 Subject: BASELINE: Update Chromium to 85.0.4183.140 Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057 Reviewed-by: Allan Sandfeld Jensen --- chromium/base/strings/strcat_internal.h | 60 +++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 chromium/base/strings/strcat_internal.h (limited to 'chromium/base/strings/strcat_internal.h') diff --git a/chromium/base/strings/strcat_internal.h b/chromium/base/strings/strcat_internal.h new file mode 100644 index 00000000000..f5e52f08667 --- /dev/null +++ b/chromium/base/strings/strcat_internal.h @@ -0,0 +1,60 @@ +// Copyright 2020 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_STRINGS_STRCAT_INTERNAL_H_ +#define BASE_STRINGS_STRCAT_INTERNAL_H_ + +#include + +#include "base/containers/span.h" + +namespace base { + +namespace internal { + +// Reserves an additional amount of capacity in the given string, growing by at +// least 2x if necessary. Used by StrAppendT(). +// +// The "at least 2x" growing rule duplicates the exponential growth of +// std::string. The problem is that most implementations of reserve() will grow +// exactly to the requested amount instead of exponentially growing like would +// happen when appending normally. If we didn't do this, an append after the +// call to StrAppend() would definitely cause a reallocation, and loops with +// StrAppend() calls would have O(n^2) complexity to execute. Instead, we want +// StrAppend() to have the same semantics as std::string::append(). +template +void ReserveAdditionalIfNeeded(String* str, + typename String::size_type additional) { + const size_t required = str->size() + additional; + // Check whether we need to reserve additional capacity at all. + if (required <= str->capacity()) + return; + + str->reserve(std::max(required, str->capacity() * 2)); +} + +template +void StrAppendT(DestString* dest, span pieces) { + size_t additional_size = 0; + for (const auto& cur : pieces) + additional_size += cur.size(); + ReserveAdditionalIfNeeded(dest, additional_size); + + for (const auto& cur : pieces) + dest->append(cur.data(), cur.size()); +} + +template +auto StrCatT(span pieces) { + std::basic_string + result; + StrAppendT(&result, pieces); + return result; +} + +} // namespace internal + +} // namespace base + +#endif // BASE_STRINGS_STRCAT_INTERNAL_H_ -- cgit v1.2.1