Add util::split(string, delim)
This commit is contained in:
parent
8a437086d6
commit
8c8ede7587
1 changed files with 24 additions and 0 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue