string.h 6.7 KB

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