diff options
| author | Leonetienne <leonetienne@hotmail.de> | 2022-03-13 16:42:40 +0100 |
|---|---|---|
| committer | Leonetienne <leonetienne@hotmail.de> | 2022-03-13 16:42:40 +0100 |
| commit | 5fbf07f946995fed76804555a7a5e38f1ef94f38 (patch) | |
| tree | 7b63a9845580a70d0013a0b036d7073aa6176b5a /Src | |
| parent | 25bd26972945f956ad4c92ce8a61bb6126c0dedb (diff) | |
Added tests for string-split, and fixed some bigs
Diffstat (limited to 'Src')
| -rw-r--r-- | Src/StringTools.cpp | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/Src/StringTools.cpp b/Src/StringTools.cpp index 302fad9..346e1bd 100644 --- a/Src/StringTools.cpp +++ b/Src/StringTools.cpp @@ -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
+ ));
+ }
}
}
|
