mirror of
https://gitlab.com/niansa/commoncpp.git
synced 2025-03-06 20:48:30 +01:00
Added str_split_once accepting char delimiter
This commit is contained in:
parent
6a6c0c7c49
commit
41f58e36cf
2 changed files with 11 additions and 0 deletions
|
@ -14,6 +14,7 @@ namespace utils {
|
|||
std::vector<std::string_view> str_split(std::string_view s, char delimiter, size_t times = -1);
|
||||
#endif
|
||||
std::pair<std::string_view, std::string_view> str_split_once(std::string_view s, std::string_view delim);
|
||||
std::pair<std::string_view, std::string_view> str_split_once(std::string_view s, char delim);
|
||||
bool str_replace_in_place(std::string& subject, std::string_view search, const std::string& replace);
|
||||
std::string_view max_words(std::string_view text, unsigned count);
|
||||
bool contains(std::string_view value, std::string_view other);
|
||||
|
|
10
utils.cpp
10
utils.cpp
|
@ -34,6 +34,15 @@ std::pair<std::string_view, std::string_view> str_split_once(std::string_view s,
|
|||
// Split there
|
||||
return {s.substr(0, pos), s.substr(pos+delim.size(), s.size()-1)};
|
||||
}
|
||||
std::pair<std::string_view, std::string_view> str_split_once(std::string_view s, char delim) {
|
||||
// Find the delimiter
|
||||
auto pos = s.find(delim);
|
||||
if (pos == s.npos) {
|
||||
return {s, ""};
|
||||
}
|
||||
// Split there
|
||||
return {s.substr(0, pos), s.substr(pos+1, s.size()-1)};
|
||||
}
|
||||
|
||||
bool str_replace_in_place(std::string& subject, std::string_view search,
|
||||
const std::string& replace) {
|
||||
|
@ -110,6 +119,7 @@ std::string read_text_file(const std::string& path) {
|
|||
sstr << in.rdbuf();
|
||||
return sstr.str();
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue