From 5d603cbae259e492b8ff034a748573d3f680e7c7 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 23 Jan 2017 16:50:29 -0800 Subject: [PATCH] Improve Strings::split() --- toolsrc/src/vcpkg_Strings.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/toolsrc/src/vcpkg_Strings.cpp b/toolsrc/src/vcpkg_Strings.cpp index e53cfa1efb..b974b0a063 100644 --- a/toolsrc/src/vcpkg_Strings.cpp +++ b/toolsrc/src/vcpkg_Strings.cpp @@ -125,17 +125,16 @@ namespace vcpkg::Strings std::vector output; size_t i = 0; - size_t pos = s.find(delimiter); - while (pos != std::string::npos) + for (size_t pos = s.find(delimiter); pos != std::string::npos; pos = s.find(delimiter, pos)) { output.push_back(s.substr(i, pos - i)); i = ++pos; - pos = s.find(delimiter, pos); + } - if (pos == std::string::npos && i != s.length()) // The second check is so no items are added if there is nothing after the last delimiter - { - output.push_back(s.substr(i, s.length())); - } + // Add the rest of the string after the last delimiter, unless there is nothing after it + if (i != s.length()) + { + output.push_back(s.substr(i, s.length())); } return output;