feat add EqualsIgnoreCase,StartsWithIgnoreCase,EndsWithIgnoreCase
All checks were successful
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 51s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 50s

This commit is contained in:
tqcq 2024-03-15 15:31:12 +08:00
parent 9b1de49188
commit 84688ebd24
2 changed files with 31 additions and 20 deletions

View File

@ -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<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::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::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

View File

@ -33,9 +33,7 @@ ToUpper(const std::string &str)
}
std::string
StrJoin(const std::vector<std::string> &strings,
const std::string &delim,
bool skip_empty)
StrJoin(const std::vector<std::string> &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