• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #ifndef BASE_STRINGS_STRCAT_INTERNAL_H_
11 #define BASE_STRINGS_STRCAT_INTERNAL_H_
12 
13 #include <concepts>
14 #include <string>
15 
16 #include "base/containers/span.h"
17 
18 namespace base {
19 
20 namespace internal {
21 
22 // Optimized version of `std::basic_string::resize()` that skips zero
23 // initialization of appended characters. Reading from the newly allocated
24 // characters results in undefined behavior if they are not explicitly
25 // initialized afterwards. Currently proposed for standardization as
26 // std::basic_string::resize_and_overwrite: https://wg21.link/P1072R6
27 template <typename CharT>
requires(std::basic_string<CharT> & str,size_t total_size)28   requires requires(std::basic_string<CharT>& str, size_t total_size) {
29     { str.__resize_default_init(total_size) } -> std::same_as<void>;
30   }
Resize(std::basic_string<CharT> & str,size_t total_size)31 auto Resize(std::basic_string<CharT>& str, size_t total_size) {
32   str.__resize_default_init(total_size);
33 }
34 
35 // Fallback to regular std::basic_string::resize() if invoking
36 // __resize_default_init is ill-formed.
37 template <typename CharT>
Resize(std::basic_string<CharT> & str,size_t total_size)38 void Resize(std::basic_string<CharT>& str, size_t total_size) {
39   str.resize(total_size);
40 }
41 
42 // Appends `pieces` to `dest`. Instead of simply calling `dest.append()`
43 // `pieces.size()` times, this method first resizes `dest` to be of the desired
44 // size, and then appends each piece via `std::char_traits::copy`. This achieves
45 // two goals:
46 // 1) Allocating the desired size all at once avoids other allocations that
47 //    could happen if intermediate allocations did not reserve enough capacity.
48 // 2) Invoking std::char_traits::copy instead of std::basic_string::append
49 //    avoids having to write the terminating '\0' character n times.
50 template <typename CharT, typename StringT>
StrAppendT(std::basic_string<CharT> & dest,span<const StringT> pieces)51 void StrAppendT(std::basic_string<CharT>& dest, span<const StringT> pieces) {
52   const size_t initial_size = dest.size();
53   size_t total_size = initial_size;
54   for (const auto& cur : pieces)
55     total_size += cur.size();
56 
57   // Note: As opposed to `reserve()` calling `resize()` with an argument smaller
58   // than the current `capacity()` does not result in the string releasing spare
59   // capacity. Furthermore, common std::string implementations apply a geometric
60   // growth strategy if the current capacity is not sufficient for the newly
61   // added characters. Since this codepath is also triggered by `resize()`, we
62   // don't have to manage the std::string's capacity ourselves here to avoid
63   // performance hits in case `StrAppend()` gets called in a loop.
64   Resize(dest, total_size);
65   CharT* dest_char = &dest[initial_size];
66   for (const auto& cur : pieces) {
67     std::char_traits<CharT>::copy(dest_char, cur.data(), cur.size());
68     dest_char += cur.size();
69   }
70 }
71 
72 template <typename StringT>
StrCatT(span<const StringT> pieces)73 auto StrCatT(span<const StringT> pieces) {
74   std::basic_string<typename StringT::value_type> result;
75   StrAppendT(result, pieces);
76   return result;
77 }
78 
79 }  // namespace internal
80 
81 }  // namespace base
82 
83 #endif  // BASE_STRINGS_STRCAT_INTERNAL_H_
84