From ea2d007f6036baf323014231209d790a6f9d2951 Mon Sep 17 00:00:00 2001 From: Christian Berger Date: Fri, 2 Aug 2019 22:12:18 +0200 Subject: [PATCH] * allowing for empty fields Signed-off-by: Christian Berger --- stringtoolbox.hpp | 6 ++++++ test/Test-stringtoolbox.cpp | 31 ++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/stringtoolbox.hpp b/stringtoolbox.hpp index a3ab2ad..dfdeb81 100644 --- a/stringtoolbox.hpp +++ b/stringtoolbox.hpp @@ -78,10 +78,16 @@ inline std::vector split(const std::string &str, if (i != prev) { retVal.emplace_back(str.substr(prev, i - prev)); } + else { + retVal.emplace_back(""); + } } if ((prev > 0) && (prev < str.size())) { retVal.emplace_back(str.substr(prev, str.size() - prev)); } + else if (prev > 0) { + retVal.emplace_back(""); + } return retVal; } diff --git a/test/Test-stringtoolbox.cpp b/test/Test-stringtoolbox.cpp index fdc19d1..58c82ef 100644 --- a/test/Test-stringtoolbox.cpp +++ b/test/Test-stringtoolbox.cpp @@ -115,19 +115,36 @@ TEST_CASE("Test split") { std::string s3 = ";abc;def"; std::vector vs3 = stringtoolbox::split(s3, ';'); - REQUIRE(vs3.size() == 2); - REQUIRE(vs3.at(0) == "abc"); - REQUIRE(vs3.at(1) == "def"); + REQUIRE(vs3.size() == 3); + REQUIRE(vs3.at(1) == "abc"); + REQUIRE(vs3.at(2) == "def"); std::string s4 = "abc;def;"; std::vector vs4 = stringtoolbox::split(s4, ';'); - REQUIRE(vs4.size() == 2); + REQUIRE(vs4.size() == 3); REQUIRE(vs4.at(0) == "abc"); REQUIRE(vs4.at(1) == "def"); std::string s5 = ";abc;def;"; std::vector vs5 = stringtoolbox::split(s5, ';'); - REQUIRE(vs5.size() == 2); - REQUIRE(vs5.at(0) == "abc"); - REQUIRE(vs5.at(1) == "def"); + REQUIRE(vs5.size() == 4); + REQUIRE(vs5.at(1) == "abc"); + REQUIRE(vs5.at(2) == "def"); + + std::string s6 = ";abc;;def;"; + std::vector vs6 = stringtoolbox::split(s6, ';'); + REQUIRE(vs6.size() == 5); + REQUIRE(vs6.at(1) == "abc"); + REQUIRE(vs6.at(3) == "def"); +} + +TEST_CASE("Empty field") { + std::string s1 = "1;0;;57.71941;11.95701"; + std::vector vs1 = stringtoolbox::split(s1, ';'); + REQUIRE(vs1.size() == 5); + REQUIRE(vs1.at(0) == "1"); + REQUIRE(vs1.at(1) == "0"); + REQUIRE(vs1.at(2) == ""); + REQUIRE(vs1.at(3) == "57.71941"); + REQUIRE(vs1.at(4) == "11.95701"); }