Merge "[base] Convert Starts/Ends/Equals string functions to string_view" am: 1265bc9c6f

am: 8a7e47861a

Change-Id: Ib37994c7c1bd672c18d0b4466458a65aaa2b90ec
This commit is contained in:
Yurii Zubrytskyi 2019-03-21 10:34:25 -07:00 committed by android-build-merger
commit 1534041335
2 changed files with 22 additions and 53 deletions

View file

@ -56,23 +56,17 @@ extern template std::string Join(const std::vector<std::string>&, const std::str
extern template std::string Join(const std::vector<const char*>&, const std::string&); extern template std::string Join(const std::vector<const char*>&, const std::string&);
// Tests whether 's' starts with 'prefix'. // Tests whether 's' starts with 'prefix'.
// TODO: string_view bool StartsWith(std::string_view s, std::string_view prefix);
bool StartsWith(const std::string& s, const char* prefix); bool StartsWith(std::string_view s, char prefix);
bool StartsWithIgnoreCase(const std::string& s, const char* prefix); bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix);
bool StartsWith(const std::string& s, const std::string& prefix);
bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix);
bool StartsWith(const std::string& s, char prefix);
// Tests whether 's' ends with 'suffix'. // Tests whether 's' ends with 'suffix'.
// TODO: string_view bool EndsWith(std::string_view s, std::string_view suffix);
bool EndsWith(const std::string& s, const char* suffix); bool EndsWith(std::string_view s, char suffix);
bool EndsWithIgnoreCase(const std::string& s, const char* suffix); bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix);
bool EndsWith(const std::string& s, const std::string& suffix);
bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix);
bool EndsWith(const std::string& s, char suffix);
// Tests whether 'lhs' equals 'rhs', ignoring case. // Tests whether 'lhs' equals 'rhs', ignoring case.
bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs);
} // namespace base } // namespace base
} // namespace android } // namespace android

View file

@ -87,58 +87,33 @@ template std::string Join(const std::vector<const char*>&, char);
template std::string Join(const std::vector<std::string>&, const std::string&); template std::string Join(const std::vector<std::string>&, const std::string&);
template std::string Join(const std::vector<const char*>&, const std::string&); template std::string Join(const std::vector<const char*>&, const std::string&);
bool StartsWith(const std::string& s, const char* prefix) { bool StartsWith(std::string_view s, std::string_view prefix) {
return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; return s.substr(0, prefix.size()) == prefix;
} }
bool StartsWith(const std::string& s, const std::string& prefix) { bool StartsWith(std::string_view s, char prefix) {
return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; return !s.empty() && s.front() == prefix;
} }
bool StartsWith(const std::string& s, char prefix) { bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) {
return *s.c_str() == prefix; // Use c_str() to guarantee there is at least a '\0'. return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0;
} }
bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { bool EndsWith(std::string_view s, std::string_view suffix) {
return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix;
} }
bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { bool EndsWith(std::string_view s, char suffix) {
return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; return !s.empty() && s.back() == suffix;
} }
static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) {
bool case_sensitive) { return s.size() >= suffix.size() &&
size_t string_length = s.size(); strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0;
if (suffix_length > string_length) {
return false;
}
size_t offset = string_length - suffix_length;
return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0;
} }
bool EndsWith(const std::string& s, const char* suffix) { bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) {
return EndsWith(s, suffix, strlen(suffix), true); return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0;
}
bool EndsWith(const std::string& s, const std::string& suffix) {
return EndsWith(s, suffix.c_str(), suffix.size(), true);
}
bool EndsWith(const std::string& s, char suffix) {
return EndsWith(s, &suffix, 1, true);
}
bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
return EndsWith(s, suffix, strlen(suffix), false);
}
bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) {
return EndsWith(s, suffix.c_str(), suffix.size(), false);
}
bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) {
return strcasecmp(lhs.c_str(), rhs.c_str()) == 0;
} }
} // namespace base } // namespace base