File: split.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (46 lines) | stat: -rw-r--r-- 1,639 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once

namespace nall {

template<bool Insensitive, bool Quoted>
inline auto vector<string>::_split(string_view source, string_view find, long limit) -> type& {
  reset();
  if(limit <= 0 || find.size() == 0) return *this;

  const char* p = source.data();
  s32 size = source.size();
  s32 base = 0;
  s32 matches = 0;

  for(s32 n = 0, quoted = 0; n <= size - (s32)find.size();) {
    if constexpr(Quoted) {
      if(quoted && p[n] == '\\') { n += 2; continue; }
      if(p[n] == '\'' && quoted != 2) { quoted ^= 1; n++; continue; }
      if(p[n] == '\"' && quoted != 1) { quoted ^= 2; n++; continue; }
      if(quoted) { n++; continue; }
    }
    if(string::_compare<Insensitive>(p + n, size - n, find.data(), find.size())) { n++; continue; }
    if(matches >= limit) break;

    string& s = operator()(matches);
    s.resize(n - base);
    memory::copy(s.get(), p + base, n - base);

    n += find.size();
    base = n;
    matches++;
  }

  string& s = operator()(matches);
  s.resize(size - base);
  memory::copy(s.get(), p + base, size - base);

  return *this;
}

inline auto string::split(string_view on, long limit) const -> vector<string> { return vector<string>()._split<0, 0>(*this, on, limit); }
inline auto string::isplit(string_view on, long limit) const -> vector<string> { return vector<string>()._split<1, 0>(*this, on, limit); }
inline auto string::qsplit(string_view on, long limit) const -> vector<string> { return vector<string>()._split<0, 1>(*this, on, limit); }
inline auto string::iqsplit(string_view on, long limit) const -> vector<string> { return vector<string>()._split<1, 1>(*this, on, limit); }

}