string.h 5.9 KB

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