fix Trim failed
All checks were successful
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 34s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 36s

This commit is contained in:
tqcq 2024-03-03 18:05:37 +08:00
parent 5413784c57
commit f31657640f

View File

@ -44,14 +44,15 @@ StrSplit(const std::string &str, const std::string &delim, bool skip_empty)
std::string std::string
Trim(const std::string &str, const std::string &chars) Trim(const std::string &str, const std::string &chars)
{ {
return TrimLeft(TrimRight(str)); return TrimLeft(TrimRight(str, chars), chars);
} }
std::string std::string
TrimLeft(const std::string &str, const std::string &chars) TrimLeft(const std::string &str, const std::string &chars)
{ {
size_t start = 0; size_t start = 0;
while (start < str.size() && chars.find(str[start]) != std::string::npos) { while (start < str.size()
&& chars.find_first_of(str[start]) != std::string::npos) {
++start; ++start;
} }
return str.substr(start); return str.substr(start);
@ -61,7 +62,9 @@ std::string
TrimRight(const std::string &str, const std::string &chars) TrimRight(const std::string &str, const std::string &chars)
{ {
size_t end = str.size(); size_t end = str.size();
while (end > 0 && chars.find(str[end - 1]) != std::string::npos) { --end; } while (end > 0 && chars.find_first_of(str[end - 1]) != std::string::npos) {
--end;
}
return str.substr(0, end); return str.substr(0, end);
} }