summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Exec/.main.cpp.swpbin0 -> 12288 bytes
-rw-r--r--Exec/main.cpp2
-rw-r--r--Src/StringTools.cpp28
-rw-r--r--Test/.String__Split.cpp.swpbin0 -> 20480 bytes
-rw-r--r--Test/CMakeLists.txt1
-rw-r--r--Test/String__Split.cpp156
6 files changed, 178 insertions, 9 deletions
diff --git a/Exec/.main.cpp.swp b/Exec/.main.cpp.swp
new file mode 100644
index 0000000..22cdcce
--- /dev/null
+++ b/Exec/.main.cpp.swp
Binary files differ
diff --git a/Exec/main.cpp b/Exec/main.cpp
index bb337a5..ef95632 100644
--- a/Exec/main.cpp
+++ b/Exec/main.cpp
@@ -4,7 +4,7 @@
int main()
{
std::vector<std::string> foo =
- StringTools::Split("Hello, lol, test", ", ");
+ StringTools::Split(",,Hello,,lol,,test,,", ",,");
for (const auto& it : foo)
std::cout << "'" << it << "'" << std::endl;
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
+ ));
+ }
}
}
diff --git a/Test/.String__Split.cpp.swp b/Test/.String__Split.cpp.swp
new file mode 100644
index 0000000..776cfc7
--- /dev/null
+++ b/Test/.String__Split.cpp.swp
Binary files differ
diff --git a/Test/CMakeLists.txt b/Test/CMakeLists.txt
index d4d3ea1..41ad3c9 100644
--- a/Test/CMakeLists.txt
+++ b/Test/CMakeLists.txt
@@ -15,6 +15,7 @@ add_executable(Test
String__Upper.cpp
String__Replace_Char.cpp
String__Replace_String.cpp
+ String__Split.cpp
# CharTools-Tests
Char__IsVowel.cpp
diff --git a/Test/String__Split.cpp b/Test/String__Split.cpp
new file mode 100644
index 0000000..9da8e94
--- /dev/null
+++ b/Test/String__Split.cpp
@@ -0,0 +1,156 @@
+#include <StringTools.h>
+#include "Catch2.h"
+
+// Tests that splitting an empty string always returns {""}
+TEST_CASE(__FILE__"/EmptyString", "[Strings][Split]")
+{
+ SECTION("Empty seperator") {
+ // Setup
+ const std::string in = "";
+ const std::string sep = "";
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out.size() == 1);
+ REQUIRE(out[0] == "");
+ }
+
+ SECTION("Nonempty seperator") {
+ // Setup
+ const std::string in = "";
+ const std::string sep = ",";
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out.size() == 1);
+ REQUIRE(out[0] == "");
+ }
+
+
+ return;
+}
+
+// Tests that splitting a string with an empty seperator returns all the chars
+TEST_CASE(__FILE__"/EmptySeperator", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = "hello world";
+ const std::string sep = "";
+ const std::vector<std::string> expected = { "h", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+
+// Tests that splitting a string with a single-char seperator works
+TEST_CASE(__FILE__"/SingleCharSeperator", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = "0,1,2,3,4,5,6,7,8,9";
+ const std::string sep = ",";
+ const std::vector<std::string> expected = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+
+// Tests that having seperators next to each other gets empty strings
+TEST_CASE(__FILE__"/SingleCharSeperatorsNextToEachOther", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = "0,1,,3";
+ const std::string sep = ",";
+ const std::vector<std::string> expected = { "0", "1", "", "3" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+
+// Tests that having seperators at index 0 and -1 returns empty strings
+TEST_CASE(__FILE__"/SingleCharSeperatorsAtExtremePoints", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = ",0,1,2,";
+ const std::string sep = ",";
+ const std::vector<std::string> expected = { "", "0", "1", "2", "" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+
+
+// Tests that splitting a string with a multi-char seperator works
+TEST_CASE(__FILE__"/MultiCharSeperator", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = "0;;1;;2;;3;;4;;5;;6;;7;;8;;9";
+ const std::string sep = ";;";
+ const std::vector<std::string> expected = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+
+// Tests that having seperators next to each other gets empty strings
+TEST_CASE(__FILE__"/MultiCharSeperatorsNextToEachOther", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = "0;;1;;;;3";
+ const std::string sep = ";;";
+ const std::vector<std::string> expected = { "0", "1", "", "3" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+
+// Tests that having seperators at index 0 and -1 returns empty strings
+TEST_CASE(__FILE__"/MultiCharSeperatorsAtExtremePoints", "[Strings][Split]")
+{
+ // Setup
+ const std::string in = ";;0;;1;;2;;";
+ const std::string sep = ";;";
+ const std::vector<std::string> expected = { "", "0", "1", "2", "" };
+
+ // Exercise
+ const std::vector<std::string> out = StringTools::Split(in, sep);
+
+ // Verify
+ REQUIRE(out == expected);
+
+ return;
+}
+