string.h 5.8 KB

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