#include "utils.hpp" namespace common { namespace utils { std::vector str_split(std::string_view s, char delimiter, size_t times) { std::vector to_return; decltype(s.size()) start = 0, finish = 0; while ((finish = s.find_first_of(delimiter, start)) != std::string_view::npos) { to_return.emplace_back(s.substr(start, finish - start)); start = finish + 1; if (to_return.size() == times) { break; } } to_return.emplace_back(s.substr(start)); return to_return; } bool str_replace_in_place(std::string& subject, std::string_view search, const std::string& replace) { if (search.empty()) return false; bool had_effect = false; size_t pos = 0; while ((pos = subject.find(search, pos)) != std::string::npos) { subject.replace(pos, search.length(), replace); pos += replace.length(); had_effect = true; } return had_effect; } std::string_view max_words(std::string_view text, unsigned count) { unsigned word_len = 0, word_count = 0, idx; // Get idx after last word for (idx = 0; idx != text.size() && word_count != count; idx++) { char c = text[idx]; if (c == ' ' || word_len == 8) { if (word_len != 0) { word_count++; word_len = 0; } } else { word_len++; } } // Return resulting string return {text.data(), idx}; } bool contains(std::string_view value, std::string_view ending) { return value.find(ending) != value.npos; } bool starts_with(std::string_view value, std::string_view ending) { return value.find(ending) == 0; } bool ends_with(std::string_view value, std::string_view ending) { if (ending.size() > value.size()) return false; return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); } bool chop_down(std::string& s, char delim) { const auto pos = s.find(delim); if (pos == s.npos) return false; s.erase(pos); return true; } std::string remove_nonprintable(std::string_view str) { std::string fres; for (const char c : str) { if (std::isprint(c)) fres.push_back(c); } return fres; } } }