str_cat.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: str_cat.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This package contains functions for efficiently concatenating and appending
  21. // strings: `StrCat()` and `StrAppend()`. Most of the work within these routines
  22. // is actually handled through use of a special AlphaNum type, which was
  23. // designed to be used as a parameter type that efficiently manages conversion
  24. // to strings and avoids copies in the above operations.
  25. //
  26. // Any routine accepting either a string or a number may accept `AlphaNum`.
  27. // The basic idea is that by accepting a `const AlphaNum &` as an argument
  28. // to your function, your callers will automagically convert bools, integers,
  29. // and floating point values to strings for you.
  30. //
  31. // NOTE: Use of `AlphaNum` outside of the //absl/strings package is unsupported
  32. // except for the specific case of function parameters of type `AlphaNum` or
  33. // `const AlphaNum &`. In particular, instantiating `AlphaNum` directly as a
  34. // stack variable is not supported.
  35. //
  36. // Conversion from 8-bit values is not accepted because, if it were, then an
  37. // attempt to pass ':' instead of ":" might result in a 58 ending up in your
  38. // result.
  39. //
  40. // Bools convert to "0" or "1". Pointers to types other than `char *` are not
  41. // valid inputs. No output is generated for null `char *` pointers.
  42. //
  43. // Floating point numbers are formatted with six-digit precision, which is
  44. // the default for "std::cout <<" or printf "%g" (the same as "%.6g").
  45. //
  46. // You can convert to hexadecimal output rather than decimal output using the
  47. // `Hex` type contained here. To do so, pass `Hex(my_int)` as a parameter to
  48. // `StrCat()` or `StrAppend()`. You may specify a minimum hex field width using
  49. // a `PadSpec` enum.
  50. //
  51. // User-defined types can be formatted with the `AbslStringify()` customization
  52. // point. The API relies on detecting an overload in the user-defined type's
  53. // namespace of a free (non-member) `AbslStringify()` function as a definition
  54. // (typically declared as a friend and implemented in-line.
  55. // with the following signature:
  56. //
  57. // class MyClass { ... };
  58. //
  59. // template <typename Sink>
  60. // void AbslStringify(Sink& sink, const MyClass& value);
  61. //
  62. // An `AbslStringify()` overload for a type should only be declared in the same
  63. // file and namespace as said type.
  64. //
  65. // Note that `AbslStringify()` also supports use with `absl::StrFormat()` and
  66. // `absl::Substitute()`.
  67. //
  68. // Example:
  69. //
  70. // struct Point {
  71. // // To add formatting support to `Point`, we simply need to add a free
  72. // // (non-member) function `AbslStringify()`. This method specifies how
  73. // // Point should be printed when absl::StrCat() is called on it. You can add
  74. // // such a free function using a friend declaration within the body of the
  75. // // class. The sink parameter is a templated type to avoid requiring
  76. // // dependencies.
  77. // template <typename Sink> friend void AbslStringify(Sink&
  78. // sink, const Point& p) {
  79. // absl::Format(&sink, "(%v, %v)", p.x, p.y);
  80. // }
  81. //
  82. // int x;
  83. // int y;
  84. // };
  85. // -----------------------------------------------------------------------------
  86. #ifndef ABSL_STRINGS_STR_CAT_H_
  87. #define ABSL_STRINGS_STR_CAT_H_
  88. #include <algorithm>
  89. #include <array>
  90. #include <cassert>
  91. #include <cstddef>
  92. #include <cstdint>
  93. #include <cstring>
  94. #include <initializer_list>
  95. #include <limits>
  96. #include <string>
  97. #include <type_traits>
  98. #include <utility>
  99. #include <vector>
  100. #include "absl/base/attributes.h"
  101. #include "absl/base/config.h"
  102. #include "absl/base/nullability.h"
  103. #include "absl/base/port.h"
  104. #include "absl/meta/type_traits.h"
  105. #include "absl/strings/has_absl_stringify.h"
  106. #include "absl/strings/internal/resize_uninitialized.h"
  107. #include "absl/strings/internal/stringify_sink.h"
  108. #include "absl/strings/numbers.h"
  109. #include "absl/strings/string_view.h"
  110. #if defined(ABSL_HAVE_STD_STRING_VIEW) && !defined(ABSL_USES_STD_STRING_VIEW)
  111. #include <string_view>
  112. #endif
  113. namespace absl {
  114. ABSL_NAMESPACE_BEGIN
  115. namespace strings_internal {
  116. // AlphaNumBuffer allows a way to pass a string to StrCat without having to do
  117. // memory allocation. It is simply a pair of a fixed-size character array, and
  118. // a size. Please don't use outside of absl, yet.
  119. template <size_t max_size>
  120. struct AlphaNumBuffer {
  121. std::array<char, max_size> data;
  122. size_t size;
  123. };
  124. } // namespace strings_internal
  125. // Enum that specifies the number of significant digits to return in a `Hex` or
  126. // `Dec` conversion and fill character to use. A `kZeroPad2` value, for example,
  127. // would produce hexadecimal strings such as "0a","0f" and a 'kSpacePad5' value
  128. // would produce hexadecimal strings such as " a"," f".
  129. enum PadSpec : uint8_t {
  130. kNoPad = 1,
  131. kZeroPad2,
  132. kZeroPad3,
  133. kZeroPad4,
  134. kZeroPad5,
  135. kZeroPad6,
  136. kZeroPad7,
  137. kZeroPad8,
  138. kZeroPad9,
  139. kZeroPad10,
  140. kZeroPad11,
  141. kZeroPad12,
  142. kZeroPad13,
  143. kZeroPad14,
  144. kZeroPad15,
  145. kZeroPad16,
  146. kZeroPad17,
  147. kZeroPad18,
  148. kZeroPad19,
  149. kZeroPad20,
  150. kSpacePad2 = kZeroPad2 + 64,
  151. kSpacePad3,
  152. kSpacePad4,
  153. kSpacePad5,
  154. kSpacePad6,
  155. kSpacePad7,
  156. kSpacePad8,
  157. kSpacePad9,
  158. kSpacePad10,
  159. kSpacePad11,
  160. kSpacePad12,
  161. kSpacePad13,
  162. kSpacePad14,
  163. kSpacePad15,
  164. kSpacePad16,
  165. kSpacePad17,
  166. kSpacePad18,
  167. kSpacePad19,
  168. kSpacePad20,
  169. };
  170. // -----------------------------------------------------------------------------
  171. // Hex
  172. // -----------------------------------------------------------------------------
  173. //
  174. // `Hex` stores a set of hexadecimal string conversion parameters for use
  175. // within `AlphaNum` string conversions.
  176. struct Hex {
  177. uint64_t value;
  178. uint8_t width;
  179. char fill;
  180. template <typename Int>
  181. explicit Hex(
  182. Int v, PadSpec spec = absl::kNoPad,
  183. typename std::enable_if<sizeof(Int) == 1 &&
  184. !std::is_pointer<Int>::value>::type* = nullptr)
  185. : Hex(spec, static_cast<uint8_t>(v)) {}
  186. template <typename Int>
  187. explicit Hex(
  188. Int v, PadSpec spec = absl::kNoPad,
  189. typename std::enable_if<sizeof(Int) == 2 &&
  190. !std::is_pointer<Int>::value>::type* = nullptr)
  191. : Hex(spec, static_cast<uint16_t>(v)) {}
  192. template <typename Int>
  193. explicit Hex(
  194. Int v, PadSpec spec = absl::kNoPad,
  195. typename std::enable_if<sizeof(Int) == 4 &&
  196. !std::is_pointer<Int>::value>::type* = nullptr)
  197. : Hex(spec, static_cast<uint32_t>(v)) {}
  198. template <typename Int>
  199. explicit Hex(
  200. Int v, PadSpec spec = absl::kNoPad,
  201. typename std::enable_if<sizeof(Int) == 8 &&
  202. !std::is_pointer<Int>::value>::type* = nullptr)
  203. : Hex(spec, static_cast<uint64_t>(v)) {}
  204. template <typename Pointee>
  205. explicit Hex(absl::Nullable<Pointee*> v, PadSpec spec = absl::kNoPad)
  206. : Hex(spec, reinterpret_cast<uintptr_t>(v)) {}
  207. template <typename S>
  208. friend void AbslStringify(S& sink, Hex hex) {
  209. static_assert(
  210. numbers_internal::kFastToBufferSize >= 32,
  211. "This function only works when output buffer >= 32 bytes long");
  212. char buffer[numbers_internal::kFastToBufferSize];
  213. char* const end = &buffer[numbers_internal::kFastToBufferSize];
  214. auto real_width =
  215. absl::numbers_internal::FastHexToBufferZeroPad16(hex.value, end - 16);
  216. if (real_width >= hex.width) {
  217. sink.Append(absl::string_view(end - real_width, real_width));
  218. } else {
  219. // Pad first 16 chars because FastHexToBufferZeroPad16 pads only to 16 and
  220. // max pad width can be up to 20.
  221. std::memset(end - 32, hex.fill, 16);
  222. // Patch up everything else up to the real_width.
  223. std::memset(end - real_width - 16, hex.fill, 16);
  224. sink.Append(absl::string_view(end - hex.width, hex.width));
  225. }
  226. }
  227. private:
  228. Hex(PadSpec spec, uint64_t v)
  229. : value(v),
  230. width(spec == absl::kNoPad
  231. ? 1
  232. : spec >= absl::kSpacePad2 ? spec - absl::kSpacePad2 + 2
  233. : spec - absl::kZeroPad2 + 2),
  234. fill(spec >= absl::kSpacePad2 ? ' ' : '0') {}
  235. };
  236. // -----------------------------------------------------------------------------
  237. // Dec
  238. // -----------------------------------------------------------------------------
  239. //
  240. // `Dec` stores a set of decimal string conversion parameters for use
  241. // within `AlphaNum` string conversions. Dec is slower than the default
  242. // integer conversion, so use it only if you need padding.
  243. struct Dec {
  244. uint64_t value;
  245. uint8_t width;
  246. char fill;
  247. bool neg;
  248. template <typename Int>
  249. explicit Dec(Int v, PadSpec spec = absl::kNoPad,
  250. typename std::enable_if<(sizeof(Int) <= 8)>::type* = nullptr)
  251. : value(v >= 0 ? static_cast<uint64_t>(v)
  252. : uint64_t{0} - static_cast<uint64_t>(v)),
  253. width(spec == absl::kNoPad ? 1
  254. : spec >= absl::kSpacePad2 ? spec - absl::kSpacePad2 + 2
  255. : spec - absl::kZeroPad2 + 2),
  256. fill(spec >= absl::kSpacePad2 ? ' ' : '0'),
  257. neg(v < 0) {}
  258. template <typename S>
  259. friend void AbslStringify(S& sink, Dec dec) {
  260. assert(dec.width <= numbers_internal::kFastToBufferSize);
  261. char buffer[numbers_internal::kFastToBufferSize];
  262. char* const end = &buffer[numbers_internal::kFastToBufferSize];
  263. char* const minfill = end - dec.width;
  264. char* writer = end;
  265. uint64_t val = dec.value;
  266. while (val > 9) {
  267. *--writer = '0' + (val % 10);
  268. val /= 10;
  269. }
  270. *--writer = '0' + static_cast<char>(val);
  271. if (dec.neg) *--writer = '-';
  272. ptrdiff_t fillers = writer - minfill;
  273. if (fillers > 0) {
  274. // Tricky: if the fill character is ' ', then it's <fill><+/-><digits>
  275. // But...: if the fill character is '0', then it's <+/-><fill><digits>
  276. bool add_sign_again = false;
  277. if (dec.neg && dec.fill == '0') { // If filling with '0',
  278. ++writer; // ignore the sign we just added
  279. add_sign_again = true; // and re-add the sign later.
  280. }
  281. writer -= fillers;
  282. std::fill_n(writer, fillers, dec.fill);
  283. if (add_sign_again) *--writer = '-';
  284. }
  285. sink.Append(absl::string_view(writer, static_cast<size_t>(end - writer)));
  286. }
  287. };
  288. // -----------------------------------------------------------------------------
  289. // AlphaNum
  290. // -----------------------------------------------------------------------------
  291. //
  292. // The `AlphaNum` class acts as the main parameter type for `StrCat()` and
  293. // `StrAppend()`, providing efficient conversion of numeric, boolean, decimal,
  294. // and hexadecimal values (through the `Dec` and `Hex` types) into strings.
  295. // `AlphaNum` should only be used as a function parameter. Do not instantiate
  296. // `AlphaNum` directly as a stack variable.
  297. class AlphaNum {
  298. public:
  299. // No bool ctor -- bools convert to an integral type.
  300. // A bool ctor would also convert incoming pointers (bletch).
  301. // Prevent brace initialization
  302. template <typename T>
  303. AlphaNum(std::initializer_list<T>) = delete; // NOLINT(runtime/explicit)
  304. AlphaNum(int x) // NOLINT(runtime/explicit)
  305. : piece_(digits_, static_cast<size_t>(
  306. numbers_internal::FastIntToBuffer(x, digits_) -
  307. &digits_[0])) {}
  308. AlphaNum(unsigned int x) // NOLINT(runtime/explicit)
  309. : piece_(digits_, static_cast<size_t>(
  310. numbers_internal::FastIntToBuffer(x, digits_) -
  311. &digits_[0])) {}
  312. AlphaNum(long x) // NOLINT(*)
  313. : piece_(digits_, static_cast<size_t>(
  314. numbers_internal::FastIntToBuffer(x, digits_) -
  315. &digits_[0])) {}
  316. AlphaNum(unsigned long x) // NOLINT(*)
  317. : piece_(digits_, static_cast<size_t>(
  318. numbers_internal::FastIntToBuffer(x, digits_) -
  319. &digits_[0])) {}
  320. AlphaNum(long long x) // NOLINT(*)
  321. : piece_(digits_, static_cast<size_t>(
  322. numbers_internal::FastIntToBuffer(x, digits_) -
  323. &digits_[0])) {}
  324. AlphaNum(unsigned long long x) // NOLINT(*)
  325. : piece_(digits_, static_cast<size_t>(
  326. numbers_internal::FastIntToBuffer(x, digits_) -
  327. &digits_[0])) {}
  328. AlphaNum(float f) // NOLINT(runtime/explicit)
  329. : piece_(digits_, numbers_internal::SixDigitsToBuffer(f, digits_)) {}
  330. AlphaNum(double f) // NOLINT(runtime/explicit)
  331. : piece_(digits_, numbers_internal::SixDigitsToBuffer(f, digits_)) {}
  332. template <size_t size>
  333. AlphaNum( // NOLINT(runtime/explicit)
  334. const strings_internal::AlphaNumBuffer<size>& buf
  335. ABSL_ATTRIBUTE_LIFETIME_BOUND)
  336. : piece_(&buf.data[0], buf.size) {}
  337. AlphaNum(absl::Nullable<const char*> c_str // NOLINT(runtime/explicit)
  338. ABSL_ATTRIBUTE_LIFETIME_BOUND)
  339. : piece_(NullSafeStringView(c_str)) {}
  340. AlphaNum(absl::string_view pc // NOLINT(runtime/explicit)
  341. ABSL_ATTRIBUTE_LIFETIME_BOUND)
  342. : piece_(pc) {}
  343. #if defined(ABSL_HAVE_STD_STRING_VIEW) && !defined(ABSL_USES_STD_STRING_VIEW)
  344. AlphaNum(std::string_view pc // NOLINT(runtime/explicit)
  345. ABSL_ATTRIBUTE_LIFETIME_BOUND)
  346. : piece_(pc.data(), pc.size()) {}
  347. #endif // !ABSL_USES_STD_STRING_VIEW
  348. template <typename T, typename = typename std::enable_if<
  349. HasAbslStringify<T>::value>::type>
  350. AlphaNum( // NOLINT(runtime/explicit)
  351. const T& v ABSL_ATTRIBUTE_LIFETIME_BOUND,
  352. strings_internal::StringifySink&& sink ABSL_ATTRIBUTE_LIFETIME_BOUND = {})
  353. : piece_(strings_internal::ExtractStringification(sink, v)) {}
  354. template <typename Allocator>
  355. AlphaNum( // NOLINT(runtime/explicit)
  356. const std::basic_string<char, std::char_traits<char>, Allocator>& str
  357. ABSL_ATTRIBUTE_LIFETIME_BOUND)
  358. : piece_(str) {}
  359. // Use string literals ":" instead of character literals ':'.
  360. AlphaNum(char c) = delete; // NOLINT(runtime/explicit)
  361. AlphaNum(const AlphaNum&) = delete;
  362. AlphaNum& operator=(const AlphaNum&) = delete;
  363. absl::string_view::size_type size() const { return piece_.size(); }
  364. absl::Nullable<const char*> data() const { return piece_.data(); }
  365. absl::string_view Piece() const { return piece_; }
  366. // Match unscoped enums. Use integral promotion so that a `char`-backed
  367. // enum becomes a wider integral type AlphaNum will accept.
  368. template <typename T,
  369. typename = typename std::enable_if<
  370. std::is_enum<T>{} && std::is_convertible<T, int>{} &&
  371. !HasAbslStringify<T>::value>::type>
  372. AlphaNum(T e) // NOLINT(runtime/explicit)
  373. : AlphaNum(+e) {}
  374. // This overload matches scoped enums. We must explicitly cast to the
  375. // underlying type, but use integral promotion for the same reason as above.
  376. template <typename T,
  377. typename std::enable_if<std::is_enum<T>{} &&
  378. !std::is_convertible<T, int>{} &&
  379. !HasAbslStringify<T>::value,
  380. char*>::type = nullptr>
  381. AlphaNum(T e) // NOLINT(runtime/explicit)
  382. : AlphaNum(+static_cast<typename std::underlying_type<T>::type>(e)) {}
  383. // vector<bool>::reference and const_reference require special help to
  384. // convert to `AlphaNum` because it requires two user defined conversions.
  385. template <
  386. typename T,
  387. typename std::enable_if<
  388. std::is_class<T>::value &&
  389. (std::is_same<T, std::vector<bool>::reference>::value ||
  390. std::is_same<T, std::vector<bool>::const_reference>::value)>::type* =
  391. nullptr>
  392. AlphaNum(T e) : AlphaNum(static_cast<bool>(e)) {} // NOLINT(runtime/explicit)
  393. private:
  394. absl::string_view piece_;
  395. char digits_[numbers_internal::kFastToBufferSize];
  396. };
  397. // -----------------------------------------------------------------------------
  398. // StrCat()
  399. // -----------------------------------------------------------------------------
  400. //
  401. // Merges given strings or numbers, using no delimiter(s), returning the merged
  402. // result as a string.
  403. //
  404. // `StrCat()` is designed to be the fastest possible way to construct a string
  405. // out of a mix of raw C strings, string_views, strings, bool values,
  406. // and numeric values.
  407. //
  408. // Don't use `StrCat()` for user-visible strings. The localization process
  409. // works poorly on strings built up out of fragments.
  410. //
  411. // For clarity and performance, don't use `StrCat()` when appending to a
  412. // string. Use `StrAppend()` instead. In particular, avoid using any of these
  413. // (anti-)patterns:
  414. //
  415. // str.append(StrCat(...))
  416. // str += StrCat(...)
  417. // str = StrCat(str, ...)
  418. //
  419. // The last case is the worst, with a potential to change a loop
  420. // from a linear time operation with O(1) dynamic allocations into a
  421. // quadratic time operation with O(n) dynamic allocations.
  422. //
  423. // See `StrAppend()` below for more information.
  424. namespace strings_internal {
  425. // Do not call directly - this is not part of the public API.
  426. std::string CatPieces(std::initializer_list<absl::string_view> pieces);
  427. void AppendPieces(absl::Nonnull<std::string*> dest,
  428. std::initializer_list<absl::string_view> pieces);
  429. template <typename Integer>
  430. std::string IntegerToString(Integer i) {
  431. // Any integer (signed/unsigned) up to 64 bits can be formatted into a buffer
  432. // with 22 bytes (including NULL at the end).
  433. constexpr size_t kMaxDigits10 = 22;
  434. std::string result;
  435. strings_internal::STLStringResizeUninitialized(&result, kMaxDigits10);
  436. char* start = &result[0];
  437. // note: this can be optimized to not write last zero.
  438. char* end = numbers_internal::FastIntToBuffer(i, start);
  439. auto size = static_cast<size_t>(end - start);
  440. assert((size < result.size()) &&
  441. "StrCat(Integer) does not fit into kMaxDigits10");
  442. result.erase(size);
  443. return result;
  444. }
  445. template <typename Float>
  446. std::string FloatToString(Float f) {
  447. std::string result;
  448. strings_internal::STLStringResizeUninitialized(
  449. &result, numbers_internal::kSixDigitsToBufferSize);
  450. char* start = &result[0];
  451. result.erase(numbers_internal::SixDigitsToBuffer(f, start));
  452. return result;
  453. }
  454. // `SingleArgStrCat` overloads take built-in `int`, `long` and `long long` types
  455. // (signed / unsigned) to avoid ambiguity on the call side. If we used int32_t
  456. // and int64_t, then at least one of the three (`int` / `long` / `long long`)
  457. // would have been ambiguous when passed to `SingleArgStrCat`.
  458. inline std::string SingleArgStrCat(int x) { return IntegerToString(x); }
  459. inline std::string SingleArgStrCat(unsigned int x) {
  460. return IntegerToString(x);
  461. }
  462. // NOLINTNEXTLINE
  463. inline std::string SingleArgStrCat(long x) { return IntegerToString(x); }
  464. // NOLINTNEXTLINE
  465. inline std::string SingleArgStrCat(unsigned long x) {
  466. return IntegerToString(x);
  467. }
  468. // NOLINTNEXTLINE
  469. inline std::string SingleArgStrCat(long long x) { return IntegerToString(x); }
  470. // NOLINTNEXTLINE
  471. inline std::string SingleArgStrCat(unsigned long long x) {
  472. return IntegerToString(x);
  473. }
  474. inline std::string SingleArgStrCat(float x) { return FloatToString(x); }
  475. inline std::string SingleArgStrCat(double x) { return FloatToString(x); }
  476. // As of September 2023, the SingleArgStrCat() optimization is only enabled for
  477. // libc++. The reasons for this are:
  478. // 1) The SSO size for libc++ is 23, while libstdc++ and MSSTL have an SSO size
  479. // of 15. Since IntegerToString unconditionally resizes the string to 22 bytes,
  480. // this causes both libstdc++ and MSSTL to allocate.
  481. // 2) strings_internal::STLStringResizeUninitialized() only has an
  482. // implementation that avoids initialization when using libc++. This isn't as
  483. // relevant as (1), and the cost should be benchmarked if (1) ever changes on
  484. // libstc++ or MSSTL.
  485. #ifdef _LIBCPP_VERSION
  486. #define ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE true
  487. #else
  488. #define ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE false
  489. #endif
  490. template <typename T, typename = std::enable_if_t<
  491. ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE &&
  492. std::is_arithmetic<T>{} && !std::is_same<T, char>{}>>
  493. using EnableIfFastCase = T;
  494. #undef ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE
  495. } // namespace strings_internal
  496. ABSL_MUST_USE_RESULT inline std::string StrCat() { return std::string(); }
  497. template <typename T>
  498. ABSL_MUST_USE_RESULT inline std::string StrCat(
  499. strings_internal::EnableIfFastCase<T> a) {
  500. return strings_internal::SingleArgStrCat(a);
  501. }
  502. ABSL_MUST_USE_RESULT inline std::string StrCat(const AlphaNum& a) {
  503. return std::string(a.data(), a.size());
  504. }
  505. ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b);
  506. ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
  507. const AlphaNum& c);
  508. ABSL_MUST_USE_RESULT std::string StrCat(const AlphaNum& a, const AlphaNum& b,
  509. const AlphaNum& c, const AlphaNum& d);
  510. // Support 5 or more arguments
  511. template <typename... AV>
  512. ABSL_MUST_USE_RESULT inline std::string StrCat(
  513. const AlphaNum& a, const AlphaNum& b, const AlphaNum& c, const AlphaNum& d,
  514. const AlphaNum& e, const AV&... args) {
  515. return strings_internal::CatPieces(
  516. {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(),
  517. static_cast<const AlphaNum&>(args).Piece()...});
  518. }
  519. // -----------------------------------------------------------------------------
  520. // StrAppend()
  521. // -----------------------------------------------------------------------------
  522. //
  523. // Appends a string or set of strings to an existing string, in a similar
  524. // fashion to `StrCat()`.
  525. //
  526. // WARNING: `StrAppend(&str, a, b, c, ...)` requires that none of the
  527. // a, b, c, parameters be a reference into str. For speed, `StrAppend()` does
  528. // not try to check each of its input arguments to be sure that they are not
  529. // a subset of the string being appended to. That is, while this will work:
  530. //
  531. // std::string s = "foo";
  532. // s += s;
  533. //
  534. // This output is undefined:
  535. //
  536. // std::string s = "foo";
  537. // StrAppend(&s, s);
  538. //
  539. // This output is undefined as well, since `absl::string_view` does not own its
  540. // data:
  541. //
  542. // std::string s = "foobar";
  543. // absl::string_view p = s;
  544. // StrAppend(&s, p);
  545. inline void StrAppend(absl::Nonnull<std::string*>) {}
  546. void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a);
  547. void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a,
  548. const AlphaNum& b);
  549. void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a,
  550. const AlphaNum& b, const AlphaNum& c);
  551. void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a,
  552. const AlphaNum& b, const AlphaNum& c, const AlphaNum& d);
  553. // Support 5 or more arguments
  554. template <typename... AV>
  555. inline void StrAppend(absl::Nonnull<std::string*> dest, const AlphaNum& a,
  556. const AlphaNum& b, const AlphaNum& c, const AlphaNum& d,
  557. const AlphaNum& e, const AV&... args) {
  558. strings_internal::AppendPieces(
  559. dest, {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(),
  560. static_cast<const AlphaNum&>(args).Piece()...});
  561. }
  562. // Helper function for the future StrCat default floating-point format, %.6g
  563. // This is fast.
  564. inline strings_internal::AlphaNumBuffer<
  565. numbers_internal::kSixDigitsToBufferSize>
  566. SixDigits(double d) {
  567. strings_internal::AlphaNumBuffer<numbers_internal::kSixDigitsToBufferSize>
  568. result;
  569. result.size = numbers_internal::SixDigitsToBuffer(d, &result.data[0]);
  570. return result;
  571. }
  572. ABSL_NAMESPACE_END
  573. } // namespace absl
  574. #endif // ABSL_STRINGS_STR_CAT_H_