From 6fbe517729f83263119c5502fe8d3508ea678c69 Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Sat, 16 Nov 2024 21:33:55 +0800 Subject: [PATCH] refactor: refactor splitByPattern --- src/ll/api/utils/StringUtils.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ll/api/utils/StringUtils.h b/src/ll/api/utils/StringUtils.h index ff0d6e8797..e0d3898a0b 100644 --- a/src/ll/api/utils/StringUtils.h +++ b/src/ll/api/utils/StringUtils.h @@ -25,14 +25,12 @@ namespace ll::inline utils::string_utils { template Fn> constexpr void splitByPattern(Fn&& fn, std::string_view s, std::string_view pattern, bool keepEmpty = false) { if (s.empty()) return; - size_t pos = s.find(pattern); - size_t size = s.size(); - while (pos != std::string::npos) { + size_t pos{}; + while ((pos = s.find(pattern)) != std::string::npos) { if (keepEmpty || pos != 0) { if (!std::invoke(std::forward(fn), s.substr(0, pos))) return; } - s = s.substr(pos + pattern.size(), size - pos - pattern.size()); - pos = s.find(pattern); + s = s.substr(pos + pattern.size()); } if (keepEmpty || !s.empty()) std::invoke(std::forward(fn), s); }