string-inl.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef STRING_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include string.h"
  3. // For the sake of sane code completion.
  4. #include "string.h"
  5. #endif
  6. #include "string_builder.h"
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //! Joins a range of items into a string intermixing them with the delimiter.
  10. /*!
  11. * \param builder String builder where the output goes.
  12. * \param begin Iterator pointing to the first item (inclusive).
  13. * \param end Iterator pointing to the last item (not inclusive).
  14. * \param formatter Formatter to apply to the items.
  15. * \param delimiter A delimiter to be inserted between items: ", " by default.
  16. * \return The resulting combined string.
  17. */
  18. template <std::forward_iterator TIterator, class TFormatter>
  19. void JoinToString(
  20. TStringBuilderBase* builder,
  21. const TIterator& begin,
  22. const TIterator& end,
  23. const TFormatter& formatter,
  24. TStringBuf delimiter)
  25. {
  26. for (auto current = begin; current != end; ++current) {
  27. if (current != begin) {
  28. builder->AppendString(delimiter);
  29. }
  30. formatter(builder, *current);
  31. }
  32. }
  33. template <std::forward_iterator TIterator, class TFormatter>
  34. TString JoinToString(
  35. const TIterator& begin,
  36. const TIterator& end,
  37. const TFormatter& formatter,
  38. TStringBuf delimiter)
  39. {
  40. TStringBuilder builder;
  41. JoinToString(&builder, begin, end, formatter, delimiter);
  42. return builder.Flush();
  43. }
  44. //! A handy shortcut with default formatter.
  45. template <std::forward_iterator TIterator>
  46. TString JoinToString(
  47. const TIterator& begin,
  48. const TIterator& end,
  49. TStringBuf delimiter)
  50. {
  51. return JoinToString(begin, end, TDefaultFormatter(), delimiter);
  52. }
  53. //! Joins a collection of given items into a string intermixing them with the delimiter.
  54. /*!
  55. * \param collection A collection containing the items to be joined.
  56. * \param formatter Formatter to apply to the items.
  57. * \param delimiter A delimiter to be inserted between items; ", " by default.
  58. */
  59. template <std::ranges::range TCollection, class TFormatter>
  60. TString JoinToString(
  61. TCollection&& collection,
  62. const TFormatter& formatter,
  63. TStringBuf delimiter)
  64. {
  65. using std::begin;
  66. using std::end;
  67. return JoinToString(begin(collection), end(collection), formatter, delimiter);
  68. }
  69. //! A handy shortcut with the default formatter.
  70. template <std::ranges::range TCollection>
  71. TString JoinToString(
  72. TCollection&& collection,
  73. TStringBuf delimiter)
  74. {
  75. return JoinToString(std::forward<TCollection>(collection), TDefaultFormatter(), delimiter);
  76. }
  77. //! Concatenates a bunch of TStringBuf-like instances into TString.
  78. template <class... Ts>
  79. TString ConcatToString(Ts... args)
  80. {
  81. size_t length = 0;
  82. ((length += args.length()), ...);
  83. TString result;
  84. result.reserve(length);
  85. (result.append(args), ...);
  86. return result;
  87. }
  88. //! Converts a range of items into strings.
  89. template <std::forward_iterator TIter, class TFormatter>
  90. std::vector<TString> ConvertToStrings(
  91. const TIter& begin,
  92. const TIter& end,
  93. const TFormatter& formatter,
  94. size_t maxSize)
  95. {
  96. std::vector<TString> result;
  97. for (auto it = begin; it != end; ++it) {
  98. TStringBuilder builder;
  99. formatter(&builder, *it);
  100. result.push_back(builder.Flush());
  101. if (result.size() == maxSize) {
  102. break;
  103. }
  104. }
  105. return result;
  106. }
  107. //! A handy shortcut with the default formatter.
  108. template <std::forward_iterator TIter>
  109. std::vector<TString> ConvertToStrings(
  110. const TIter& begin,
  111. const TIter& end,
  112. size_t maxSize)
  113. {
  114. return ConvertToStrings(begin, end, TDefaultFormatter(), maxSize);
  115. }
  116. //! Converts a given collection of items into strings.
  117. /*!
  118. * \param collection A collection containing the items to be converted.
  119. * \param formatter Formatter to apply to the items.
  120. * \param maxSize Size limit for the resulting vector.
  121. */
  122. template <std::ranges::range TCollection, class TFormatter>
  123. std::vector<TString> ConvertToStrings(
  124. TCollection&& collection,
  125. const TFormatter& formatter,
  126. size_t maxSize)
  127. {
  128. using std::begin;
  129. using std::end;
  130. return ConvertToStrings(begin(collection), end(collection), formatter, maxSize);
  131. }
  132. //! A handy shortcut with default formatter.
  133. template <std::ranges::range TCollection>
  134. std::vector<TString> ConvertToStrings(
  135. TCollection&& collection,
  136. size_t maxSize)
  137. {
  138. return ConvertToStrings(std::forward<TCollection>(collection), TDefaultFormatter(), maxSize);
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////
  141. } // namespace NYT