Fix asymmetry in hidl_string::operator==
Bug: 32456816
Test: libhidl-test
Change-Id: If13ef62a18b12cecb466b1e9701da2614ce75db8
diff --git a/include/hidl/HidlSupport.h b/include/hidl/HidlSupport.h
index 07eebeb..37c4431 100644
--- a/include/hidl/HidlSupport.h
+++ b/include/hidl/HidlSupport.h
@@ -76,14 +76,6 @@
status_t writeEmbeddedToParcel(
Parcel *parcel, size_t parentHandle, size_t parentOffset) const;
- inline bool operator==(const char *s) const {
- return strcmp(mBuffer, s) == 0;
- }
-
- inline bool operator!=(const char *s) const {
- return !(operator==(s));
- }
-
// offsetof(hidl_string, mBuffer) exposed since mBuffer is private.
static const size_t kOffsetOfBuffer;
@@ -99,6 +91,22 @@
void moveFrom(hidl_string &&);
};
+inline bool operator==(const hidl_string &hs, const char *s) {
+ return strcmp(hs.c_str(), s) == 0;
+}
+
+inline bool operator!=(const hidl_string &hs, const char *s) {
+ return !(hs == s);
+}
+
+inline bool operator==(const char *s, const hidl_string &hs) {
+ return strcmp(hs.c_str(), s) == 0;
+}
+
+inline bool operator!=(const char *s, const hidl_string &hs) {
+ return !(s == hs);
+}
+
////////////////////////////////////////////////////////////////////////////////
template<typename T>
diff --git a/test/main.cpp b/test/main.cpp
index fff69b0..f5b5f20 100644
--- a/test/main.cpp
+++ b/test/main.cpp
@@ -105,6 +105,19 @@
EXPECT_TRUE(flag);
}
+TEST_F(LibHidlTest, StringCmpTest) {
+ using android::hardware::hidl_string;
+ const char * s = "good";
+ hidl_string hs(s);
+ EXPECT_NE(hs.c_str(), s);
+
+ EXPECT_TRUE(hs == s); // operator ==
+ EXPECT_TRUE(s == hs);
+
+ EXPECT_FALSE(hs != s); // operator ==
+ EXPECT_FALSE(s != hs);
+}
+
template <typename T>
void great(android::hardware::hidl_vec<T>) {}