str_cat.h 23 KB

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