/src/abseil-cpp/absl/strings/str_cat.cc
Line | Count | Source |
1 | | // Copyright 2017 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "absl/strings/str_cat.h" |
16 | | |
17 | | #include <assert.h> |
18 | | |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <cstring> |
22 | | #include <initializer_list> |
23 | | #include <string> |
24 | | #include <type_traits> |
25 | | |
26 | | #include "absl/base/config.h" |
27 | | #include "absl/base/nullability.h" |
28 | | #include "absl/strings/internal/resize_uninitialized.h" |
29 | | #include "absl/strings/numbers.h" |
30 | | #include "absl/strings/string_view.h" |
31 | | |
32 | | namespace absl { |
33 | | ABSL_NAMESPACE_BEGIN |
34 | | |
35 | | |
36 | | // ---------------------------------------------------------------------- |
37 | | // StrCat() |
38 | | // This merges the given strings or integers, with no delimiter. This |
39 | | // is designed to be the fastest possible way to construct a string out |
40 | | // of a mix of raw C strings, string_views, strings, and integer values. |
41 | | // ---------------------------------------------------------------------- |
42 | | |
43 | | namespace { |
44 | | // Append is merely a version of memcpy that returns the address of the byte |
45 | | // after the area just overwritten. |
46 | 351k | absl::Nonnull<char*> Append(absl::Nonnull<char*> out, const AlphaNum& x) { |
47 | | // memcpy is allowed to overwrite arbitrary memory, so doing this after the |
48 | | // call would force an extra fetch of x.size(). |
49 | 351k | char* after = out + x.size(); |
50 | 351k | if (x.size() != 0) { |
51 | 210k | memcpy(out, x.data(), x.size()); |
52 | 210k | } |
53 | 351k | return after; |
54 | 351k | } |
55 | | |
56 | | } // namespace |
57 | | |
58 | 175k | std::string StrCat(const AlphaNum& a, const AlphaNum& b) { |
59 | 175k | std::string result; |
60 | 175k | absl::strings_internal::STLStringResizeUninitialized(&result, |
61 | 175k | a.size() + b.size()); |
62 | 175k | char* const begin = &result[0]; |
63 | 175k | char* out = begin; |
64 | 175k | out = Append(out, a); |
65 | 175k | out = Append(out, b); |
66 | 175k | assert(out == begin + result.size()); |
67 | 175k | return result; |
68 | 175k | } |
69 | | |
70 | 0 | std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c) { |
71 | 0 | std::string result; |
72 | 0 | strings_internal::STLStringResizeUninitialized( |
73 | 0 | &result, a.size() + b.size() + c.size()); |
74 | 0 | char* const begin = &result[0]; |
75 | 0 | char* out = begin; |
76 | 0 | out = Append(out, a); |
77 | 0 | out = Append(out, b); |
78 | 0 | out = Append(out, c); |
79 | 0 | assert(out == begin + result.size()); |
80 | 0 | return result; |
81 | 0 | } |
82 | | |
83 | | std::string StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c, |
84 | 0 | const AlphaNum& d) { |
85 | 0 | std::string result; |
86 | 0 | strings_internal::STLStringResizeUninitialized( |
87 | 0 | &result, a.size() + b.size() + c.size() + d.size()); |
88 | 0 | char* const begin = &result[0]; |
89 | 0 | char* out = begin; |
90 | 0 | out = Append(out, a); |
91 | 0 | out = Append(out, b); |
92 | 0 | out = Append(out, c); |
93 | 0 | out = Append(out, d); |
94 | 0 | assert(out == begin + result.size()); |
95 | 0 | return result; |
96 | 0 | } |
97 | | |
98 | | namespace strings_internal { |
99 | | |
100 | | // Do not call directly - these are not part of the public API. |
101 | | void STLStringAppendUninitializedAmortized(std::string* dest, |
102 | 0 | size_t to_append) { |
103 | 0 | strings_internal::AppendUninitializedTraits<std::string>::Append(dest, |
104 | 0 | to_append); |
105 | 0 | } |
106 | | |
107 | | template <typename Integer> |
108 | | std::enable_if_t<std::is_integral<Integer>::value, std::string> IntegerToString( |
109 | 0 | Integer i) { |
110 | 0 | std::string str; |
111 | 0 | const auto /* either bool or std::false_type */ is_negative = |
112 | 0 | absl::numbers_internal::IsNegative(i); |
113 | 0 | const uint32_t digits = absl::numbers_internal::Base10Digits( |
114 | 0 | absl::numbers_internal::UnsignedAbsoluteValue(i)); |
115 | 0 | absl::strings_internal::STLStringResizeUninitialized( |
116 | 0 | &str, digits + static_cast<uint32_t>(is_negative)); |
117 | 0 | absl::numbers_internal::FastIntToBufferBackward(i, &str[str.size()], digits); |
118 | 0 | return str; |
119 | 0 | } Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal15IntegerToStringIiEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueENS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEE4typeES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal15IntegerToStringIxEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueENS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEE4typeES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal15IntegerToStringIjEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueENS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEE4typeES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal15IntegerToStringIyEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueENS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEE4typeES5_ |
120 | | |
121 | | template <> |
122 | 0 | std::string IntegerToString(long i) { // NOLINT |
123 | 0 | if (sizeof(i) <= sizeof(int)) { |
124 | 0 | return IntegerToString(static_cast<int>(i)); |
125 | 0 | } else { |
126 | 0 | return IntegerToString(static_cast<long long>(i)); // NOLINT |
127 | 0 | } |
128 | 0 | } |
129 | | |
130 | | template <> |
131 | 0 | std::string IntegerToString(unsigned long i) { // NOLINT |
132 | 0 | if (sizeof(i) <= sizeof(unsigned int)) { |
133 | 0 | return IntegerToString(static_cast<unsigned int>(i)); |
134 | 0 | } else { |
135 | 0 | return IntegerToString(static_cast<unsigned long long>(i)); // NOLINT |
136 | 0 | } |
137 | 0 | } |
138 | | |
139 | | template <typename Float> |
140 | | std::enable_if_t<std::is_floating_point<Float>::value, std::string> |
141 | 0 | FloatToString(Float f) { |
142 | 0 | std::string result; |
143 | 0 | strings_internal::STLStringResizeUninitialized( |
144 | 0 | &result, numbers_internal::kSixDigitsToBufferSize); |
145 | 0 | char* start = &result[0]; |
146 | 0 | result.erase(numbers_internal::SixDigitsToBuffer(f, start)); |
147 | 0 | return result; |
148 | 0 | } Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal13FloatToStringIfEENSt3__19enable_ifIXsr3std17is_floating_pointIT_EE5valueENS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEE4typeES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal13FloatToStringIdEENSt3__19enable_ifIXsr3std17is_floating_pointIT_EE5valueENS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEEE4typeES5_ |
149 | | |
150 | 0 | std::string SingleArgStrCat(int x) { return IntegerToString(x); } |
151 | 0 | std::string SingleArgStrCat(unsigned int x) { return IntegerToString(x); } |
152 | | // NOLINTNEXTLINE |
153 | 0 | std::string SingleArgStrCat(long x) { return IntegerToString(x); } |
154 | | // NOLINTNEXTLINE |
155 | 0 | std::string SingleArgStrCat(unsigned long x) { return IntegerToString(x); } |
156 | | // NOLINTNEXTLINE |
157 | 0 | std::string SingleArgStrCat(long long x) { return IntegerToString(x); } |
158 | | // NOLINTNEXTLINE |
159 | 0 | std::string SingleArgStrCat(unsigned long long x) { return IntegerToString(x); } |
160 | 0 | std::string SingleArgStrCat(float x) { return FloatToString(x); } |
161 | 0 | std::string SingleArgStrCat(double x) { return FloatToString(x); } |
162 | | |
163 | | template <class Integer> |
164 | | std::enable_if_t<std::is_integral<Integer>::value, void> AppendIntegerToString( |
165 | 0 | std::string& str, Integer i) { |
166 | 0 | const auto /* either bool or std::false_type */ is_negative = |
167 | 0 | absl::numbers_internal::IsNegative(i); |
168 | 0 | const uint32_t digits = absl::numbers_internal::Base10Digits( |
169 | 0 | absl::numbers_internal::UnsignedAbsoluteValue(i)); |
170 | 0 | absl::strings_internal::STLStringAppendUninitializedAmortized( |
171 | 0 | &str, digits + static_cast<uint32_t>(is_negative)); |
172 | 0 | absl::numbers_internal::FastIntToBufferBackward(i, &str[str.size()], digits); |
173 | 0 | } Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal21AppendIntegerToStringIiEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueEvE4typeERNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal21AppendIntegerToStringIxEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueEvE4typeERNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal21AppendIntegerToStringIjEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueEvE4typeERNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEES5_ Unexecuted instantiation: _ZN4absl12lts_2024011616strings_internal21AppendIntegerToStringIyEENSt3__19enable_ifIXsr3std11is_integralIT_EE5valueEvE4typeERNS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEES5_ |
174 | | |
175 | | template <> |
176 | 0 | void AppendIntegerToString(std::string& str, long i) { // NOLINT |
177 | 0 | if (sizeof(i) <= sizeof(int)) { |
178 | 0 | return AppendIntegerToString(str, static_cast<int>(i)); |
179 | 0 | } else { |
180 | 0 | return AppendIntegerToString(str, static_cast<long long>(i)); // NOLINT |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | template <> |
185 | | void AppendIntegerToString(std::string& str, |
186 | 0 | unsigned long i) { // NOLINT |
187 | 0 | if (sizeof(i) <= sizeof(unsigned int)) { |
188 | 0 | return AppendIntegerToString(str, static_cast<unsigned int>(i)); |
189 | 0 | } else { |
190 | 0 | return AppendIntegerToString(str, |
191 | 0 | static_cast<unsigned long long>(i)); // NOLINT |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | | // `SingleArgStrAppend` overloads are defined here for the same reasons as with |
196 | | // `SingleArgStrCat` above. |
197 | 0 | void SingleArgStrAppend(std::string& str, int x) { |
198 | 0 | return AppendIntegerToString(str, x); |
199 | 0 | } |
200 | | |
201 | 0 | void SingleArgStrAppend(std::string& str, unsigned int x) { |
202 | 0 | return AppendIntegerToString(str, x); |
203 | 0 | } |
204 | | |
205 | | // NOLINTNEXTLINE |
206 | 0 | void SingleArgStrAppend(std::string& str, long x) { |
207 | 0 | return AppendIntegerToString(str, x); |
208 | 0 | } |
209 | | |
210 | | // NOLINTNEXTLINE |
211 | 0 | void SingleArgStrAppend(std::string& str, unsigned long x) { |
212 | 0 | return AppendIntegerToString(str, x); |
213 | 0 | } |
214 | | |
215 | | // NOLINTNEXTLINE |
216 | 0 | void SingleArgStrAppend(std::string& str, long long x) { |
217 | 0 | return AppendIntegerToString(str, x); |
218 | 0 | } |
219 | | |
220 | | // NOLINTNEXTLINE |
221 | 0 | void SingleArgStrAppend(std::string& str, unsigned long long x) { |
222 | 0 | return AppendIntegerToString(str, x); |
223 | 0 | } |
224 | | |
225 | 0 | std::string CatPieces(std::initializer_list<absl::string_view> pieces) { |
226 | 0 | std::string result; |
227 | 0 | size_t total_size = 0; |
228 | 0 | for (absl::string_view piece : pieces) total_size += piece.size(); |
229 | 0 | strings_internal::STLStringResizeUninitialized(&result, total_size); |
230 | |
|
231 | 0 | char* const begin = &result[0]; |
232 | 0 | char* out = begin; |
233 | 0 | for (absl::string_view piece : pieces) { |
234 | 0 | const size_t this_size = piece.size(); |
235 | 0 | if (this_size != 0) { |
236 | 0 | memcpy(out, piece.data(), this_size); |
237 | 0 | out += this_size; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | assert(out == begin + result.size()); |
241 | 0 | return result; |
242 | 0 | } |
243 | | |
244 | | // It's possible to call StrAppend with an absl::string_view that is itself a |
245 | | // fragment of the string we're appending to. However the results of this are |
246 | | // random. Therefore, check for this in debug mode. Use unsigned math so we |
247 | | // only have to do one comparison. Note, there's an exception case: appending an |
248 | | // empty string is always allowed. |
249 | | #define ASSERT_NO_OVERLAP(dest, src) \ |
250 | 0 | assert(((src).size() == 0) || \ |
251 | 0 | (uintptr_t((src).data() - (dest).data()) > uintptr_t((dest).size()))) |
252 | | |
253 | | void AppendPieces(absl::Nonnull<std::string*> dest, |
254 | 0 | std::initializer_list<absl::string_view> pieces) { |
255 | 0 | size_t old_size = dest->size(); |
256 | 0 | size_t to_append = 0; |
257 | 0 | for (absl::string_view piece : pieces) { |
258 | 0 | ASSERT_NO_OVERLAP(*dest, piece); |
259 | 0 | to_append += piece.size(); |
260 | 0 | } |
261 | 0 | strings_internal::STLStringAppendUninitializedAmortized(dest, to_append); |
262 | |
|
263 | 0 | char* const begin = &(*dest)[0]; |
264 | 0 | char* out = begin + old_size; |
265 | 0 | for (absl::string_view piece : pieces) { |
266 | 0 | const size_t this_size = piece.size(); |
267 | 0 | if (this_size != 0) { |
268 | 0 | memcpy(out, piece.data(), this_size); |
269 | 0 | out += this_size; |
270 | 0 | } |
271 | 0 | } |
272 | 0 | assert(out == begin + dest->size()); |
273 | 0 | } |
274 | | |
275 | | } // namespace strings_internal |
276 | | |
277 | 0 | void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a) { |
278 | 0 | ASSERT_NO_OVERLAP(*dest, a); |
279 | 0 | std::string::size_type old_size = dest->size(); |
280 | 0 | strings_internal::STLStringAppendUninitializedAmortized(dest, a.size()); |
281 | 0 | char* const begin = &(*dest)[0]; |
282 | 0 | char* out = begin + old_size; |
283 | 0 | out = Append(out, a); |
284 | 0 | assert(out == begin + dest->size()); |
285 | 0 | } |
286 | | |
287 | | void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a, |
288 | 0 | const AlphaNum& b) { |
289 | 0 | ASSERT_NO_OVERLAP(*dest, a); |
290 | 0 | ASSERT_NO_OVERLAP(*dest, b); |
291 | 0 | std::string::size_type old_size = dest->size(); |
292 | 0 | strings_internal::STLStringAppendUninitializedAmortized(dest, |
293 | 0 | a.size() + b.size()); |
294 | 0 | char* const begin = &(*dest)[0]; |
295 | 0 | char* out = begin + old_size; |
296 | 0 | out = Append(out, a); |
297 | 0 | out = Append(out, b); |
298 | 0 | assert(out == begin + dest->size()); |
299 | 0 | } |
300 | | |
301 | | void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a, |
302 | 0 | const AlphaNum& b, const AlphaNum& c) { |
303 | 0 | ASSERT_NO_OVERLAP(*dest, a); |
304 | 0 | ASSERT_NO_OVERLAP(*dest, b); |
305 | 0 | ASSERT_NO_OVERLAP(*dest, c); |
306 | 0 | std::string::size_type old_size = dest->size(); |
307 | 0 | strings_internal::STLStringAppendUninitializedAmortized( |
308 | 0 | dest, a.size() + b.size() + c.size()); |
309 | 0 | char* const begin = &(*dest)[0]; |
310 | 0 | char* out = begin + old_size; |
311 | 0 | out = Append(out, a); |
312 | 0 | out = Append(out, b); |
313 | 0 | out = Append(out, c); |
314 | 0 | assert(out == begin + dest->size()); |
315 | 0 | } |
316 | | |
317 | | void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a, |
318 | 0 | const AlphaNum& b, const AlphaNum& c, const AlphaNum& d) { |
319 | 0 | ASSERT_NO_OVERLAP(*dest, a); |
320 | 0 | ASSERT_NO_OVERLAP(*dest, b); |
321 | 0 | ASSERT_NO_OVERLAP(*dest, c); |
322 | 0 | ASSERT_NO_OVERLAP(*dest, d); |
323 | 0 | std::string::size_type old_size = dest->size(); |
324 | 0 | strings_internal::STLStringAppendUninitializedAmortized( |
325 | 0 | dest, a.size() + b.size() + c.size() + d.size()); |
326 | 0 | char* const begin = &(*dest)[0]; |
327 | 0 | char* out = begin + old_size; |
328 | 0 | out = Append(out, a); |
329 | 0 | out = Append(out, b); |
330 | 0 | out = Append(out, c); |
331 | 0 | out = Append(out, d); |
332 | | assert(out == begin + dest->size()); |
333 | 0 | } |
334 | | |
335 | | ABSL_NAMESPACE_END |
336 | | } // namespace absl |