Commit b62956f5 authored by Brad King's avatar Brad King Committed by Kitware Robot
Browse files

Merge topic 'split-string-start-edge'

986519af SystemTools: handle splitting a string starting with the separator
09942f51

 testSystemTools: add tests for SplitString
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !178
parents 95a97253 986519af
...@@ -1904,7 +1904,7 @@ std::vector<std::string> SystemTools::SplitString(const std::string& p, ...@@ -1904,7 +1904,7 @@ std::vector<std::string> SystemTools::SplitString(const std::string& p,
paths.emplace_back("/"); paths.emplace_back("/");
} }
std::string::size_type pos1 = 0; std::string::size_type pos1 = 0;
std::string::size_type pos2 = path.find(sep, pos1 + 1); std::string::size_type pos2 = path.find(sep, pos1);
while (pos2 != std::string::npos) { while (pos2 != std::string::npos) {
paths.push_back(path.substr(pos1, pos2 - pos1)); paths.push_back(path.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1; pos1 = pos2 + 1;
......
...@@ -1120,6 +1120,42 @@ static bool CheckURLParsing() ...@@ -1120,6 +1120,42 @@ static bool CheckURLParsing()
return ret; return ret;
} }
static bool CheckSplitString()
{
bool ret = true;
auto check_split = [](std::string const& input,
std::initializer_list<const char*> expected) -> bool {
auto const components = kwsys::SystemTools::SplitString(input, '/');
if (components.size() != expected.size()) {
std::cerr << "Incorrect split count for " << input << ": "
<< components.size() << std::endl;
return false;
}
size_t i = 0;
for (auto& part : expected) {
if (components[i] != part) {
std::cerr << "Incorrect split component " << i << " for " << input
<< ": " << components[i] << std::endl;
return false;
}
++i;
}
return true;
};
// No separators
ret &= check_split("nosep", { "nosep" });
// Simple
ret &= check_split("first/second", { "first", "second" });
// Separator at beginning
ret &= check_split("/starts/sep", { "", "starts", "sep" });
// Separator at end
ret &= check_split("ends/sep/", { "ends", "sep", "" });
return ret;
}
int testSystemTools(int, char* []) int testSystemTools(int, char* [])
{ {
bool res = true; bool res = true;
...@@ -1169,5 +1205,7 @@ int testSystemTools(int, char* []) ...@@ -1169,5 +1205,7 @@ int testSystemTools(int, char* [])
res &= CheckURLParsing(); res &= CheckURLParsing();
res &= CheckSplitString();
return res ? 0 : 1; return res ? 0 : 1;
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment