str_cat.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 //y_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 `y_absl::StrFormat()` and
  66. // `y_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 y_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. // y_absl::Format(&sink, "(%v, %v)", p.x, p.y);
  80. // }
  81. //
  82. // int x;
  83. // int y;
  84. // };
  85. // -----------------------------------------------------------------------------
  86. #ifndef Y_ABSL_STRINGS_STR_CAT_H_
  87. #define Y_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 <util/generic/string.h>
  97. #include <type_traits>
  98. #include <utility>
  99. #include <vector>
  100. #include "y_absl/base/attributes.h"
  101. #include "y_absl/base/nullability.h"
  102. #include "y_absl/base/port.h"
  103. #include "y_absl/meta/type_traits.h"
  104. #include "y_absl/strings/has_absl_stringify.h"
  105. #include "y_absl/strings/internal/resize_uninitialized.h"
  106. #include "y_absl/strings/internal/stringify_sink.h"
  107. #include "y_absl/strings/numbers.h"
  108. #include "y_absl/strings/string_view.h"
  109. namespace y_absl {
  110. Y_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 y_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 = y_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 = y_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 = y_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 = y_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(y_absl::Nullable<Pointee*> v, PadSpec spec = y_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. y_absl::numbers_internal::FastHexToBufferZeroPad16(hex.value, end - 16);
  212. if (real_width >= hex.width) {
  213. sink.Append(y_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(y_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 == y_absl::kNoPad
  227. ? 1
  228. : spec >= y_absl::kSpacePad2 ? spec - y_absl::kSpacePad2 + 2
  229. : spec - y_absl::kZeroPad2 + 2),
  230. fill(spec >= y_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 = y_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 == y_absl::kNoPad ? 1
  250. : spec >= y_absl::kSpacePad2 ? spec - y_absl::kSpacePad2 + 2
  251. : spec - y_absl::kZeroPad2 + 2),
  252. fill(spec >= y_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(y_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. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND)
  332. : piece_(&buf.data[0], buf.size) {}
  333. AlphaNum(y_absl::Nullable<const char*> c_str // NOLINT(runtime/explicit)
  334. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND)
  335. : piece_(NullSafeStringView(c_str)) {}
  336. AlphaNum(y_absl::string_view pc // NOLINT(runtime/explicit)
  337. Y_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 Y_ABSL_ATTRIBUTE_LIFETIME_BOUND,
  343. strings_internal::StringifySink&& sink Y_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. Y_ABSL_ATTRIBUTE_LIFETIME_BOUND)
  349. : piece_(str) {}
  350. AlphaNum(const TString& str)
  351. : piece_(str.data(), str.size()) {}
  352. // Use string literals ":" instead of character literals ':'.
  353. AlphaNum(char c) = delete; // NOLINT(runtime/explicit)
  354. AlphaNum(const AlphaNum&) = delete;
  355. AlphaNum& operator=(const AlphaNum&) = delete;
  356. y_absl::string_view::size_type size() const { return piece_.size(); }
  357. y_absl::Nullable<const char*> data() const { return piece_.data(); }
  358. y_absl::string_view Piece() const { return piece_; }
  359. // Match unscoped enums. Use integral promotion so that a `char`-backed
  360. // enum becomes a wider integral type AlphaNum will accept.
  361. template <typename T,
  362. typename = typename std::enable_if<
  363. std::is_enum<T>{} && std::is_convertible<T, int>{} &&
  364. !HasAbslStringify<T>::value>::type>
  365. AlphaNum(T e) // NOLINT(runtime/explicit)
  366. : AlphaNum(+e) {}
  367. // This overload matches scoped enums. We must explicitly cast to the
  368. // underlying type, but use integral promotion for the same reason as above.
  369. template <typename T,
  370. typename std::enable_if<std::is_enum<T>{} &&
  371. !std::is_convertible<T, int>{} &&
  372. !HasAbslStringify<T>::value,
  373. char*>::type = nullptr>
  374. AlphaNum(T e) // NOLINT(runtime/explicit)
  375. : AlphaNum(+static_cast<typename std::underlying_type<T>::type>(e)) {}
  376. // vector<bool>::reference and const_reference require special help to
  377. // convert to `AlphaNum` because it requires two user defined conversions.
  378. template <
  379. typename T,
  380. typename std::enable_if<
  381. std::is_class<T>::value &&
  382. (std::is_same<T, std::vector<bool>::reference>::value ||
  383. std::is_same<T, std::vector<bool>::const_reference>::value)>::type* =
  384. nullptr>
  385. AlphaNum(T e) : AlphaNum(static_cast<bool>(e)) {} // NOLINT(runtime/explicit)
  386. private:
  387. y_absl::string_view piece_;
  388. char digits_[numbers_internal::kFastToBufferSize];
  389. };
  390. // -----------------------------------------------------------------------------
  391. // StrCat()
  392. // -----------------------------------------------------------------------------
  393. //
  394. // Merges given strings or numbers, using no delimiter(s), returning the merged
  395. // result as a string.
  396. //
  397. // `StrCat()` is designed to be the fastest possible way to construct a string
  398. // out of a mix of raw C strings, string_views, strings, bool values,
  399. // and numeric values.
  400. //
  401. // Don't use `StrCat()` for user-visible strings. The localization process
  402. // works poorly on strings built up out of fragments.
  403. //
  404. // For clarity and performance, don't use `StrCat()` when appending to a
  405. // string. Use `StrAppend()` instead. In particular, avoid using any of these
  406. // (anti-)patterns:
  407. //
  408. // str.append(StrCat(...))
  409. // str += StrCat(...)
  410. // str = StrCat(str, ...)
  411. //
  412. // The last case is the worst, with a potential to change a loop
  413. // from a linear time operation with O(1) dynamic allocations into a
  414. // quadratic time operation with O(n) dynamic allocations.
  415. //
  416. // See `StrAppend()` below for more information.
  417. namespace strings_internal {
  418. // Do not call directly - this is not part of the public API.
  419. TString CatPieces(std::initializer_list<y_absl::string_view> pieces);
  420. void AppendPieces(y_absl::Nonnull<TString*> dest,
  421. std::initializer_list<y_absl::string_view> pieces);
  422. template <typename Integer>
  423. TString IntegerToString(Integer i) {
  424. // Any integer (signed/unsigned) up to 64 bits can be formatted into a buffer
  425. // with 22 bytes (including NULL at the end).
  426. constexpr size_t kMaxDigits10 = 22;
  427. TString result;
  428. strings_internal::STLStringResizeUninitialized(&result, kMaxDigits10);
  429. char* start = &result[0];
  430. // note: this can be optimized to not write last zero.
  431. char* end = numbers_internal::FastIntToBuffer(i, start);
  432. auto size = static_cast<size_t>(end - start);
  433. assert((size < result.size()) &&
  434. "StrCat(Integer) does not fit into kMaxDigits10");
  435. result.erase(size);
  436. return result;
  437. }
  438. template <typename Float>
  439. TString FloatToString(Float f) {
  440. TString result;
  441. strings_internal::STLStringResizeUninitialized(
  442. &result, numbers_internal::kSixDigitsToBufferSize);
  443. char* start = &result[0];
  444. result.erase(numbers_internal::SixDigitsToBuffer(f, start));
  445. return result;
  446. }
  447. // `SingleArgStrCat` overloads take built-in `int`, `long` and `long long` types
  448. // (signed / unsigned) to avoid ambiguity on the call side. If we used int32_t
  449. // and int64_t, then at least one of the three (`int` / `long` / `long long`)
  450. // would have been ambiguous when passed to `SingleArgStrCat`.
  451. inline TString SingleArgStrCat(int x) { return IntegerToString(x); }
  452. inline TString SingleArgStrCat(unsigned int x) {
  453. return IntegerToString(x);
  454. }
  455. // NOLINTNEXTLINE
  456. inline TString SingleArgStrCat(long x) { return IntegerToString(x); }
  457. // NOLINTNEXTLINE
  458. inline TString SingleArgStrCat(unsigned long x) {
  459. return IntegerToString(x);
  460. }
  461. // NOLINTNEXTLINE
  462. inline TString SingleArgStrCat(long long x) { return IntegerToString(x); }
  463. // NOLINTNEXTLINE
  464. inline TString SingleArgStrCat(unsigned long long x) {
  465. return IntegerToString(x);
  466. }
  467. inline TString SingleArgStrCat(float x) { return FloatToString(x); }
  468. inline TString SingleArgStrCat(double x) { return FloatToString(x); }
  469. // As of September 2023, the SingleArgStrCat() optimization is only enabled for
  470. // libc++. The reasons for this are:
  471. // 1) The SSO size for libc++ is 23, while libstdc++ and MSSTL have an SSO size
  472. // of 15. Since IntegerToString unconditionally resizes the string to 22 bytes,
  473. // this causes both libstdc++ and MSSTL to allocate.
  474. // 2) strings_internal::STLStringResizeUninitialized() only has an
  475. // implementation that avoids initialization when using libc++. This isn't as
  476. // relevant as (1), and the cost should be benchmarked if (1) ever changes on
  477. // libstc++ or MSSTL.
  478. #ifdef _LIBCPP_VERSION
  479. #define Y_ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE true
  480. #else
  481. #define Y_ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE false
  482. #endif
  483. template <typename T, typename = std::enable_if_t<
  484. Y_ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE &&
  485. std::is_arithmetic<T>{} && !std::is_same<T, char>{}>>
  486. using EnableIfFastCase = T;
  487. #undef Y_ABSL_INTERNAL_STRCAT_ENABLE_FAST_CASE
  488. } // namespace strings_internal
  489. Y_ABSL_MUST_USE_RESULT inline TString StrCat() { return TString(); }
  490. template <typename T>
  491. Y_ABSL_MUST_USE_RESULT inline TString StrCat(
  492. strings_internal::EnableIfFastCase<T> a) {
  493. return strings_internal::SingleArgStrCat(a);
  494. }
  495. Y_ABSL_MUST_USE_RESULT inline TString StrCat(const AlphaNum& a) {
  496. return TString(a.data(), a.size());
  497. }
  498. Y_ABSL_MUST_USE_RESULT TString StrCat(const AlphaNum& a, const AlphaNum& b);
  499. Y_ABSL_MUST_USE_RESULT TString StrCat(const AlphaNum& a, const AlphaNum& b,
  500. const AlphaNum& c);
  501. Y_ABSL_MUST_USE_RESULT TString StrCat(const AlphaNum& a, const AlphaNum& b,
  502. const AlphaNum& c, const AlphaNum& d);
  503. // Support 5 or more arguments
  504. template <typename... AV>
  505. Y_ABSL_MUST_USE_RESULT inline TString StrCat(
  506. const AlphaNum& a, const AlphaNum& b, const AlphaNum& c, const AlphaNum& d,
  507. const AlphaNum& e, const AV&... args) {
  508. return strings_internal::CatPieces(
  509. {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(),
  510. static_cast<const AlphaNum&>(args).Piece()...});
  511. }
  512. // -----------------------------------------------------------------------------
  513. // StrAppend()
  514. // -----------------------------------------------------------------------------
  515. //
  516. // Appends a string or set of strings to an existing string, in a similar
  517. // fashion to `StrCat()`.
  518. //
  519. // WARNING: `StrAppend(&str, a, b, c, ...)` requires that none of the
  520. // a, b, c, parameters be a reference into str. For speed, `StrAppend()` does
  521. // not try to check each of its input arguments to be sure that they are not
  522. // a subset of the string being appended to. That is, while this will work:
  523. //
  524. // TString s = "foo";
  525. // s += s;
  526. //
  527. // This output is undefined:
  528. //
  529. // TString s = "foo";
  530. // StrAppend(&s, s);
  531. //
  532. // This output is undefined as well, since `y_absl::string_view` does not own its
  533. // data:
  534. //
  535. // TString s = "foobar";
  536. // y_absl::string_view p = s;
  537. // StrAppend(&s, p);
  538. inline void StrAppend(y_absl::Nonnull<TString*>) {}
  539. void StrAppend(y_absl::Nonnull<TString*> dest, const AlphaNum& a);
  540. void StrAppend(y_absl::Nonnull<TString*> dest, const AlphaNum& a,
  541. const AlphaNum& b);
  542. void StrAppend(y_absl::Nonnull<TString*> dest, const AlphaNum& a,
  543. const AlphaNum& b, const AlphaNum& c);
  544. void StrAppend(y_absl::Nonnull<TString*> dest, const AlphaNum& a,
  545. const AlphaNum& b, const AlphaNum& c, const AlphaNum& d);
  546. // Support 5 or more arguments
  547. template <typename... AV>
  548. inline void StrAppend(y_absl::Nonnull<TString*> dest, const AlphaNum& a,
  549. const AlphaNum& b, const AlphaNum& c, const AlphaNum& d,
  550. const AlphaNum& e, const AV&... args) {
  551. strings_internal::AppendPieces(
  552. dest, {a.Piece(), b.Piece(), c.Piece(), d.Piece(), e.Piece(),
  553. static_cast<const AlphaNum&>(args).Piece()...});
  554. }
  555. // Helper function for the future StrCat default floating-point format, %.6g
  556. // This is fast.
  557. inline strings_internal::AlphaNumBuffer<
  558. numbers_internal::kSixDigitsToBufferSize>
  559. SixDigits(double d) {
  560. strings_internal::AlphaNumBuffer<numbers_internal::kSixDigitsToBufferSize>
  561. result;
  562. result.size = numbers_internal::SixDigitsToBuffer(d, &result.data[0]);
  563. return result;
  564. }
  565. Y_ABSL_NAMESPACE_END
  566. } // namespace y_absl
  567. #endif // Y_ABSL_STRINGS_STR_CAT_H_