vector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. TString result = ToString(*begin);
  60. for (++begin; begin != end; ++begin) {
  61. result.append(delim);
  62. result.append(ToString(*begin));
  63. }
  64. return result;
  65. }
  66. template <class TIter>
  67. inline TUtf16String JoinStrings(TIter begin, TIter end, const TWtringBuf delim) {
  68. if (begin == end)
  69. return TUtf16String();
  70. TUtf16String result = ToWtring(*begin);
  71. for (++begin; begin != end; ++begin) {
  72. result.append(delim);
  73. result.append(ToWtring(*begin));
  74. }
  75. return result;
  76. }
  77. /// Concatenates elements of given TVector<TString>.
  78. inline TString JoinStrings(const TVector<TString>& v, const TStringBuf delim) {
  79. return JoinStrings(v.begin(), v.end(), delim);
  80. }
  81. inline TString JoinStrings(const TVector<TString>& v, size_t index, size_t count, const TStringBuf delim) {
  82. Y_ASSERT(index + count <= v.size() && "JoinStrings(): index or count out of range");
  83. return JoinStrings(v.begin() + index, v.begin() + index + count, delim);
  84. }
  85. template <typename T>
  86. inline TString JoinVectorIntoString(const TVector<T>& v, const TStringBuf delim) {
  87. return JoinStrings(v.begin(), v.end(), delim);
  88. }
  89. template <typename T>
  90. inline TString JoinVectorIntoString(const TVector<T>& v, size_t index, size_t count, const TStringBuf delim) {
  91. Y_ASSERT(index + count <= v.size() && "JoinVectorIntoString(): index or count out of range");
  92. return JoinStrings(v.begin() + index, v.begin() + index + count, delim);
  93. }
  94. TUtf16String JoinStrings(const TVector<TUtf16String>& v, const TWtringBuf delim);
  95. TUtf16String JoinStrings(const TVector<TUtf16String>& v, size_t index, size_t count, const TWtringBuf delim);
  96. //! Converts vector of strings to vector of type T variables
  97. template <typename T, typename TStringType>
  98. TVector<T> Scan(const TVector<TStringType>& input) {
  99. TVector<T> output;
  100. output.reserve(input.size());
  101. for (int i = 0; i < input.ysize(); ++i) {
  102. output.push_back(FromString<T>(input[i]));
  103. }
  104. return output;
  105. }