diff --git a/include/util.h b/include/util.h index b943b3d..dfd085c 100644 --- a/include/util.h +++ b/include/util.h @@ -44,6 +44,7 @@ namespace Util std::string getHomeDir(); std::string getConfigHome(); std::string getCacheHome(); + std::vector tokenize(const std::string& str, const std::string& separator = ","); } #endif // UTIL_H diff --git a/main.cpp b/main.cpp index 3e31fb0..1755be4 100644 --- a/main.cpp +++ b/main.cpp @@ -29,14 +29,11 @@ template void set_vm_value(std::map &priority, unsigned int &type) { - size_t idx = 0, found; - - while ((found = priority_string.find(',', idx)) != std::string::npos) - { - priority.push_back(std::stoi(priority_string.substr(idx, found - idx))); - idx = found + 1; - } - priority.push_back(std::stoi(priority_string.substr(idx))); + std::vector tokens = Util::tokenize(priority_string, ","); + for (std::vector::iterator it = tokens.begin(); it != tokens.end(); it++) + { + priority.push_back(std::stoi(*it)); + } unsigned int wanted = 0; #ifdef DEBUG diff --git a/src/util.cpp b/src/util.cpp index 5a2abdf..f16400d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -405,3 +405,22 @@ std::string Util::getCacheHome() cacheHome = Util::getHomeDir() + "/.cache"; return cacheHome; } + +std::vector Util::tokenize(const std::string& str, const std::string& separator) +{ + std::vector tokens; + std::string token; + size_t idx = 0, found; + while ((found = str.find(separator, idx)) != std::string::npos) + { + token = str.substr(idx, found - idx); + if (!token.empty()) + tokens.push_back(token); + idx = found + separator.length(); + } + token = str.substr(idx); + if (!token.empty()) + tokens.push_back(token); + + return tokens; +}