From 2140782d241375db1ad93ee4f5ef5835355ac94b Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 13 Jan 2017 18:51:32 -0800 Subject: [PATCH] bootable/recovery wants EqualsIgnoreCase. Bug: N/A Test: ran tests Change-Id: I4a6ee9eba0514b8bb8fb0489f4d370964ce9c1c2 --- base/include/android-base/strings.h | 3 +++ base/strings.cpp | 4 ++++ base/strings_test.cpp | 7 +++++++ 3 files changed, 14 insertions(+) diff --git a/base/include/android-base/strings.h b/base/include/android-base/strings.h index b8a92896a..f5f5c1139 100644 --- a/base/include/android-base/strings.h +++ b/base/include/android-base/strings.h @@ -64,6 +64,9 @@ bool StartsWithIgnoreCase(const std::string& s, const char* prefix); bool EndsWith(const std::string& s, const char* suffix); bool EndsWithIgnoreCase(const std::string& s, const char* suffix); +// Tests whether 'lhs' equals 'rhs', ignoring case. +bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); + } // namespace base } // namespace android diff --git a/base/strings.cpp b/base/strings.cpp index 7a94ad78c..46fe93995 100644 --- a/base/strings.cpp +++ b/base/strings.cpp @@ -112,5 +112,9 @@ bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, false); } +bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { + return strcasecmp(lhs.c_str(), rhs.c_str()) == 0; +} + } // namespace base } // namespace android diff --git a/base/strings_test.cpp b/base/strings_test.cpp index 5fb21dde6..7a65a00b1 100644 --- a/base/strings_test.cpp +++ b/base/strings_test.cpp @@ -244,3 +244,10 @@ TEST(strings, EndsWithIgnoreCase_contains_prefix) { ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "OBA")); ASSERT_FALSE(android::base::EndsWithIgnoreCase("foobar", "FOO")); } + +TEST(strings, EqualsIgnoreCase) { + ASSERT_TRUE(android::base::EqualsIgnoreCase("foo", "FOO")); + ASSERT_TRUE(android::base::EqualsIgnoreCase("FOO", "foo")); + ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "bar")); + ASSERT_FALSE(android::base::EqualsIgnoreCase("foo", "fool")); +}