From a1deb4476e32c9ef850925f7fc8fd08a62197013 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de> Date: Thu, 2 May 2024 15:48:22 +0200 Subject: [PATCH] correct split function --- Base/Util/StringUtil.cpp | 8 ++++++-- Fit/Tool/StringUtil.cpp | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Base/Util/StringUtil.cpp b/Base/Util/StringUtil.cpp index a7c1ec53934..b3e0ac33ac7 100644 --- a/Base/Util/StringUtil.cpp +++ b/Base/Util/StringUtil.cpp @@ -24,13 +24,17 @@ //! Returns token vector obtained by splitting string at delimiters. std::vector<std::string> Base::String::split(const std::string& text, const std::string& delimiter) { + if (text.empty()) + return {}; std::vector<std::string> result; size_t pos = 0; while (pos != std::string::npos) { 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; + } + result.push_back(text.substr(pos, next_pos - pos)); pos = next_pos + delimiter.length(); } return result; diff --git a/Fit/Tool/StringUtil.cpp b/Fit/Tool/StringUtil.cpp index 400a6be62c7..592afd38a67 100644 --- a/Fit/Tool/StringUtil.cpp +++ b/Fit/Tool/StringUtil.cpp @@ -21,13 +21,17 @@ std::vector<std::string> mumufit::stringUtil::split(const std::string& text, const std::string& delimiter) { + if (text.empty()) + return {}; std::vector<std::string> result; size_t pos = 0; while (pos != std::string::npos) { 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; + } + result.push_back(text.substr(pos, next_pos - pos)); pos = next_pos + delimiter.length(); } return result; -- GitLab