feat use string_view
All checks were successful
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (push) Successful in 1m57s
linux-arm-gcc / linux-gcc-armhf (push) Successful in 2m27s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 2m23s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 2m30s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 2m29s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 2m29s

This commit is contained in:
tqcq 2024-04-14 15:46:50 +08:00
parent 26c643d026
commit b6e7c12aee
2 changed files with 61 additions and 2 deletions

View File

@ -32,6 +32,19 @@ ToUpper(sled::string_view str)
return ss.str();
}
std::string
ToHex(sled::string_view str)
{
constexpr static char hex_chars[] = "0123456789ABCDEF";
std::string result;
result.reserve(str.size() * 2);
for (auto &ch : str) {
result.push_back(hex_chars[ch >> 4]);
result.push_back(hex_chars[ch & 0x0F]);
}
return result;
}
std::string
StrJoin(const std::vector<std::string> &strings, const std::string &delim, bool skip_empty)
{
@ -48,6 +61,47 @@ StrJoin(const std::vector<std::string> &strings, const std::string &delim, bool
return ss.str();
}
std::vector<std::string>
StrSplit(sled::string_view str, sled::string_view delim, bool skip_empty)
{
if (str.empty() || delim.empty()) { return {}; }
std::vector<std::string> result;
size_t start = 0;
size_t next_pos = str.find_first_of(delim, start);
while (next_pos != std::string::npos) {
if ((!skip_empty && next_pos == start) || next_pos > start) {
result.emplace_back(str.begin() + start, str.begin() + next_pos);
}
if (!skip_empty) {
start = next_pos + 1;
next_pos = str.find_first_of(delim, start);
} else {
start = str.find_first_not_of(delim, next_pos);
if (start == std::string::npos) {
// all remaining characters are delimiters
break;
}
next_pos = str.find_first_of(delim, start);
}
}
if (start < str.size()) {
result.emplace_back(str.substr(start));
} else if (!skip_empty && !str.empty() && delim.find(str.back()) != std::string::npos) {
result.emplace_back("");
}
return result;
}
std::vector<std::string>
StrSplit(sled::string_view str, char delim, bool skip_empty)
{
return StrSplit(str, std::string(1, delim), skip_empty);
}
std::vector<std::string>
StrSplit(const std::string &str, const std::string &delim, bool skip_empty)
{

View File

@ -13,9 +13,14 @@ char ToUpper(char c);
// std::string ToUpper(const std::string &str);
std::string ToLower(sled::string_view str);
std::string ToUpper(sled::string_view str);
std::string ToHex(const std::string &str);
std::string ToHex(sled::string_view str);
std::string StrJoin(const std::vector<std::string> &strings, const std::string &delim, bool skip_empty = false);
std::vector<std::string> StrSplit(const std::string &str, const std::string &delim, bool skip_empty = false);
// std::vector<std::string> StrSplit(const std::string &str, const std::string &delim, bool skip_empty = false);
std::vector<std::string> StrSplit(sled::string_view str, sled::string_view delim, bool skip_empty = false);
std::vector<std::string> StrSplit(sled::string_view str, char delim, bool skip_empty = false);
std::string Trim(const std::string &str, const std::string &chars = " \t\n\r");
std::string TrimLeft(const std::string &str, const std::string &chars = " \t\n\r");
std::string TrimRight(const std::string &str, const std::string &chars = " \t\n\r");