Now supports string32 splitting

This commit is contained in:
Xnoe 2020-07-10 05:51:07 +01:00 committed by Elsie
parent b502cd28f3
commit 656185b113

36
utf8.h
View File

@ -9,6 +9,8 @@
class char32 { class char32 {
uint32_t fetch32(char** cstr) { uint32_t fetch32(char** cstr) {
uint32_t r(0); uint32_t r(0);
if (!**cstr)
return r;
int i(1); int i(1);
unsigned char compare = (unsigned char)**cstr; unsigned char compare = (unsigned char)**cstr;
if (compare >> 3 == 0b11110) i = 4; if (compare >> 3 == 0b11110) i = 4;
@ -49,12 +51,19 @@ struct string32 {
while (sd[0]) while (sd[0])
cs.push_back(char32(&sd)); cs.push_back(char32(&sd));
} }
string32() {}
int size() const { int size() const {
return cs.size(); return cs.size();
} }
char32 operator[](int i) const { char32 operator[](int i) const {
return cs[i]; return cs[i];
} }
void operator+=(string32 s) {
cs.insert(cs.end(), s.cs.begin(), s.cs.end());
}
void operator+=(char32 c) {
cs.push_back(c);
}
string32 replace(string32 find, string32 with) { string32 replace(string32 find, string32 with) {
string32 copyOfSelf = *this; string32 copyOfSelf = *this;
int havematched(0); int havematched(0);
@ -73,6 +82,33 @@ struct string32 {
} }
return copyOfSelf; return copyOfSelf;
} }
std::vector<string32> split(string32 delim) {
std::vector<string32> toReturn;
string32 temporary, throwaway;
int havematched(0);
int findsize = delim.cs.size();
for (int index(0); index < cs.size(); index++) {
if (cs.at(index) == delim.cs.at(havematched)) {
throwaway += cs[index];
havematched++;
} else {
temporary += throwaway;
throwaway = "";
temporary += cs[index];
havematched=0;
}
if (havematched == findsize) {
toReturn.push_back(temporary);
temporary = "";
throwaway = "";
havematched = 0;
}
}
if (temporary.size()>0)
toReturn.push_back(temporary);
return toReturn;
}
}; };
std::ostream& operator<<(std::ostream& stream, const string32& s32) { std::ostream& operator<<(std::ostream& stream, const string32& s32) {