1
0
Fork 0
mirror of https://gitlab.com/niansa/commoncpp.git synced 2025-03-06 20:48:30 +01:00
commoncpp/utils.cpp
2023-06-10 13:35:05 +02:00

54 lines
1.6 KiB
C++

#include "utils.hpp"
namespace common {
namespace utils {
std::vector<std::string_view> str_split(std::string_view s, char delimiter, size_t times) {
std::vector<std::string_view> 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;
}
void str_replace_in_place(std::string& subject, std::string_view search,
const std::string& replace) {
if (search.empty()) return;
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
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 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());
}
}
}