vector.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "util.h"
  2. #include "split.h"
  3. #include "vector.h"
  4. #include <util/system/defaults.h>
  5. template <class TConsumer, class TDelim, typename TChr>
  6. static inline void DoSplit2(TConsumer& c, TDelim& d, const TBasicStringBuf<TChr> str, int) {
  7. SplitString(str.data(), str.data() + str.size(), d, c);
  8. }
  9. template <class TConsumer, class TDelim, typename TChr>
  10. static inline void DoSplit1(TConsumer& cc, TDelim& d, const TBasicStringBuf<TChr> str, int opts) {
  11. if (opts & KEEP_EMPTY_TOKENS) {
  12. DoSplit2(cc, d, str, opts);
  13. } else {
  14. TSkipEmptyTokens<TConsumer> sc(&cc);
  15. DoSplit2(sc, d, str, opts);
  16. }
  17. }
  18. template <class C, class TDelim, typename TChr>
  19. static inline void DoSplit0(C* res, const TBasicStringBuf<TChr> str, TDelim& d, size_t maxFields, int options) {
  20. using TStringType = std::conditional_t<std::is_same<TChr, wchar16>::value, TUtf16String, TString>;
  21. res->clear();
  22. if (!str.data()) {
  23. return;
  24. }
  25. using TConsumer = TContainerConsumer<C>;
  26. TConsumer cc(res);
  27. if (maxFields) {
  28. TLimitingConsumer<TConsumer, const TChr> lc(maxFields, &cc);
  29. DoSplit1(lc, d, str, options);
  30. if (lc.Last) {
  31. res->push_back(TStringType(lc.Last, str.data() + str.size() - lc.Last));
  32. }
  33. } else {
  34. DoSplit1(cc, d, str, options);
  35. }
  36. }
  37. template <typename TChr>
  38. static void SplitStringImplT(TVector<std::conditional_t<std::is_same<TChr, wchar16>::value, TUtf16String, TString>>* res,
  39. const TBasicStringBuf<TChr> str, const TChr* delim, size_t maxFields, int options) {
  40. if (!*delim) {
  41. return;
  42. }
  43. if (*(delim + 1)) {
  44. TStringDelimiter<const TChr> d(delim, std::char_traits<TChr>::length(delim));
  45. DoSplit0(res, str, d, maxFields, options);
  46. } else {
  47. TCharDelimiter<const TChr> d(*delim);
  48. DoSplit0(res, str, d, maxFields, options);
  49. }
  50. }
  51. void ::NPrivate::SplitStringImpl(TVector<TString>* res, const char* ptr, const char* delim, size_t maxFields, int options) {
  52. return SplitStringImplT<char>(res, TStringBuf(ptr), delim, maxFields, options);
  53. }
  54. void ::NPrivate::SplitStringImpl(TVector<TString>* res, const char* ptr, size_t len, const char* delim, size_t maxFields, int options) {
  55. return SplitStringImplT<char>(res, TStringBuf(ptr, len), delim, maxFields, options);
  56. }
  57. void ::NPrivate::SplitStringImpl(TVector<TUtf16String>* res, const wchar16* ptr, const wchar16* delimiter, size_t maxFields, int options) {
  58. return SplitStringImplT<wchar16>(res, TWtringBuf(ptr), delimiter, maxFields, options);
  59. }
  60. void ::NPrivate::SplitStringImpl(TVector<TUtf16String>* res, const wchar16* ptr, size_t len, const wchar16* delimiter, size_t maxFields, int options) {
  61. return SplitStringImplT<wchar16>(res, TWtringBuf(ptr, len), delimiter, maxFields, options);
  62. }
  63. TUtf16String JoinStrings(const TVector<TUtf16String>& v, const TWtringBuf delim) {
  64. return JoinStrings(v.begin(), v.end(), delim);
  65. }
  66. TUtf16String JoinStrings(const TVector<TUtf16String>& v, size_t index, size_t count, const TWtringBuf delim) {
  67. const size_t f = Min(index, v.size());
  68. const size_t l = f + Min(count, v.size() - f);
  69. return JoinStrings(v.begin() + f, v.begin() + l, delim);
  70. }