More uses of string_view

This commit is contained in:
Henrik Rydgård 2023-11-13 23:36:34 +01:00
parent d0ee5fc308
commit 19eeaef2ea
11 changed files with 53 additions and 45 deletions

View file

@ -284,6 +284,23 @@ std::string_view StripQuotes(std::string_view s) {
return s;
}
void SplitString(std::string_view str, const char delim, std::vector<std::string_view> &output) {
size_t next = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
if (str[pos] == delim) {
output.emplace_back(str.substr(next, pos - next));
// Skip the delimiter itself.
next = pos + 1;
}
}
if (next == 0) {
output.push_back(str);
} else if (next < str.length()) {
output.emplace_back(str.substr(next));
}
}
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output) {
size_t next = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
@ -295,7 +312,7 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
}
if (next == 0) {
output.push_back(std::string(str));
output.emplace_back(str);
} else if (next < str.length()) {
output.emplace_back(str.substr(next));
}
@ -320,8 +337,7 @@ static std::string ApplyHtmlEscapes(std::string str) {
}
// Meant for HTML listings and similar, so supports some HTML escapes.
void GetQuotedStrings(const std::string& str, std::vector<std::string>& output)
{
void GetQuotedStrings(const std::string& str, std::vector<std::string> &output) {
size_t next = 0;
bool even = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
@ -340,15 +356,13 @@ void GetQuotedStrings(const std::string& str, std::vector<std::string>& output)
}
}
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest)
{
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest) {
size_t pos = 0;
if (src == dest)
return result;
while (1)
{
while (true) {
pos = result.find(src, pos);
if (pos == result.npos)
break;