From 5fbf07f946995fed76804555a7a5e38f1ef94f38 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Sun, 13 Mar 2022 16:42:40 +0100 Subject: Added tests for string-split, and fixed some bigs --- Src/StringTools.cpp | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'Src/StringTools.cpp') 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 StringTools::Split(const std::string& str, const std::string& seperator) { - std::vector toRet; + std::vector 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 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 + )); + } } } -- cgit v1.2.3