Skip to content

Commit ea2d007

Browse files
committed
* allowing for empty fields
Signed-off-by: Christian Berger <christian.berger@gu.se>
1 parent e70f305 commit ea2d007

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

stringtoolbox.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,16 @@ inline std::vector<std::string> split(const std::string &str,
7878
if (i != prev) {
7979
retVal.emplace_back(str.substr(prev, i - prev));
8080
}
81+
else {
82+
retVal.emplace_back("");
83+
}
8184
}
8285
if ((prev > 0) && (prev < str.size())) {
8386
retVal.emplace_back(str.substr(prev, str.size() - prev));
8487
}
88+
else if (prev > 0) {
89+
retVal.emplace_back("");
90+
}
8591
return retVal;
8692
}
8793

test/Test-stringtoolbox.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,36 @@ TEST_CASE("Test split") {
115115

116116
std::string s3 = ";abc;def";
117117
std::vector<std::string> vs3 = stringtoolbox::split(s3, ';');
118-
REQUIRE(vs3.size() == 2);
119-
REQUIRE(vs3.at(0) == "abc");
120-
REQUIRE(vs3.at(1) == "def");
118+
REQUIRE(vs3.size() == 3);
119+
REQUIRE(vs3.at(1) == "abc");
120+
REQUIRE(vs3.at(2) == "def");
121121

122122
std::string s4 = "abc;def;";
123123
std::vector<std::string> vs4 = stringtoolbox::split(s4, ';');
124-
REQUIRE(vs4.size() == 2);
124+
REQUIRE(vs4.size() == 3);
125125
REQUIRE(vs4.at(0) == "abc");
126126
REQUIRE(vs4.at(1) == "def");
127127

128128
std::string s5 = ";abc;def;";
129129
std::vector<std::string> vs5 = stringtoolbox::split(s5, ';');
130-
REQUIRE(vs5.size() == 2);
131-
REQUIRE(vs5.at(0) == "abc");
132-
REQUIRE(vs5.at(1) == "def");
130+
REQUIRE(vs5.size() == 4);
131+
REQUIRE(vs5.at(1) == "abc");
132+
REQUIRE(vs5.at(2) == "def");
133+
134+
std::string s6 = ";abc;;def;";
135+
std::vector<std::string> vs6 = stringtoolbox::split(s6, ';');
136+
REQUIRE(vs6.size() == 5);
137+
REQUIRE(vs6.at(1) == "abc");
138+
REQUIRE(vs6.at(3) == "def");
139+
}
140+
141+
TEST_CASE("Empty field") {
142+
std::string s1 = "1;0;;57.71941;11.95701";
143+
std::vector<std::string> vs1 = stringtoolbox::split(s1, ';');
144+
REQUIRE(vs1.size() == 5);
145+
REQUIRE(vs1.at(0) == "1");
146+
REQUIRE(vs1.at(1) == "0");
147+
REQUIRE(vs1.at(2) == "");
148+
REQUIRE(vs1.at(3) == "57.71941");
149+
REQUIRE(vs1.at(4) == "11.95701");
133150
}

0 commit comments

Comments
 (0)