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