From 8c8ede75873f363e1aa8d7816004a1cb2f17abd5 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 1 Oct 2023 01:19:31 +0300 Subject: [PATCH] Add util::split(string, delim) --- libs/util/include/psemek/util/to_string.hpp | 24 +++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/libs/util/include/psemek/util/to_string.hpp b/libs/util/include/psemek/util/to_string.hpp index 2e8a1161..7b3eec7d 100644 --- a/libs/util/include/psemek/util/to_string.hpp +++ b/libs/util/include/psemek/util/to_string.hpp @@ -4,6 +4,7 @@ #include #include +#include namespace psemek::util { @@ -54,4 +55,27 @@ namespace psemek::util return from_string>(s); } + inline std::vector split(std::string const & str, char delim) + { + std::vector result; + + for (std::size_t pos = 0;;) + { + auto next = str.find(delim, pos); + + if (next == std::string::npos) + { + result.push_back(str.substr(pos)); + break; + } + else + { + result.push_back(str.substr(pos, next - pos)); + pos = next + 1; + } + } + + return result; + } + }