diff --git a/src/util.cpp b/src/util.cpp new file mode 100644 index 0000000..b51e3b0 --- /dev/null +++ b/src/util.cpp @@ -0,0 +1,47 @@ +#include "util.h" + +vector findAll(const string& data, const string& toSearch){ + vector indices; + + size_t pos = data.find(toSearch); + + while (pos != string::npos){ + indices.push_back(pos); + + pos = data.find(toSearch, pos + toSearch.size()); + } + + return indices; +} + +void removeAll(string& data, const string& toRemove) { + vector indices = findAll(data, toRemove); + std::sort(indices.rbegin(), indices.rend()); + for (size_t &i : indices) + data.erase(i, toRemove.size()); +} + +vector split(const string& data, const char c){ + vector parts; + size_t start = 0; + for (size_t i : findAll(data, std::string(1, c))){ + parts.push_back(data.substr(start, i - start)); + start = i + 1; + } + parts.push_back(data.substr(start)); + return parts; +} + +bool isDigit(char c, uint8_t& result){ + auto val = c - '0'; + if (val >= 0 && val < 10){ + result = val; + return true; + } + return false; +} + +bool isDigit(char c){ + auto val = c - '0'; + return val >= 0 && val < 10; +} \ No newline at end of file diff --git a/src/util.h b/src/util.h index 20c1636..c64eec6 100644 --- a/src/util.h +++ b/src/util.h @@ -20,37 +20,12 @@ using std::string, std::vector, std::pair, std::map; using std::list, std::set, std::unordered_set; using std::priority_queue; -inline vector findAll(const string& data, const string& toSearch){ - vector indices; - - size_t pos = data.find(toSearch); - - while (pos != string::npos){ - indices.push_back(pos); - - pos = data.find(toSearch, pos + toSearch.size()); - } - - return indices; -} - -inline void removeAll(string& data, const string& toRemove) { - vector indices = findAll(data, toRemove); - std::sort(indices.rbegin(), indices.rend()); - for (size_t &i : indices) - data.erase(i, toRemove.size()); -} - -inline bool isDigit(char c, uint8_t& result){ - auto val = c - '0'; - if (val >= 0 && val < 10){ - result = val; - return true; - } - return false; -} - -inline bool isDigit(char c){ - auto val = c - '0'; - return val >= 0 && val < 10; -} \ No newline at end of file +vector findAll(const string& data, const string& toSearch); + +void removeAll(string& data, const string& toRemove); + +vector split(const string& data, char c); + +bool isDigit(char c, uint8_t& result); + +bool isDigit(char c); \ No newline at end of file