Skip to content
Snippets Groups Projects
Commit a1deb447 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

correct split function

parent 53c25f35
No related branches found
No related tags found
1 merge request!2534rm some uses of Boost
Pipeline #141220 passed
...@@ -24,13 +24,17 @@ ...@@ -24,13 +24,17 @@
//! Returns token vector obtained by splitting string at delimiters. //! Returns token vector obtained by splitting string at delimiters.
std::vector<std::string> Base::String::split(const std::string& text, const std::string& delimiter) std::vector<std::string> Base::String::split(const std::string& text, const std::string& delimiter)
{ {
if (text.empty())
return {};
std::vector<std::string> result; std::vector<std::string> result;
size_t pos = 0; size_t pos = 0;
while (pos != std::string::npos) { while (pos != std::string::npos) {
size_t next_pos = text.find(delimiter, pos); size_t next_pos = text.find(delimiter, pos);
result.push_back(text.substr(pos, next_pos - pos)); if (next_pos == std::string::npos) {
if (next_pos == std::string::npos) result.push_back(text.substr(pos));
break; break;
}
result.push_back(text.substr(pos, next_pos - pos));
pos = next_pos + delimiter.length(); pos = next_pos + delimiter.length();
} }
return result; return result;
......
...@@ -21,13 +21,17 @@ ...@@ -21,13 +21,17 @@
std::vector<std::string> mumufit::stringUtil::split(const std::string& text, std::vector<std::string> mumufit::stringUtil::split(const std::string& text,
const std::string& delimiter) const std::string& delimiter)
{ {
if (text.empty())
return {};
std::vector<std::string> result; std::vector<std::string> result;
size_t pos = 0; size_t pos = 0;
while (pos != std::string::npos) { while (pos != std::string::npos) {
size_t next_pos = text.find(delimiter, pos); size_t next_pos = text.find(delimiter, pos);
result.push_back(text.substr(pos, next_pos - pos)); if (next_pos == std::string::npos) {
if (next_pos == std::string::npos) result.push_back(text.substr(pos));
break; break;
}
result.push_back(text.substr(pos, next_pos - pos));
pos = next_pos + delimiter.length(); pos = next_pos + delimiter.length();
} }
return result; return result;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment