You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.0 KiB
36 lines
1.0 KiB
#include <util.hpp>
|
|
|
|
std::vector<std::string> util::split(const std::string input, const std::string::value_type separator, size_t max_tokens) {
|
|
std::vector<std::string> tokens;
|
|
|
|
for(auto index = 0, last = -1; index != max_tokens; ++index) {
|
|
auto current = input.find(separator, last + 1);
|
|
auto eol = current == std::string::npos;
|
|
|
|
if(eol || index == max_tokens) {
|
|
current = input.length();
|
|
}
|
|
|
|
auto length = current - last - 1;
|
|
auto token = input.substr(last + 1, length);
|
|
tokens.push_back(token);
|
|
last = current;
|
|
|
|
if(eol) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
return tokens;
|
|
}
|
|
|
|
std::string util::join(const std::vector<std::string> strings, const std::string::value_type separator) {
|
|
std::string output;
|
|
for(size_t index = 0; index < strings.size(); ++index) {
|
|
output.append(strings.at(index));
|
|
if(index + 1 < strings.size()) {
|
|
output.push_back(separator);
|
|
}
|
|
}
|
|
return output;
|
|
} |