Skip to content

Commit

Permalink
refactor: refactor splitByPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Nov 16, 2024
1 parent 5d29681 commit 6fbe517
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/ll/api/utils/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ namespace ll::inline utils::string_utils {
template <std::invocable<std::string_view> 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>(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>(fn), s);
}
Expand Down

0 comments on commit 6fbe517

Please sign in to comment.