From 84688ebd24576b1c1199302abb66b69c5bf39a72 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Fri, 15 Mar 2024 15:31:12 +0800 Subject: [PATCH] feat add EqualsIgnoreCase,StartsWithIgnoreCase,EndsWithIgnoreCase --- include/sled/strings/utils.h | 19 +++++++++---------- src/strings/utils.cc | 32 ++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/include/sled/strings/utils.h b/include/sled/strings/utils.h index 790c2ad..5383c84 100644 --- a/include/sled/strings/utils.h +++ b/include/sled/strings/utils.h @@ -11,19 +11,18 @@ char ToUpper(char c); std::string ToLower(const std::string &str); std::string ToUpper(const std::string &str); std::string ToHex(const std::string &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::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::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"); +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"); bool EndsWith(const std::string &str, const std::string &suffix); bool StartsWith(const std::string &str, const std::string &prefix); +bool EndsWithIgnoreCase(const std::string &str, const std::string &suffix); +bool StartsWithIgnoreCase(const std::string &str, const std::string &prefix); + +bool EqualsIgnoreCase(const std::string &lhs, const std::string &rhs); + }// namespace sled #endif// SLED_STRINGS_UTILS_H diff --git a/src/strings/utils.cc b/src/strings/utils.cc index 05a3651..c41658b 100644 --- a/src/strings/utils.cc +++ b/src/strings/utils.cc @@ -33,9 +33,7 @@ ToUpper(const std::string &str) } std::string -StrJoin(const std::vector &strings, - const std::string &delim, - bool skip_empty) +StrJoin(const std::vector &strings, const std::string &delim, bool skip_empty) { if (strings.empty()) { return ""; } @@ -59,9 +57,7 @@ StrSplit(const std::string &str, const std::string &delim, bool skip_empty) 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) { - result.emplace_back(str.substr(start, next_pos - start)); - } + if (!skip_empty || next_pos > start) { result.emplace_back(str.substr(start, next_pos - start)); } start = next_pos + 1; next_pos = str.find_first_of(delim, start); } @@ -93,15 +89,31 @@ TrimRight(const std::string &str, const std::string &chars) bool EndsWith(const std::string &str, const std::string &suffix) { - return str.size() >= suffix.size() - && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; + return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0; } bool StartsWith(const std::string &str, const std::string &prefix) { - return str.size() >= prefix.size() - && str.compare(0, prefix.size(), prefix) == 0; + return str.size() >= prefix.size() && str.compare(0, prefix.size(), prefix) == 0; +} + +bool +EndsWithIgnoreCase(const std::string &str, const std::string &suffix) +{ + return EndsWith(ToLower(str), ToLower(suffix)); +} + +bool +StartsWithIgnoreCase(const std::string &str, const std::string &prefix) +{ + return StartsWith(ToLower(str), ToLower(prefix)); +} + +bool +EqualsIgnoreCase(const std::string &lhs, const std::string &rhs) +{ + return ToLower(lhs) == ToLower(rhs); } }// namespace sled