split.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. namespace nall {
  3. template<bool Insensitive, bool Quoted>
  4. auto vector<string>::_split(string_view source, string_view find, long limit) -> type& {
  5. reset();
  6. if(limit <= 0 || find.size() == 0) return *this;
  7. const char* p = source.data();
  8. int size = source.size();
  9. int base = 0;
  10. int matches = 0;
  11. for(int n = 0, quoted = 0; n <= size - (int)find.size();) {
  12. if(Quoted) { if(p[n] == '\"') { quoted ^= 1; n++; continue; } if(quoted) { n++; continue; } }
  13. if(string::_compare<Insensitive>(p + n, size - n, find.data(), find.size())) { n++; continue; }
  14. if(matches >= limit) break;
  15. string& s = operator()(matches);
  16. s.resize(n - base);
  17. memory::copy(s.get(), p + base, n - base);
  18. n += find.size();
  19. base = n;
  20. matches++;
  21. }
  22. string& s = operator()(matches);
  23. s.resize(size - base);
  24. memory::copy(s.get(), p + base, size - base);
  25. return *this;
  26. }
  27. auto string::split(string_view on, long limit) const -> vector<string> { return vector<string>()._split<0, 0>(*this, on, limit); }
  28. auto string::isplit(string_view on, long limit) const -> vector<string> { return vector<string>()._split<1, 0>(*this, on, limit); }
  29. auto string::qsplit(string_view on, long limit) const -> vector<string> { return vector<string>()._split<0, 1>(*this, on, limit); }
  30. auto string::iqsplit(string_view on, long limit) const -> vector<string> { return vector<string>()._split<1, 1>(*this, on, limit); }
  31. }