diff --git a/SystemTools.cxx b/SystemTools.cxx index 8ec30580975b40e63adddfe07fd6c69ff003d778..3a6ceec92d388ea3a42009b7c0085803db8d2c81 100644 --- a/SystemTools.cxx +++ b/SystemTools.cxx @@ -1904,7 +1904,7 @@ std::vector<std::string> SystemTools::SplitString(const std::string& p, paths.emplace_back("/"); } 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) { paths.push_back(path.substr(pos1, pos2 - pos1)); pos1 = pos2 + 1; diff --git a/testSystemTools.cxx b/testSystemTools.cxx index aa38aec4fd571dfcdde53e2470078687f16ed07d..8909b49782b8e54a309bc29a7a1a87b2e5ce1111 100644 --- a/testSystemTools.cxx +++ b/testSystemTools.cxx @@ -1120,6 +1120,42 @@ static bool CheckURLParsing() 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* []) { bool res = true; @@ -1169,5 +1205,7 @@ int testSystemTools(int, char* []) res &= CheckURLParsing(); + res &= CheckSplitString(); + return res ? 0 : 1; }