#include <string>
#include <vector>
/// declaration
std::vector<std::string> splitstr(
const std::string &s, const std::string &sep = "\t\n\v\r ",
int maxsplit = 0);
//////////////////////////////////////////////////////////////////////////////
/// \brief Non-destructive strtok() replacement
///
/// Splits a string into tokens and returns them as a vector of
/// strings.
std::vector<std::string>
splitstr(
/// string to split
const std::string &s,
/// separators (default is ASCII whitespace)
const std::string &sep,
/// make no more than this many splits (0 = unlimited). For
/// example, if \a maxsplit == 1 the string will be split into no more
/// than two tokens.
int maxsplit
)
{
int
splits = 0;
std::string::size_type
p,
q;
std::vector<std::string>
rv;
for (p = 0; ; splits++) {
p = s.find_first_not_of(sep, p);
if (p == std::string::npos)
break;
if (maxsplit && splits >= maxsplit) {
rv.push_back(s.substr(p));
break;
}
q = s.find_first_of(sep, p);
rv.push_back(s.substr(p, q - p));
p = q;
}
return rv;
}