summaryrefslogtreecommitdiffstats
path: root/base/strings.cpp
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2016-10-25 14:56:04 -0700
committerElliott Hughes <enh@google.com>2016-10-25 14:56:04 -0700
commit42937492c8ca790676af6372e67971043de0711b (patch)
treea584c4bbb2e348dbdd4c56a1a42bec049565e278 /base/strings.cpp
parent8cf0bd75f79ed641b5841dea1d34af18c6b4d40d (diff)
downloadsystem_core-42937492c8ca790676af6372e67971043de0711b.tar.gz
system_core-42937492c8ca790676af6372e67971043de0711b.tar.bz2
system_core-42937492c8ca790676af6372e67971043de0711b.zip
Add StartsWithIgnoreCase/EndsWithIgnoreCase.
This has come up a couple of times now. Bug: wanted as part of http://b/32094640 Test: ran tests Change-Id: I51b67074b7ddeedd771d7be9651ba33e05491b33
Diffstat (limited to 'base/strings.cpp')
-rw-r--r--base/strings.cpp18
1 files changed, 15 insertions, 3 deletions
diff --git a/base/strings.cpp b/base/strings.cpp
index b8775df21..7a94ad78c 100644
--- a/base/strings.cpp
+++ b/base/strings.cpp
@@ -87,17 +87,29 @@ 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 s.compare(0, strlen(prefix), prefix) == 0;
+ return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
}
-bool EndsWith(const std::string& s, const char* suffix) {
+bool StartsWithIgnoreCase(const std::string& s, const char* prefix) {
+ return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
+}
+
+static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) {
size_t suffix_length = strlen(suffix);
size_t string_length = s.size();
if (suffix_length > string_length) {
return false;
}
size_t offset = string_length - suffix_length;
- return s.compare(offset, suffix_length, suffix) == 0;
+ 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, true);
+}
+
+bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
+ return EndsWith(s, suffix, false);
}
} // namespace base