From 72d07de1933bae3569be19a31be3f75aa7265930 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 4 May 2015 14:14:33 -0700 Subject: Merge to XFA: Fix issuse with != and == shown by fx_basic_bstring unit tests. Original Review URL: https://codereview.chromium.org/1125703004 TBR=thestig@chromium.org Review URL: https://codereview.chromium.org/1126643003 --- core/include/fxcrt/fx_string.h | 74 +++++++++++++++------------- core/src/fxcrt/fx_basic_bstring.cpp | 29 ++++++++--- core/src/fxcrt/fx_basic_bstring_unittest.cpp | 22 ++------- 3 files changed, 65 insertions(+), 60 deletions(-) diff --git a/core/include/fxcrt/fx_string.h b/core/include/fxcrt/fx_string.h index b4d7249209..7e9b1a15f6 100644 --- a/core/include/fxcrt/fx_string.h +++ b/core/include/fxcrt/fx_string.h @@ -94,14 +94,17 @@ public: CFX_ByteStringC& operator = (const CFX_ByteString& src); - bool operator == (const CFX_ByteStringC& str) const - { - return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) == 0; + bool operator== (const char* ptr) const { + return strlen(ptr) == m_Length && + FXSYS_memcmp32(ptr, m_Ptr, m_Length) == 0; } - - bool operator != (const CFX_ByteStringC& str) const - { - return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length) != 0; + bool operator== (const CFX_ByteStringC& other) const { + return other.m_Length == m_Length && + FXSYS_memcmp32(other.m_Ptr, m_Ptr, m_Length) == 0; + } + bool operator!= (const char* ptr) const { return !(*this == ptr); } + bool operator!= (const CFX_ByteStringC& other) const { + return !(*this == other); } FX_DWORD GetID(FX_STRSIZE start_pos = 0) const; @@ -166,6 +169,12 @@ private: return NULL; } }; +inline bool operator== (const char* lhs, const CFX_ByteStringC& rhs) { + return rhs == lhs; +} +inline bool operator!= (const char* lhs, const CFX_ByteStringC& rhs) { + return rhs != lhs; +} typedef const CFX_ByteStringC& FX_BSTR; #define FX_BSTRC(str) CFX_ByteStringC(str, sizeof str-1) #define FXBSTR_ID(c1, c2, c3, c4) ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) @@ -243,36 +252,22 @@ public: int Compare(FX_BSTR str) const; - bool Equal(FX_BSTR str) const; - - - bool EqualNoCase(FX_BSTR str) const; - - bool operator == (FX_LPCSTR str) const - { - return Equal(str); - } - - bool operator == (FX_BSTR str) const - { - return Equal(str); - } + bool Equal(const char* ptr) const; + bool Equal(const CFX_ByteStringC& str) const; + bool Equal(const CFX_ByteString& other) const; - bool operator == (const CFX_ByteString& str) const; + bool EqualNoCase(FX_BSTR str) const; - bool operator != (FX_LPCSTR str) const - { - return !Equal(str); - } + bool operator== (const char* ptr) const { return Equal(ptr); } + bool operator== (const CFX_ByteStringC& str) const { return Equal(str); } + bool operator== (const CFX_ByteString& other) const { return Equal(other); } - bool operator != (FX_BSTR str) const - { - return !Equal(str); + bool operator!= (const char* ptr) const { return !(*this == ptr); } + bool operator!= (const CFX_ByteStringC& str) const { + return !(*this == str); } - - bool operator != (const CFX_ByteString& str) const - { - return !operator==(str); + bool operator!= (const CFX_ByteString& other) const { + return !(*this == other); } bool operator< (const CFX_ByteString& str) const @@ -398,6 +393,19 @@ inline CFX_ByteStringC& CFX_ByteStringC::operator = (const CFX_ByteString& src) return *this; } +inline bool operator== (const char* lhs, const CFX_ByteString& rhs) { + return rhs == lhs; +} +inline bool operator== (const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { + return rhs == lhs; +} +inline bool operator!= (const char* lhs, const CFX_ByteString& rhs) { + return rhs != lhs; +} +inline bool operator!= (const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { + return rhs != lhs; +} + inline CFX_ByteString operator + (FX_BSTR str1, FX_BSTR str2) { return CFX_ByteString(str1, str2); diff --git a/core/src/fxcrt/fx_basic_bstring.cpp b/core/src/fxcrt/fx_basic_bstring.cpp index 9cf084c2fb..f18ae0825c 100644 --- a/core/src/fxcrt/fx_basic_bstring.cpp +++ b/core/src/fxcrt/fx_basic_bstring.cpp @@ -251,24 +251,37 @@ const CFX_ByteString& CFX_ByteString::operator+=(FX_BSTR string) ConcatInPlace(string.GetLength(), string.GetCStr()); return *this; } -bool CFX_ByteString::Equal(FX_BSTR str) const +bool CFX_ByteString::Equal(const char* ptr) const +{ + if (!m_pData) { + return !ptr; + } + if (!ptr) { + return false; + } + return strlen(ptr) == m_pData->m_nDataLength && + FXSYS_memcmp32(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; +} +bool CFX_ByteString::Equal(const CFX_ByteStringC& str) const { if (m_pData == NULL) { return str.IsEmpty(); } return m_pData->m_nDataLength == str.GetLength() && - FXSYS_memcmp32(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0; + FXSYS_memcmp32(m_pData->m_String, str.GetCStr(), str.GetLength()) == 0; } -bool CFX_ByteString::operator ==(const CFX_ByteString& s2) const +bool CFX_ByteString::Equal(const CFX_ByteString& other) const { - if (m_pData == NULL) { - return s2.IsEmpty(); + if (!m_pData) { + return other.IsEmpty(); } - if (s2.m_pData == NULL) { + if (!other.m_pData) { return false; } - return m_pData->m_nDataLength == s2.m_pData->m_nDataLength && - FXSYS_memcmp32(m_pData->m_String, s2.m_pData->m_String, m_pData->m_nDataLength) == 0; + return other.m_pData->m_nDataLength == m_pData->m_nDataLength && + FXSYS_memcmp32(other.m_pData->m_String, + m_pData->m_String, + m_pData->m_nDataLength) == 0; } void CFX_ByteString::Empty() { diff --git a/core/src/fxcrt/fx_basic_bstring_unittest.cpp b/core/src/fxcrt/fx_basic_bstring_unittest.cpp index 7259c97b1f..9f37cbcf19 100644 --- a/core/src/fxcrt/fx_basic_bstring_unittest.cpp +++ b/core/src/fxcrt/fx_basic_bstring_unittest.cpp @@ -83,11 +83,7 @@ TEST(fxcrt, ByteStringOperatorEQ) { const char* c_string_same1 = "hello"; ASSERT_TRUE(byte_string == c_string_same1); -#if 0 - // TODO(tsepez): See, you don't want implicit c_str() casting. - // This degrades to a pointer comparision, which flunks. ASSERT_TRUE(c_string_same1 == byte_string); -#endif const char* c_string1 = "he"; const char* c_string2 = "hellp"; @@ -138,10 +134,8 @@ TEST(fxcrt, ByteStringOperatorNE) { const char* c_string_same1 = "hello"; ASSERT_FALSE(byte_string != c_string_same1); -#if 0 - // See above TODO. ASSERT_FALSE(c_string_same1 != byte_string); -#endif + const char* c_string1 = "he"; const char* c_string2 = "hellp"; const char* c_string3 = "hellod"; @@ -415,10 +409,7 @@ TEST(fxcrt, ByteStringCOperatorEQ) { const char* c_string_same1 = "hello"; ASSERT_TRUE(byte_string_c == c_string_same1); -#if 0 - // TODO(tsepez): missing operator (but no implicit cast to c_str). ASSERT_TRUE(c_string_same1 == byte_string_c); -#endif const char* c_string1 = "he"; const char* c_string2 = "hellp"; @@ -426,12 +417,10 @@ TEST(fxcrt, ByteStringCOperatorEQ) { ASSERT_FALSE(byte_string_c == c_string1); ASSERT_FALSE(byte_string_c == c_string2); ASSERT_FALSE(byte_string_c == c_string3); -#if 0 - // See above TODO. + ASSERT_FALSE(c_string1 == byte_string_c); ASSERT_FALSE(c_string2 == byte_string_c); ASSERT_FALSE(c_string3 == byte_string_c); -#endif } TEST(fxcrt, ByteStringCOperatorNE) { @@ -472,10 +461,7 @@ TEST(fxcrt, ByteStringCOperatorNE) { const char* c_string_same1 = "hello"; ASSERT_FALSE(byte_string_c != c_string_same1); -#if 0 - // TODO(tsepez): missing operator (but no implicit cast to c_str). ASSERT_FALSE(c_string_same1 != byte_string_c); -#endif const char* c_string1 = "he"; const char* c_string2 = "hellp"; @@ -483,10 +469,8 @@ TEST(fxcrt, ByteStringCOperatorNE) { ASSERT_TRUE(byte_string_c != c_string1); ASSERT_TRUE(byte_string_c != c_string2); ASSERT_TRUE(byte_string_c != c_string3); -#if 0 - // See above TODO. + ASSERT_TRUE(c_string1 != byte_string_c); ASSERT_TRUE(c_string2 != byte_string_c); ASSERT_TRUE(c_string3 != byte_string_c); -#endif } -- cgit v1.2.3