1 // Copyright 2017 The Chromium Authors. All rights reserved.
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/compiler_specific.h"
12 #include "base/containers/span.h"
13 #include "base/strings/string_piece.h"
14 #include "build/build_config.h"
15
16 #if defined(OS_WIN)
17 // To resolve a conflict with Win32 API StrCat macro.
18 #include "base/win/windows_types.h"
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 BASE_EXPORT std::string StrCat(span<const StringPiece> pieces);
62 BASE_EXPORT string16 StrCat(span<const StringPiece16> pieces);
63 BASE_EXPORT std::string StrCat(span<const std::string> pieces);
64 BASE_EXPORT string16 StrCat(span<const string16> pieces);
65
66 // Initializer list forwards to the array version.
StrCat(std::initializer_list<StringPiece> pieces)67 inline std::string StrCat(std::initializer_list<StringPiece> pieces) {
68 return StrCat(make_span(pieces.begin(), pieces.size()));
69 }
StrCat(std::initializer_list<StringPiece16> pieces)70 inline string16 StrCat(std::initializer_list<StringPiece16> pieces) {
71 return StrCat(make_span(pieces.begin(), pieces.size()));
72 }
73
74 // StrAppend -------------------------------------------------------------------
75 //
76 // Appends a sequence of strings to a destination. Prefer:
77 // StrAppend(&foo, ...);
78 // over:
79 // foo += StrCat(...);
80 // because it avoids a temporary string allocation and copy.
81
82 BASE_EXPORT void StrAppend(std::string* dest, span<const StringPiece> pieces);
83 BASE_EXPORT void StrAppend(string16* dest, span<const StringPiece16> pieces);
84 BASE_EXPORT void StrAppend(std::string* dest, span<const std::string> pieces);
85 BASE_EXPORT void StrAppend(string16* dest, span<const string16> pieces);
86
87 // Initializer list forwards to the array version.
StrAppend(std::string * dest,std::initializer_list<StringPiece> pieces)88 inline void StrAppend(std::string* dest,
89 std::initializer_list<StringPiece> pieces) {
90 return StrAppend(dest, make_span(pieces.begin(), pieces.size()));
91 }
StrAppend(string16 * dest,std::initializer_list<StringPiece16> pieces)92 inline void StrAppend(string16* dest,
93 std::initializer_list<StringPiece16> pieces) {
94 return StrAppend(dest, make_span(pieces.begin(), pieces.size()));
95 }
96
97 } // namespace base
98
99 #endif // BASE_STRINGS_STRCAT_H_
100