Add util::split(string, delim)

This commit is contained in:
Nikita Lisitsa 2023-10-01 01:19:31 +03:00
parent 8a437086d6
commit 8c8ede7587

View file

@ -4,6 +4,7 @@
#include <sstream>
#include <stdexcept>
#include <vector>
namespace psemek::util
{
@ -54,4 +55,27 @@ namespace psemek::util
return from_string<T, Char, std::char_traits<Char>>(s);
}
inline std::vector<std::string> split(std::string const & str, char delim)
{
std::vector<std::string> 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;
}
}