From b6e7c12aee7209579aa5689a9396ed3c2e44507e Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Sun, 14 Apr 2024 15:46:50 +0800 Subject: [PATCH] feat use string_view --- src/sled/strings/utils.cc | 54 +++++++++++++++++++++++++++++++++++++++ src/sled/strings/utils.h | 9 +++++-- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/sled/strings/utils.cc b/src/sled/strings/utils.cc index 9193af1..aa24e3c 100644 --- a/src/sled/strings/utils.cc +++ b/src/sled/strings/utils.cc @@ -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 &strings, const std::string &delim, bool skip_empty) { @@ -48,6 +61,47 @@ StrJoin(const std::vector &strings, const std::string &delim, bool return ss.str(); } +std::vector +StrSplit(sled::string_view str, sled::string_view delim, bool skip_empty) +{ + if (str.empty() || delim.empty()) { return {}; } + + std::vector 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 +StrSplit(sled::string_view str, char delim, bool skip_empty) +{ + return StrSplit(str, std::string(1, delim), skip_empty); +} + std::vector StrSplit(const std::string &str, const std::string &delim, bool skip_empty) { diff --git a/src/sled/strings/utils.h b/src/sled/strings/utils.h index 9736661..5d74645 100644 --- a/src/sled/strings/utils.h +++ b/src/sled/strings/utils.h @@ -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 &strings, const std::string &delim, bool skip_empty = false); -std::vector StrSplit(const std::string &str, const std::string &delim, bool skip_empty = false); +// std::vector StrSplit(const std::string &str, const std::string &delim, bool skip_empty = false); + +std::vector StrSplit(sled::string_view str, sled::string_view delim, bool skip_empty = false); +std::vector 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");