join.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #pragma once
  2. #include <util/generic/string.h>
  3. #include <util/generic/typetraits.h>
  4. #include <util/string/cast.h>
  5. #include "cast.h"
  6. /*
  7. * Default implementation of AppendToString uses a temporary TString object which is inefficient. You can overload it
  8. * for your type to speed up string joins. If you already have an Out() or operator<<() implementation you can simply
  9. * do the following:
  10. *
  11. * inline void AppendToString(TString& dst, const TMyType& t) {
  12. * TStringOutput o(dst);
  13. * o << t;
  14. * }
  15. *
  16. * Unfortunately we can't do this by default because for some types ToString() is defined while Out() is not.
  17. * For standard types (strings of all kinds and arithmetic types) we don't use a temporary TString in AppendToString().
  18. */
  19. template <typename TCharType, typename T>
  20. inline std::enable_if_t<!std::is_arithmetic<std::remove_cv_t<T>>::value, void>
  21. AppendToString(TBasicString<TCharType>& dst, const T& t) {
  22. dst.AppendNoAlias(ToString(t));
  23. }
  24. template <typename TCharType, typename T>
  25. inline std::enable_if_t<std::is_arithmetic<std::remove_cv_t<T>>::value, void>
  26. AppendToString(TBasicString<TCharType>& dst, const T& t) {
  27. char buf[512];
  28. dst.append(buf, ToString<std::remove_cv_t<T>>(t, buf, sizeof(buf)));
  29. }
  30. template <typename TCharType>
  31. inline void AppendToString(TBasicString<TCharType>& dst, const TCharType* t) {
  32. dst.append(t);
  33. }
  34. template <typename TCharType>
  35. inline void AppendToString(TBasicString<TCharType>& dst, TBasicStringBuf<TCharType> t) {
  36. dst.append(t);
  37. }
  38. namespace NPrivate {
  39. template <typename T>
  40. inline size_t GetLength(const T&) {
  41. // By default don't pre-allocate space when joining and appending non-string types.
  42. // This code can be extended by estimating stringified length for specific types (e.g. 10 for ui32).
  43. return 0;
  44. }
  45. template <>
  46. inline size_t GetLength(const TString& s) {
  47. return s.length();
  48. }
  49. template <>
  50. inline size_t GetLength(const TStringBuf& s) {
  51. return s.length();
  52. }
  53. template <>
  54. inline size_t GetLength(const char* const& s) {
  55. return (s ? std::char_traits<char>::length(s) : 0);
  56. }
  57. inline size_t GetAppendLength(const TStringBuf /*delim*/) {
  58. return 0;
  59. }
  60. template <typename TFirst, typename... TRest>
  61. size_t GetAppendLength(const TStringBuf delim, const TFirst& f, const TRest&... r) {
  62. return delim.length() + ::NPrivate::GetLength(f) + ::NPrivate::GetAppendLength(delim, r...);
  63. }
  64. }
  65. template <typename TCharType>
  66. inline void AppendJoinNoReserve(TBasicString<TCharType>&, TBasicStringBuf<TCharType>) {
  67. }
  68. template <typename TCharType, typename TFirst, typename... TRest>
  69. inline void AppendJoinNoReserve(TBasicString<TCharType>& dst, TBasicStringBuf<TCharType> delim, const TFirst& f, const TRest&... r) {
  70. AppendToString(dst, delim);
  71. AppendToString(dst, f);
  72. AppendJoinNoReserve(dst, delim, r...);
  73. }
  74. template <typename... TValues>
  75. inline void AppendJoin(TString& dst, const TStringBuf delim, const TValues&... values) {
  76. const size_t appendLength = ::NPrivate::GetAppendLength(delim, values...);
  77. if (appendLength > 0) {
  78. dst.reserve(dst.length() + appendLength);
  79. }
  80. AppendJoinNoReserve(dst, delim, values...);
  81. }
  82. template <typename TFirst, typename... TRest>
  83. inline TString Join(const TStringBuf delim, const TFirst& f, const TRest&... r) {
  84. TString ret = ToString(f);
  85. AppendJoin(ret, delim, r...);
  86. return ret;
  87. }
  88. // Note that char delimeter @cdelim will be printed as single char string,
  89. // but any char value @v will be printed as corresponding numeric code.
  90. // For example, Join('a', 'a', 'a') will print "97a97" (see unit-test).
  91. template <typename... TValues>
  92. inline TString Join(char cdelim, const TValues&... v) {
  93. return Join(TStringBuf(&cdelim, 1), v...);
  94. }
  95. namespace NPrivate {
  96. template <typename TCharType, typename TIter>
  97. inline TBasicString<TCharType> JoinRange(TBasicStringBuf<TCharType> delim, const TIter beg, const TIter end) {
  98. TBasicString<TCharType> out;
  99. if (beg != end) {
  100. size_t total = ::NPrivate::GetLength(*beg);
  101. for (TIter pos = beg; ++pos != end;) {
  102. total += delim.length() + ::NPrivate::GetLength(*pos);
  103. }
  104. if (total > 0) {
  105. out.reserve(total);
  106. }
  107. AppendToString(out, *beg);
  108. for (TIter pos = beg; ++pos != end;) {
  109. AppendJoinNoReserve(out, delim, *pos);
  110. }
  111. }
  112. return out;
  113. }
  114. } // namespace NPrivate
  115. template <typename TIter>
  116. TString JoinRange(std::string_view delim, const TIter beg, const TIter end) {
  117. return ::NPrivate::JoinRange<char>(delim, beg, end);
  118. }
  119. template <typename TIter>
  120. TString JoinRange(char delim, const TIter beg, const TIter end) {
  121. TStringBuf delimBuf(&delim, 1);
  122. return ::NPrivate::JoinRange<char>(delimBuf, beg, end);
  123. }
  124. template <typename TIter>
  125. TUtf16String JoinRange(std::u16string_view delim, const TIter beg, const TIter end) {
  126. return ::NPrivate::JoinRange<wchar16>(delim, beg, end);
  127. }
  128. template <typename TIter>
  129. TUtf16String JoinRange(wchar16 delim, const TIter beg, const TIter end) {
  130. TWtringBuf delimBuf(&delim, 1);
  131. return ::NPrivate::JoinRange<wchar16>(delimBuf, beg, end);
  132. }
  133. template <typename TIter>
  134. TUtf32String JoinRange(std::u32string_view delim, const TIter beg, const TIter end) {
  135. return ::NPrivate::JoinRange<wchar32>(delim, beg, end);
  136. }
  137. template <typename TIter>
  138. TUtf32String JoinRange(wchar32 delim, const TIter beg, const TIter end) {
  139. TUtf32StringBuf delimBuf(&delim, 1);
  140. return ::NPrivate::JoinRange<wchar32>(delimBuf, beg, end);
  141. }
  142. template <typename TCharType, typename TContainer>
  143. inline TBasicString<TCharType> JoinSeq(std::basic_string_view<TCharType> delim, const TContainer& data) {
  144. using std::begin;
  145. using std::end;
  146. return JoinRange(delim, begin(data), end(data));
  147. }
  148. template <typename TCharType, typename TContainer>
  149. inline TBasicString<TCharType> JoinSeq(const TCharType* delim, const TContainer& data) {
  150. TBasicStringBuf<TCharType> delimBuf = delim;
  151. return JoinSeq(delimBuf, data);
  152. }
  153. template <typename TCharType, typename TContainer>
  154. inline TBasicString<TCharType> JoinSeq(const TBasicString<TCharType>& delim, const TContainer& data) {
  155. TBasicStringBuf<TCharType> delimBuf = delim;
  156. return JoinSeq(delimBuf, data);
  157. }
  158. template <typename TCharType, typename TContainer>
  159. inline std::enable_if_t<
  160. std::is_same_v<TCharType, char> ||
  161. std::is_same_v<TCharType, char16_t> ||
  162. std::is_same_v<TCharType, char32_t>,
  163. TBasicString<TCharType>>
  164. JoinSeq(TCharType delim, const TContainer& data) {
  165. TBasicStringBuf<TCharType> delimBuf(&delim, 1);
  166. return JoinSeq(delimBuf, data);
  167. }
  168. /** \brief Functor for streaming iterative objects from TIterB e to TIterE b, separated with delim.
  169. * Difference from JoinSeq, JoinRange, Join is the lack of TString object - all depends on operator<< for the type and
  170. * realization of IOutputStream
  171. */
  172. template <class TIterB, class TIterE>
  173. struct TRangeJoiner {
  174. friend constexpr IOutputStream& operator<<(IOutputStream& stream, const TRangeJoiner<TIterB, TIterE>& rangeJoiner) {
  175. if (rangeJoiner.b != rangeJoiner.e) {
  176. stream << *rangeJoiner.b;
  177. for (auto it = std::next(rangeJoiner.b); it != rangeJoiner.e; ++it)
  178. stream << rangeJoiner.delim << *it;
  179. }
  180. return stream;
  181. }
  182. constexpr TRangeJoiner(TStringBuf delim, TIterB&& b, TIterE&& e)
  183. : delim(delim)
  184. , b(std::forward<TIterB>(b))
  185. , e(std::forward<TIterE>(e))
  186. {
  187. }
  188. private:
  189. const TStringBuf delim;
  190. const TIterB b;
  191. const TIterE e;
  192. };
  193. template <class TIterB, class TIterE = TIterB>
  194. constexpr auto MakeRangeJoiner(TStringBuf delim, TIterB&& b, TIterE&& e) {
  195. return TRangeJoiner<TIterB, TIterE>(delim, std::forward<TIterB>(b), std::forward<TIterE>(e));
  196. }
  197. template <class TContainer>
  198. constexpr auto MakeRangeJoiner(TStringBuf delim, const TContainer& data) {
  199. return MakeRangeJoiner(delim, std::cbegin(data), std::cend(data));
  200. }
  201. template <class TVal>
  202. constexpr auto MakeRangeJoiner(TStringBuf delim, const std::initializer_list<TVal>& data) {
  203. return MakeRangeJoiner(delim, std::cbegin(data), std::cend(data));
  204. }
  205. /* We force (std::initializer_list<TStringBuf>) input type for (TString) and (const char*) types because:
  206. * # When (std::initializer_list<TString>) is used, TString objects are copied into the initializer_list object.
  207. * Storing TStringBufs instead is faster, even with COW-enabled strings.
  208. * # For (const char*) we calculate length only once and store it in TStringBuf. Otherwise strlen scan would be executed
  209. * in both GetAppendLength and AppendToString. For string literals constant lengths get propagated in compile-time.
  210. *
  211. * This way JoinSeq(",", { s1, s2 }) always does the right thing whatever types s1 and s2 have.
  212. *
  213. * If someone needs to join std::initializer_list<TString> -- it still works because of the TContainer template above.
  214. */
  215. template <typename T>
  216. inline std::enable_if_t<
  217. !std::is_same<std::decay_t<T>, TString>::value && !std::is_same<std::decay_t<T>, const char*>::value,
  218. TString>
  219. JoinSeq(const TStringBuf delim, const std::initializer_list<T>& data) {
  220. return JoinRange(delim, data.begin(), data.end());
  221. }
  222. inline TString JoinSeq(const TStringBuf delim, const std::initializer_list<TStringBuf>& data) {
  223. return JoinRange(delim, data.begin(), data.end());
  224. }