From a3934549633550ea625604c03e25724664048f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 21 Feb 2023 19:40:56 +0100 Subject: Avoid unnecessary copies in splitColonDelimitedString() Avoid creating a stringstream by using find(). --- src/patchelf.cc | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/patchelf.cc b/src/patchelf.cc index 1a67158..946e81c 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -69,14 +70,18 @@ static int forcedPageSize = -1; #define EM_LOONGARCH 258 #endif - -static std::vector splitColonDelimitedString(const char * s) +[[nodiscard]] static std::vector splitColonDelimitedString(std::string_view s) { - std::string item; std::vector parts; - std::stringstream ss(s); - while (std::getline(ss, item, ':')) - parts.push_back(item); + + size_t pos; + while ((pos = s.find(':')) != std::string_view::npos) { + parts.emplace_back(s.substr(0, pos)); + s = s.substr(pos + 1); + } + + if (!s.empty()) + parts.emplace_back(s); return parts; } -- cgit v1.2.1