CoreUtils/PString.cpp
Brad Arant 7e06591de6 Sync.
2021-01-09 18:27:48 -08:00

112 lines
2.7 KiB
C++

#include "PString.h"
#include "Log.h"
namespace coreutils {
PString::PString() {
}
PString::PString(std::string pstring) {
this->pstring = pstring;
}
std::vector<PString> & PString::getList() {
return list;
}
std::string PString::str() {
return pstring.substr(cursor);
}
std::string PString::str(int len) {
return pstring.substr(cursor, len);
}
std::vector<PString> PString::split(std::string delimiter, int maxSize) {
list.clear();
if(pstring.size() == 0) {
list.push_back(PString(""));
return list;
}
size_t end;
size_t pos = cursor;
while(pos < pstring.length()) {
end = pstring.find(delimiter, pos);
if(end == -1)
end = pstring.length();
list.push_back(PString(pstring.substr(pos, end - pos)));
pos = end + delimiter.size();
if(pos > pstring.size())
break;
if(maxSize != 0) {
if(list.size() >= maxSize) {
list.push_back(PString(pstring.substr(pos)));
break;
}
}
}
return list;
}
std::string PString::getTokenInclude(std::string include) {
int start = cursor;
cursor = pstring.find_first_not_of(include, cursor);
return pstring.substr(start, cursor - start);
}
std::string PString::getTokenExclude(std::string exclude) {
int start = cursor;
cursor = pstring.find_first_of(exclude, cursor);
if(cursor == -1)
cursor = pstring.size();
return pstring.substr(start, cursor - start);
}
coreutils::PString PString::operator[](int index) {
return list[index];
}
bool PString::eod() {
return cursor >= pstring.size();
}
bool PString::ifNext(std::string value) {
bool test = (value == pstring.substr(cursor, value.length()));
if(test)
cursor += value.size();
return test;
}
int PString::skipWhitespace() {
size_t temp = cursor;
cursor = pstring.find_first_not_of(" \t", cursor);
cursor = cursor == -1 ? temp: cursor;
return cursor - temp;
}
std::string PString::goeol() {
size_t temp = cursor;
cursor = pstring.find_first_of("\r\n", cursor);
std::string value = pstring.substr(temp, (cursor - temp));
if(pstring[cursor] == '\r')
++cursor;
cursor = cursor == -1 ? temp: ++cursor;
return value;
}
std::string PString::readBlock(size_t size) {
int cursorSave = cursor;
cursor += size;
if(cursor > pstring.size())
cursor = pstring.size();
return pstring.substr(cursorSave, size);
}
int PString::size() {
return pstring.size() - cursor;
}
}