Added tests for string-split, and fixed some bigs

This commit is contained in:
Leonetienne
2022-03-13 16:42:40 +01:00
parent 25bd269729
commit 5fbf07f946
6 changed files with 178 additions and 9 deletions

View File

@@ -94,10 +94,14 @@ std::string StringTools::Upper(const std::string& str) {
}
std::vector<std::string> StringTools::Split(const std::string& str, const std::string& seperator) {
std::vector<std::string> toRet;
std::vector<std::string> toRet;
// Quick-accept: str length is 0
if (str.length() == 0)
toRet.push_back("");
// Quick-accept: seperator length is 0
if (seperator.length() == 0) {
else if (seperator.length() == 0) {
for (const char c : str)
toRet.push_back(std::string(&c, (&c) + 1));
}
@@ -106,15 +110,23 @@ std::vector<std::string> StringTools::Split(const std::string& str, const std::s
std::size_t idx = 0;
while (idx != std::string::npos) {
std::size_t lastIdx = idx;
idx = str.find(seperator, idx + seperator.length());
idx = str.find(seperator, idx);
toRet.push_back(str.substr(
lastIdx,
idx - lastIdx
));
// Grab our substring until the next finding of sep
if (idx != std::string::npos) {
toRet.push_back(str.substr(
lastIdx,
idx - lastIdx
));
if (idx != std::string::npos)
idx += seperator.length();
}
// No more seperator found. Grab the rest until the end of the string
else {
toRet.push_back(str.substr(
lastIdx
));
}
}
}