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