vector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include "cast.h"
  3. #include "split.h"
  4. #include <util/generic/map.h>
  5. #include <util/generic/strbuf.h>
  6. #include <util/generic/string.h>
  7. #include <util/generic/vector.h>
  8. #include <util/string/cast.h>
  9. #include <util/system/yassert.h>
  10. #define KEEP_EMPTY_TOKENS 0x01
  11. //
  12. // NOTE: Check StringSplitter below to get more convenient split string interface.
  13. namespace NPrivate {
  14. void SplitStringImpl(TVector<TString>* res, const char* ptr,
  15. const char* delimiter, size_t maxFields, int options);
  16. void SplitStringImpl(TVector<TString>* res, const char* ptr, size_t len,
  17. const char* delimiter, size_t maxFields, int options);
  18. void SplitStringImpl(TVector<TUtf16String>* res, const wchar16* ptr,
  19. const wchar16* delimiter, size_t maxFields, int options);
  20. void SplitStringImpl(TVector<TUtf16String>* res, const wchar16* ptr, size_t len,
  21. const wchar16* delimiter, size_t maxFields, int options);
  22. template <typename C>
  23. struct TStringDeducer;
  24. template <>
  25. struct TStringDeducer<char> {
  26. using type = TString;
  27. };
  28. template <>
  29. struct TStringDeducer<wchar16> {
  30. using type = TUtf16String;
  31. };
  32. } // namespace NPrivate
  33. template <typename C>
  34. TVector<typename ::NPrivate::TStringDeducer<C>::type>
  35. SplitString(const C* ptr, const C* delimiter,
  36. size_t maxFields = 0, int options = 0) {
  37. TVector<typename ::NPrivate::TStringDeducer<C>::type> res;
  38. ::NPrivate::SplitStringImpl(&res, ptr, delimiter, maxFields, options);
  39. return res;
  40. }
  41. template <typename C>
  42. TVector<typename ::NPrivate::TStringDeducer<C>::type>
  43. SplitString(const C* ptr, size_t len, const C* delimiter,
  44. size_t maxFields = 0, int options = 0) {
  45. TVector<typename ::NPrivate::TStringDeducer<C>::type> res;
  46. ::NPrivate::SplitStringImpl(&res, ptr, len, delimiter, maxFields, options);
  47. return res;
  48. }
  49. template <typename C>
  50. TVector<typename ::NPrivate::TStringDeducer<C>::type>
  51. SplitString(const typename ::NPrivate::TStringDeducer<C>::type& str, const C* delimiter,
  52. size_t maxFields = 0, int options = 0) {
  53. return SplitString(str.data(), str.size(), delimiter, maxFields, options);
  54. }
  55. template <class TIter>
  56. inline TString JoinStrings(TIter begin, TIter end, const TStringBuf delim) {
  57. if (begin == end) {
  58. return TString();
  59. }
  60. TString result = ToString(*begin);
  61. for (++begin; begin != end; ++begin) {
  62. result.append(delim);
  63. result.append(ToString(*begin));
  64. }
  65. return result;
  66. }
  67. template <class TIter>
  68. inline TUtf16String JoinStrings(TIter begin, TIter end, const TWtringBuf delim) {
  69. if (begin == end) {
  70. return TUtf16String();
  71. }
  72. TUtf16String result = ToWtring(*begin);
  73. for (++begin; begin != end; ++begin) {
  74. result.append(delim);
  75. result.append(ToWtring(*begin));
  76. }
  77. return result;
  78. }
  79. /// Concatenates elements of given TVector<TString>.
  80. inline TString JoinStrings(const TVector<TString>& v, const TStringBuf delim) {
  81. return JoinStrings(v.begin(), v.end(), delim);
  82. }
  83. inline TString JoinStrings(const TVector<TString>& v, size_t index, size_t count, const TStringBuf delim) {
  84. Y_ASSERT(index + count <= v.size() && "JoinStrings(): index or count out of range");
  85. return JoinStrings(v.begin() + index, v.begin() + index + count, delim);
  86. }
  87. template <typename T>
  88. inline TString JoinVectorIntoString(const TVector<T>& v, const TStringBuf delim) {
  89. return JoinStrings(v.begin(), v.end(), delim);
  90. }
  91. template <typename T>
  92. inline TString JoinVectorIntoString(const TVector<T>& v, size_t index, size_t count, const TStringBuf delim) {
  93. Y_ASSERT(index + count <= v.size() && "JoinVectorIntoString(): index or count out of range");
  94. return JoinStrings(v.begin() + index, v.begin() + index + count, delim);
  95. }
  96. TUtf16String JoinStrings(const TVector<TUtf16String>& v, const TWtringBuf delim);
  97. TUtf16String JoinStrings(const TVector<TUtf16String>& v, size_t index, size_t count, const TWtringBuf delim);
  98. //! Converts vector of strings to vector of type T variables
  99. template <typename T, typename TStringType>
  100. TVector<T> Scan(const TVector<TStringType>& input) {
  101. TVector<T> output;
  102. output.reserve(input.size());
  103. for (int i = 0; i < input.ysize(); ++i) {
  104. output.push_back(FromString<T>(input[i]));
  105. }
  106. return output;
  107. }