diff options
author | Yurii Zubrytskyi <zyy@google.com> | 2019-03-19 17:03:42 -0700 |
---|---|---|
committer | Yurii Zubrytskyi <zyy@google.com> | 2019-03-21 16:29:46 +0000 |
commit | 59d876c7a2468c189edebf451cbc0debd2aced47 (patch) | |
tree | 7ada3f700ad8bb8b69941cc2b614f9f656f6189a | |
parent | c3e96512c0e6243531f092e706ba002fd77997c6 (diff) | |
download | system_core-59d876c7a2468c189edebf451cbc0debd2aced47.tar.gz system_core-59d876c7a2468c189edebf451cbc0debd2aced47.tar.bz2 system_core-59d876c7a2468c189edebf451cbc0debd2aced47.zip |
[base] Convert Starts/Ends/Equals string functions to string_view
Test: UTs
Change-Id: I3751656d44e7a814488169c53d3e8af0da44f262
-rw-r--r-- | base/include/android-base/strings.h | 20 | ||||
-rw-r--r-- | base/strings.cpp | 55 |
2 files changed, 22 insertions, 53 deletions
diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h index fc5c1ce36..8e9716f9f 100644 --- a/base/include/android-base/strings.h +++ b/base/include/android-base/strings.h @@ -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&); // Tests whether 's' starts with 'prefix'. -// TODO: string_view -bool StartsWith(const std::string& s, const char* prefix); -bool StartsWithIgnoreCase(const std::string& s, const char* 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); +bool StartsWith(std::string_view s, std::string_view prefix); +bool StartsWith(std::string_view s, char prefix); +bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix); // Tests whether 's' ends with 'suffix'. -// TODO: string_view -bool EndsWith(const std::string& s, const char* suffix); -bool EndsWithIgnoreCase(const std::string& s, const char* 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); +bool EndsWith(std::string_view s, std::string_view suffix); +bool EndsWith(std::string_view s, char suffix); +bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix); // 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 android diff --git a/base/strings.cpp b/base/strings.cpp index 2d6eef099..bb3167ef0 100644 --- a/base/strings.cpp +++ b/base/strings.cpp @@ -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<const char*>&, const std::string&); -bool StartsWith(const std::string& s, const char* prefix) { - return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; +bool StartsWith(std::string_view s, std::string_view prefix) { + return s.substr(0, prefix.size()) == prefix; } -bool StartsWith(const std::string& s, const std::string& prefix) { - return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; +bool StartsWith(std::string_view s, char prefix) { + return !s.empty() && s.front() == prefix; } -bool StartsWith(const std::string& s, char prefix) { - return *s.c_str() == prefix; // Use c_str() to guarantee there is at least a '\0'. +bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) { + return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0; } -bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { - return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; +bool EndsWith(std::string_view s, std::string_view suffix) { + return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix; } -bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { - return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; +bool EndsWith(std::string_view s, char suffix) { + return !s.empty() && s.back() == suffix; } -static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, - bool case_sensitive) { - size_t string_length = s.size(); - 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) { - return EndsWith(s, suffix, strlen(suffix), true); -} - -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 EndsWithIgnoreCase(std::string_view s, std::string_view suffix) { + return s.size() >= suffix.size() && + strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0; } -bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { - return strcasecmp(lhs.c_str(), rhs.c_str()) == 0; +bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) { + return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0; } } // namespace base |