xref: /aosp_15_r20/external/cronet/base/strings/strcat.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 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_H_
6 #define BASE_STRINGS_STRCAT_H_
7 
8 #include <initializer_list>
9 
10 #include "base/base_export.h"
11 #include "base/containers/span.h"
12 #include "base/strings/string_piece.h"
13 #include "build/build_config.h"
14 
15 #if BUILDFLAG(IS_WIN)
16 // Guard against conflict with Win32 API StrCat macro:
17 // check StrCat wasn't and will not be redefined.
18 #define StrCat StrCat
19 #endif
20 
21 namespace base {
22 
23 // StrCat ----------------------------------------------------------------------
24 //
25 // StrCat is a function to perform concatenation on a sequence of strings.
26 // It is preferrable to a sequence of "a + b + c" because it is both faster and
27 // generates less code.
28 //
29 //   std::string result = base::StrCat({"foo ", result, "\nfoo ", bar});
30 //
31 // To join an array of strings with a separator, see base::JoinString in
32 // base/strings/string_util.h.
33 //
34 // MORE INFO
35 //
36 // StrCat can see all arguments at once, so it can allocate one return buffer
37 // of exactly the right size and copy once, as opposed to a sequence of
38 // operator+ which generates a series of temporary strings, copying as it goes.
39 // And by using StringPiece arguments, StrCat can avoid creating temporary
40 // string objects for char* constants.
41 //
42 // ALTERNATIVES
43 //
44 // Internal Google / Abseil has a similar StrCat function. That version takes
45 // an overloaded number of arguments instead of initializer list (overflowing
46 // to initializer list for many arguments). We don't have any legacy
47 // requirements and using only initializer_list is simpler and generates
48 // roughly the same amount of code at the call sites.
49 //
50 // Abseil's StrCat also allows numbers by using an intermediate class that can
51 // be implicitly constructed from either a string or various number types. This
52 // class formats the numbers into a static buffer for increased performance,
53 // and the call sites look nice.
54 //
55 // As-written Abseil's helper class for numbers generates slightly more code
56 // than the raw StringPiece version. We can de-inline the helper class'
57 // constructors which will cause the StringPiece constructors to be de-inlined
58 // for this call and generate slightly less code. This is something we can
59 // explore more in the future.
60 
61 [[nodiscard]] BASE_EXPORT std::string StrCat(span<const StringPiece> pieces);
62 [[nodiscard]] BASE_EXPORT std::u16string StrCat(
63     span<const StringPiece16> pieces);
64 [[nodiscard]] BASE_EXPORT std::string StrCat(span<const std::string> pieces);
65 [[nodiscard]] BASE_EXPORT std::u16string StrCat(
66     span<const std::u16string> pieces);
67 
68 // Initializer list forwards to the array version.
StrCat(std::initializer_list<StringPiece> pieces)69 inline std::string StrCat(std::initializer_list<StringPiece> pieces) {
70   return StrCat(make_span(pieces));
71 }
72 
StrCat(std::initializer_list<StringPiece16> pieces)73 inline std::u16string StrCat(std::initializer_list<StringPiece16> pieces) {
74   return StrCat(make_span(pieces));
75 }
76 
77 // StrAppend -------------------------------------------------------------------
78 //
79 // Appends a sequence of strings to a destination. Prefer:
80 //   StrAppend(&foo, ...);
81 // over:
82 //   foo += StrCat(...);
83 // because it avoids a temporary string allocation and copy.
84 
85 BASE_EXPORT void StrAppend(std::string* dest, span<const StringPiece> pieces);
86 BASE_EXPORT void StrAppend(std::u16string* dest,
87                            span<const StringPiece16> pieces);
88 BASE_EXPORT void StrAppend(std::string* dest, span<const std::string> pieces);
89 BASE_EXPORT void StrAppend(std::u16string* dest,
90                            span<const std::u16string> pieces);
91 
92 // Initializer list forwards to the array version.
StrAppend(std::string * dest,std::initializer_list<StringPiece> pieces)93 inline void StrAppend(std::string* dest,
94                       std::initializer_list<StringPiece> pieces) {
95   StrAppend(dest, make_span(pieces));
96 }
97 
StrAppend(std::u16string * dest,std::initializer_list<StringPiece16> pieces)98 inline void StrAppend(std::u16string* dest,
99                       std::initializer_list<StringPiece16> pieces) {
100   StrAppend(dest, make_span(pieces));
101 }
102 
103 }  // namespace base
104 
105 #if BUILDFLAG(IS_WIN)
106 #include "base/strings/strcat_win.h"
107 #endif
108 
109 #endif  // BASE_STRINGS_STRCAT_H_
110