str_cat.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #include "y_absl/strings/str_cat.h"
  15. #include <assert.h>
  16. #include <algorithm>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <cstring>
  20. #include <util/generic/string.h>
  21. #include "y_absl/strings/ascii.h"
  22. #include "y_absl/strings/internal/resize_uninitialized.h"
  23. #include "y_absl/strings/numbers.h"
  24. #include "y_absl/strings/string_view.h"
  25. namespace y_absl {
  26. Y_ABSL_NAMESPACE_BEGIN
  27. // ----------------------------------------------------------------------
  28. // StrCat()
  29. // This merges the given strings or integers, with no delimiter. This
  30. // is designed to be the fastest possible way to construct a string out
  31. // of a mix of raw C strings, string_views, strings, and integer values.
  32. // ----------------------------------------------------------------------
  33. // Append is merely a version of memcpy that returns the address of the byte
  34. // after the area just overwritten.
  35. static char* Append(char* out, const AlphaNum& x) {
  36. // memcpy is allowed to overwrite arbitrary memory, so doing this after the
  37. // call would force an extra fetch of x.size().
  38. char* after = out + x.size();
  39. if (x.size() != 0) {
  40. memcpy(out, x.data(), x.size());
  41. }
  42. return after;
  43. }
  44. TString StrCat(const AlphaNum& a, const AlphaNum& b) {
  45. TString result;
  46. y_absl::strings_internal::STLStringResizeUninitialized(&result,
  47. a.size() + b.size());
  48. char* const begin = &result[0];
  49. char* out = begin;
  50. out = Append(out, a);
  51. out = Append(out, b);
  52. assert(out == begin + result.size());
  53. return result;
  54. }
  55. TString StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c) {
  56. TString result;
  57. strings_internal::STLStringResizeUninitialized(
  58. &result, a.size() + b.size() + c.size());
  59. char* const begin = &result[0];
  60. char* out = begin;
  61. out = Append(out, a);
  62. out = Append(out, b);
  63. out = Append(out, c);
  64. assert(out == begin + result.size());
  65. return result;
  66. }
  67. TString StrCat(const AlphaNum& a, const AlphaNum& b, const AlphaNum& c,
  68. const AlphaNum& d) {
  69. TString result;
  70. strings_internal::STLStringResizeUninitialized(
  71. &result, a.size() + b.size() + c.size() + d.size());
  72. char* const begin = &result[0];
  73. char* out = begin;
  74. out = Append(out, a);
  75. out = Append(out, b);
  76. out = Append(out, c);
  77. out = Append(out, d);
  78. assert(out == begin + result.size());
  79. return result;
  80. }
  81. namespace strings_internal {
  82. // Do not call directly - these are not part of the public API.
  83. TString CatPieces(std::initializer_list<y_absl::string_view> pieces) {
  84. TString result;
  85. size_t total_size = 0;
  86. for (y_absl::string_view piece : pieces) total_size += piece.size();
  87. strings_internal::STLStringResizeUninitialized(&result, total_size);
  88. char* const begin = &result[0];
  89. char* out = begin;
  90. for (y_absl::string_view piece : pieces) {
  91. const size_t this_size = piece.size();
  92. if (this_size != 0) {
  93. memcpy(out, piece.data(), this_size);
  94. out += this_size;
  95. }
  96. }
  97. assert(out == begin + result.size());
  98. return result;
  99. }
  100. // It's possible to call StrAppend with an y_absl::string_view that is itself a
  101. // fragment of the string we're appending to. However the results of this are
  102. // random. Therefore, check for this in debug mode. Use unsigned math so we
  103. // only have to do one comparison. Note, there's an exception case: appending an
  104. // empty string is always allowed.
  105. #define ASSERT_NO_OVERLAP(dest, src) \
  106. assert(((src).size() == 0) || \
  107. (uintptr_t((src).data() - (dest).data()) > uintptr_t((dest).size())))
  108. void AppendPieces(TString* dest,
  109. std::initializer_list<y_absl::string_view> pieces) {
  110. size_t old_size = dest->size();
  111. size_t total_size = old_size;
  112. for (y_absl::string_view piece : pieces) {
  113. ASSERT_NO_OVERLAP(*dest, piece);
  114. total_size += piece.size();
  115. }
  116. strings_internal::STLStringResizeUninitializedAmortized(dest, total_size);
  117. char* const begin = &(*dest)[0];
  118. char* out = begin + old_size;
  119. for (y_absl::string_view piece : pieces) {
  120. const size_t this_size = piece.size();
  121. if (this_size != 0) {
  122. memcpy(out, piece.data(), this_size);
  123. out += this_size;
  124. }
  125. }
  126. assert(out == begin + dest->size());
  127. }
  128. } // namespace strings_internal
  129. void StrAppend(TString* dest, const AlphaNum& a) {
  130. ASSERT_NO_OVERLAP(*dest, a);
  131. TString::size_type old_size = dest->size();
  132. strings_internal::STLStringResizeUninitializedAmortized(dest,
  133. old_size + a.size());
  134. char* const begin = &(*dest)[0];
  135. char* out = begin + old_size;
  136. out = Append(out, a);
  137. assert(out == begin + dest->size());
  138. }
  139. void StrAppend(TString* dest, const AlphaNum& a, const AlphaNum& b) {
  140. ASSERT_NO_OVERLAP(*dest, a);
  141. ASSERT_NO_OVERLAP(*dest, b);
  142. TString::size_type old_size = dest->size();
  143. strings_internal::STLStringResizeUninitializedAmortized(
  144. dest, old_size + a.size() + b.size());
  145. char* const begin = &(*dest)[0];
  146. char* out = begin + old_size;
  147. out = Append(out, a);
  148. out = Append(out, b);
  149. assert(out == begin + dest->size());
  150. }
  151. void StrAppend(TString* dest, const AlphaNum& a, const AlphaNum& b,
  152. const AlphaNum& c) {
  153. ASSERT_NO_OVERLAP(*dest, a);
  154. ASSERT_NO_OVERLAP(*dest, b);
  155. ASSERT_NO_OVERLAP(*dest, c);
  156. TString::size_type old_size = dest->size();
  157. strings_internal::STLStringResizeUninitializedAmortized(
  158. dest, old_size + a.size() + b.size() + c.size());
  159. char* const begin = &(*dest)[0];
  160. char* out = begin + old_size;
  161. out = Append(out, a);
  162. out = Append(out, b);
  163. out = Append(out, c);
  164. assert(out == begin + dest->size());
  165. }
  166. void StrAppend(TString* dest, const AlphaNum& a, const AlphaNum& b,
  167. const AlphaNum& c, const AlphaNum& d) {
  168. ASSERT_NO_OVERLAP(*dest, a);
  169. ASSERT_NO_OVERLAP(*dest, b);
  170. ASSERT_NO_OVERLAP(*dest, c);
  171. ASSERT_NO_OVERLAP(*dest, d);
  172. TString::size_type old_size = dest->size();
  173. strings_internal::STLStringResizeUninitializedAmortized(
  174. dest, old_size + a.size() + b.size() + c.size() + d.size());
  175. char* const begin = &(*dest)[0];
  176. char* out = begin + old_size;
  177. out = Append(out, a);
  178. out = Append(out, b);
  179. out = Append(out, c);
  180. out = Append(out, d);
  181. assert(out == begin + dest->size());
  182. }
  183. Y_ABSL_NAMESPACE_END
  184. } // namespace y_absl