old-nall/string/split.hpp

42 lines
1.4 KiB
C++
Raw Normal View History

2016-05-15 10:42:10 +00:00
#pragma once
namespace nall {
template<bool Insensitive, bool Quoted>
2016-07-09 07:51:02 +00:00
auto string_vector::_split(string_view source, string_view find, long limit) -> string_vector& {
2016-05-15 10:42:10 +00:00
reset();
if(limit <= 0 || find.size() == 0) return *this;
const char* p = source.data();
2016-07-03 11:35:22 +00:00
int size = source.size();
int base = 0;
int matches = 0;
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
for(int n = 0, quoted = 0; n <= size - (int)find.size();) {
2016-05-15 10:42:10 +00:00
if(Quoted) { if(p[n] == '\"') { quoted ^= 1; 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;
}
2016-07-09 07:51:02 +00:00
auto string::split(string_view on, long limit) const -> string_vector { return string_vector()._split<0, 0>(*this, on, limit); }
auto string::isplit(string_view on, long limit) const -> string_vector { return string_vector()._split<1, 0>(*this, on, limit); }
auto string::qsplit(string_view on, long limit) const -> string_vector { return string_vector()._split<0, 1>(*this, on, limit); }
auto string::iqsplit(string_view on, long limit) const -> string_vector { return string_vector()._split<1, 1>(*this, on, limit); }
2016-05-15 10:42:10 +00:00
}