string.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. 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. template <class TIterator, class TFormatter>
  46. TString JoinToString(
  47. const TIterator& begin,
  48. const TIterator& end,
  49. const TFormatter& formatter,
  50. TStringBuf delimiter = DefaultJoinToStringDelimiter);
  51. //! A handy shortcut with default formatter.
  52. template <class TIterator>
  53. TString JoinToString(
  54. const TIterator& begin,
  55. const TIterator& end,
  56. TStringBuf delimiter = DefaultJoinToStringDelimiter);
  57. //! Joins a collection of given items into a string intermixing them with the delimiter.
  58. /*!
  59. * \param collection A collection containing the items to be joined.
  60. * \param formatter Formatter to apply to the items.
  61. * \param delimiter A delimiter to be inserted between items; ", " by default.
  62. */
  63. template <class TCollection, class TFormatter>
  64. TString JoinToString(
  65. const TCollection& collection,
  66. const TFormatter& formatter,
  67. TStringBuf delimiter = DefaultJoinToStringDelimiter);
  68. //! A handy shortcut with the default formatter.
  69. template <class TCollection>
  70. TString JoinToString(
  71. const TCollection& collection,
  72. TStringBuf delimiter = DefaultJoinToStringDelimiter);
  73. //! Concatenates a bunch of TStringBuf-like instances into TString.
  74. template <class... Ts>
  75. TString ConcatToString(Ts... args);
  76. //! Converts a range of items into strings.
  77. template <class TIter, class TFormatter>
  78. std::vector<TString> ConvertToStrings(
  79. const TIter& begin,
  80. const TIter& end,
  81. const TFormatter& formatter,
  82. size_t maxSize = std::numeric_limits<size_t>::max());
  83. //! A handy shortcut with the default formatter.
  84. template <class TIter>
  85. std::vector<TString> ConvertToStrings(
  86. const TIter& begin,
  87. const TIter& end,
  88. size_t maxSize = std::numeric_limits<size_t>::max());
  89. //! Converts a given collection of items into strings.
  90. /*!
  91. * \param collection A collection containing the items to be converted.
  92. * \param formatter Formatter to apply to the items.
  93. * \param maxSize Size limit for the resulting vector.
  94. */
  95. template <class TCollection, class TFormatter>
  96. std::vector<TString> ConvertToStrings(
  97. const TCollection& collection,
  98. const TFormatter& formatter,
  99. size_t maxSize = std::numeric_limits<size_t>::max());
  100. //! A handy shortcut with default formatter.
  101. template <class TCollection>
  102. std::vector<TString> ConvertToStrings(
  103. const TCollection& collection,
  104. size_t maxSize = std::numeric_limits<size_t>::max());
  105. ////////////////////////////////////////////////////////////////////////////////
  106. void UnderscoreCaseToCamelCase(TStringBuilderBase* builder, TStringBuf str);
  107. TString UnderscoreCaseToCamelCase(TStringBuf str);
  108. void CamelCaseToUnderscoreCase(TStringBuilderBase* builder, TStringBuf str);
  109. TString CamelCaseToUnderscoreCase(TStringBuf str);
  110. TString TrimLeadingWhitespaces(const TString& str);
  111. TString Trim(const TString& str, const TString& whitespaces);
  112. ////////////////////////////////////////////////////////////////////////////////
  113. //! Implemented for |[u]i(32|64)|.
  114. template <class T>
  115. char* WriteDecIntToBufferBackwards(char* ptr, T value);
  116. //! Implemented for |[u]i(32|64)|.
  117. template <class T>
  118. char* WriteHexIntToBufferBackwards(char* ptr, T value, bool uppercase);
  119. ////////////////////////////////////////////////////////////////////////////////
  120. struct TCaseInsensitiveStringHasher
  121. {
  122. size_t operator()(TStringBuf arg) const;
  123. };
  124. struct TCaseInsensitiveStringEqualityComparer
  125. {
  126. bool operator()(TStringBuf lhs, TStringBuf rhs) const;
  127. };
  128. ////////////////////////////////////////////////////////////////////////////////
  129. bool TryParseBool(TStringBuf value, bool* result);
  130. bool ParseBool(TStringBuf value);
  131. TStringBuf FormatBool(bool value);
  132. ////////////////////////////////////////////////////////////////////////////////
  133. } // namespace NYT
  134. #define STRING_INL_H_
  135. #include "string-inl.h"
  136. #undef STRING_INL_H_