string.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #pragma once
  2. #include "string_builder.h"
  3. #include <library/cpp/yt/exception/exception.h>
  4. #include <util/datetime/base.h>
  5. #include <util/generic/string.h>
  6. #include <util/string/strip.h>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. namespace NYT {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. //! Formatters enable customizable way to turn an object into a string.
  13. //! This default implementation uses |FormatValue|.
  14. struct TDefaultFormatter
  15. {
  16. template <class T>
  17. void operator()(TStringBuilderBase* builder, const T& obj) const
  18. {
  19. FormatValue(builder, obj, TStringBuf("v"));
  20. }
  21. };
  22. static constexpr TStringBuf DefaultJoinToStringDelimiter = ", ";
  23. static constexpr TStringBuf DefaultKeyValueDelimiter = ": ";
  24. static constexpr TStringBuf DefaultRangeEllipsisFormat = "...";
  25. // ASCII characters from 0x20 = ' ' to 0x7e = '~' are printable.
  26. static constexpr char PrintableASCIILow = 0x20;
  27. static constexpr char PrintableASCIIHigh = 0x7e;
  28. static constexpr TStringBuf IntToHexLowercase = "0123456789abcdef";
  29. static constexpr TStringBuf IntToHexUppercase = "0123456789ABCDEF";
  30. //! Joins a range of items into a string intermixing them with the delimiter.
  31. /*!
  32. * \param builder String builder where the output goes.
  33. * \param begin Iterator pointing to the first item (inclusive).
  34. * \param end Iterator pointing to the last item (not inclusive).
  35. * \param formatter Formatter to apply to the items.
  36. * \param delimiter A delimiter to be inserted between items: ", " by default.
  37. * \return The resulting combined string.
  38. */
  39. template <class TIterator, class TFormatter>
  40. void JoinToString(
  41. TStringBuilderBase* builder,
  42. const TIterator& begin,
  43. const TIterator& end,
  44. const TFormatter& formatter,
  45. TStringBuf delimiter = DefaultJoinToStringDelimiter)
  46. {
  47. for (auto current = begin; current != end; ++current) {
  48. if (current != begin) {
  49. builder->AppendString(delimiter);
  50. }
  51. formatter(builder, *current);
  52. }
  53. }
  54. template <class TIterator, class TFormatter>
  55. TString JoinToString(
  56. const TIterator& begin,
  57. const TIterator& end,
  58. const TFormatter& formatter,
  59. TStringBuf delimiter = DefaultJoinToStringDelimiter)
  60. {
  61. TStringBuilder builder;
  62. JoinToString(&builder, begin, end, formatter, delimiter);
  63. return builder.Flush();
  64. }
  65. //! A handy shortcut with default formatter.
  66. template <class TIterator>
  67. TString JoinToString(
  68. const TIterator& begin,
  69. const TIterator& end,
  70. TStringBuf delimiter = DefaultJoinToStringDelimiter)
  71. {
  72. return JoinToString(begin, end, TDefaultFormatter(), delimiter);
  73. }
  74. //! Joins a collection of given items into a string intermixing them with the delimiter.
  75. /*!
  76. * \param collection A collection containing the items to be joined.
  77. * \param formatter Formatter to apply to the items.
  78. * \param delimiter A delimiter to be inserted between items; ", " by default.
  79. */
  80. template <class TCollection, class TFormatter>
  81. TString JoinToString(
  82. const TCollection& collection,
  83. const TFormatter& formatter,
  84. TStringBuf delimiter = DefaultJoinToStringDelimiter)
  85. {
  86. using std::begin;
  87. using std::end;
  88. return JoinToString(begin(collection), end(collection), formatter, delimiter);
  89. }
  90. //! A handy shortcut with the default formatter.
  91. template <class TCollection>
  92. TString JoinToString(
  93. const TCollection& collection,
  94. TStringBuf delimiter = DefaultJoinToStringDelimiter)
  95. {
  96. return JoinToString(collection, TDefaultFormatter(), delimiter);
  97. }
  98. //! Concatenates a bunch of TStringBuf-like instances into TString.
  99. template <class... Ts>
  100. TString ConcatToString(Ts... args)
  101. {
  102. size_t length = 0;
  103. ((length += args.length()), ...);
  104. TString result;
  105. result.reserve(length);
  106. (result.append(args), ...);
  107. return result;
  108. }
  109. //! Converts a range of items into strings.
  110. template <class TIter, class TFormatter>
  111. std::vector<TString> ConvertToStrings(
  112. const TIter& begin,
  113. const TIter& end,
  114. const TFormatter& formatter,
  115. size_t maxSize = std::numeric_limits<size_t>::max())
  116. {
  117. std::vector<TString> result;
  118. for (auto it = begin; it != end; ++it) {
  119. TStringBuilder builder;
  120. formatter(&builder, *it);
  121. result.push_back(builder.Flush());
  122. if (result.size() == maxSize) {
  123. break;
  124. }
  125. }
  126. return result;
  127. }
  128. //! A handy shortcut with the default formatter.
  129. template <class TIter>
  130. std::vector<TString> ConvertToStrings(
  131. const TIter& begin,
  132. const TIter& end,
  133. size_t maxSize = std::numeric_limits<size_t>::max())
  134. {
  135. return ConvertToStrings(begin, end, TDefaultFormatter(), maxSize);
  136. }
  137. //! Converts a given collection of items into strings.
  138. /*!
  139. * \param collection A collection containing the items to be converted.
  140. * \param formatter Formatter to apply to the items.
  141. * \param maxSize Size limit for the resulting vector.
  142. */
  143. template <class TCollection, class TFormatter>
  144. std::vector<TString> ConvertToStrings(
  145. const TCollection& collection,
  146. const TFormatter& formatter,
  147. size_t maxSize = std::numeric_limits<size_t>::max())
  148. {
  149. using std::begin;
  150. using std::end;
  151. return ConvertToStrings(begin(collection), end(collection), formatter, maxSize);
  152. }
  153. //! A handy shortcut with default formatter.
  154. template <class TCollection>
  155. std::vector<TString> ConvertToStrings(
  156. const TCollection& collection,
  157. size_t maxSize = std::numeric_limits<size_t>::max())
  158. {
  159. return ConvertToStrings(collection, TDefaultFormatter(), maxSize);
  160. }
  161. ////////////////////////////////////////////////////////////////////////////////
  162. void UnderscoreCaseToCamelCase(TStringBuilderBase* builder, TStringBuf str);
  163. TString UnderscoreCaseToCamelCase(TStringBuf str);
  164. void CamelCaseToUnderscoreCase(TStringBuilderBase* builder, TStringBuf str);
  165. TString CamelCaseToUnderscoreCase(TStringBuf str);
  166. TString TrimLeadingWhitespaces(const TString& str);
  167. TString Trim(const TString& str, const TString& whitespaces);
  168. ////////////////////////////////////////////////////////////////////////////////
  169. //! Implemented for |[u]i(32|64)|.
  170. template <class T>
  171. char* WriteDecIntToBufferBackwards(char* ptr, T value);
  172. //! Implemented for |[u]i(32|64)|.
  173. template <class T>
  174. char* WriteHexIntToBufferBackwards(char* ptr, T value, bool uppercase);
  175. ////////////////////////////////////////////////////////////////////////////////
  176. struct TCaseInsensitiveStringHasher
  177. {
  178. size_t operator()(TStringBuf arg) const;
  179. };
  180. struct TCaseInsensitiveStringEqualityComparer
  181. {
  182. bool operator()(TStringBuf lhs, TStringBuf rhs) const;
  183. };
  184. ////////////////////////////////////////////////////////////////////////////////
  185. bool TryParseBool(TStringBuf value, bool* result);
  186. bool ParseBool(TStringBuf value);
  187. TStringBuf FormatBool(bool value);
  188. ////////////////////////////////////////////////////////////////////////////////
  189. } // namespace NYT