From cb90758109c5dea6be507c42007334f7cf76b1ab Mon Sep 17 00:00:00 2001 From: Lemmy Date: Sun, 10 May 2026 20:21:11 -0400 Subject: [PATCH] string_utils: trimLeftView and trimRightView --- src/util/string_utils.h | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/util/string_utils.h b/src/util/string_utils.h index 9b94c2e1e..5c804aa8c 100644 --- a/src/util/string_utils.h +++ b/src/util/string_utils.h @@ -7,18 +7,22 @@ namespace StringUtils { - [[nodiscard]] inline std::string trim(std::string_view s) { - std::size_t start = 0; - while (start < s.size() && std::isspace(static_cast(s[start])) != 0) { - ++start; + [[nodiscard]] inline std::string_view trimLeftView(std::string_view s) { + while (!s.empty() && std::isspace(static_cast(s.front())) != 0) { + s.remove_prefix(1); } - std::size_t end = s.size(); - while (end > start && std::isspace(static_cast(s[end - 1])) != 0) { - --end; - } - return std::string(s.substr(start, end - start)); + return s; } + [[nodiscard]] inline std::string_view trimRightView(std::string_view s) { + while (!s.empty() && std::isspace(static_cast(s.back())) != 0) { + s.remove_suffix(1); + } + return s; + } + + [[nodiscard]] inline std::string trim(std::string_view s) { return std::string(trimRightView(trimLeftView(s))); } + [[nodiscard]] inline std::string toLower(std::string_view s) { std::string out(s); std::transform(out.begin(), out.end(), out.begin(),