diff options
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
+ ));
+ }
}
}
|
