From aadcd71ab29f588d4997ec25855f60f5866959f2 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Mon, 4 May 2015 15:20:29 -0700 Subject: Fix issues with != and == in fx_basic_wstring Part two. Fix same issue in wide strings as in their bytestring counterparts. R=thestig@chromium.org Review URL: https://codereview.chromium.org/1127753002 --- core/include/fxcrt/fx_string.h | 63 +++++++++++++++++++--------- core/src/fxcrt/fx_basic_wstring.cpp | 58 ++++++++++--------------- core/src/fxcrt/fx_basic_wstring_unittest.cpp | 21 +--------- 3 files changed, 69 insertions(+), 73 deletions(-) diff --git a/core/include/fxcrt/fx_string.h b/core/include/fxcrt/fx_string.h index 7e9b1a15f6..c46a97166b 100644 --- a/core/include/fxcrt/fx_string.h +++ b/core/include/fxcrt/fx_string.h @@ -511,14 +511,17 @@ public: CFX_WideStringC& operator = (const CFX_WideString& src); - bool operator == (const CFX_WideStringC& str) const - { - return str.m_Length == m_Length && FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) == 0; + bool operator== (const wchar_t* ptr) const { + return FXSYS_wcslen(ptr) == m_Length && + wmemcmp(ptr, m_Ptr, m_Length) == 0; } - - bool operator != (const CFX_WideStringC& str) const - { - return str.m_Length != m_Length || FXSYS_memcmp32(str.m_Ptr, m_Ptr, m_Length * sizeof(FX_WCHAR)) != 0; + bool operator== (const CFX_WideStringC& str) const { + return str.m_Length == m_Length && + wmemcmp(str.m_Ptr, m_Ptr, m_Length) == 0; + } + bool operator!= (const wchar_t* ptr) const { return !(*this == ptr); } + bool operator!= (const CFX_WideStringC& str) const { + return !(*this == str); } FX_LPCWSTR GetPtr() const @@ -598,8 +601,15 @@ private: return NULL; } }; +inline bool operator== (const wchar_t* lhs, const CFX_WideStringC& rhs) { + return rhs == lhs; +} +inline bool operator!= (const wchar_t* lhs, const CFX_WideStringC& rhs) { + return rhs != lhs; +} typedef const CFX_WideStringC& FX_WSTR; #define FX_WSTRC(wstr) CFX_WideStringC(wstr, FX_ArraySize(wstr) - 1) + struct CFX_StringDataW { intptr_t m_nRefs; // Would prefer ssize_t, but no windows support. FX_STRSIZE m_nDataLength; @@ -681,6 +691,18 @@ public: const CFX_WideString& operator += (const CFX_WideStringC& str); + bool operator== (const wchar_t* ptr) const { return Equal(ptr); } + bool operator== (const CFX_WideStringC& str) const { return Equal(str); } + bool operator== (const CFX_WideString& other) const { return Equal(other); } + + bool operator!= (const wchar_t* ptr) const { return !(*this == ptr); } + bool operator!= (const CFX_WideStringC& str) const { + return !(*this == str); + } + bool operator!= (const CFX_WideString& other) const { + return !(*this == other); + } + bool operator< (const CFX_WideString& str) const { int result = wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); return result < 0 || (result == 0 && GetLength() < str.GetLength()); @@ -704,7 +726,9 @@ public: int CompareNoCase(FX_LPCWSTR str) const; - bool Equal(const CFX_WideStringC& str) const; + bool Equal(const wchar_t* ptr) const; + bool Equal(const CFX_WideStringC& str) const; + bool Equal(const CFX_WideString& other) const; CFX_WideString Mid(FX_STRSIZE first) const; @@ -832,17 +856,18 @@ inline CFX_WideString operator + (const CFX_WideStringC& str1, const CFX_WideStr { return CFX_WideString(str1, str2); } - -bool operator==(const CFX_WideString& s1, const CFX_WideString& s2); -bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2); -bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2); -bool operator== (const CFX_WideString& s1, FX_LPCWSTR s2); -bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2); -bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2); -bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2); -bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2); -bool operator!= (const CFX_WideString& s1, FX_LPCWSTR s2); -bool operator!=(FX_LPCWSTR s1, const CFX_WideString& s2); +inline bool operator== (const wchar_t* lhs, const CFX_WideString& rhs) { + return rhs == lhs; +} +inline bool operator== (const CFX_WideStringC& lhs, const CFX_WideString& rhs) { + return rhs == lhs; +} +inline bool operator!= (const wchar_t* lhs, const CFX_WideString& rhs) { + return rhs != lhs; +} +inline bool operator!= (const CFX_WideStringC& lhs, const CFX_WideString& rhs) { + return rhs != lhs; +} FX_FLOAT FX_atof(FX_BSTR str); void FX_atonum(FX_BSTR str, FX_BOOL& bInteger, void* pData); FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf); diff --git a/core/src/fxcrt/fx_basic_wstring.cpp b/core/src/fxcrt/fx_basic_wstring.cpp index 742f249e37..9b27537ed9 100644 --- a/core/src/fxcrt/fx_basic_wstring.cpp +++ b/core/src/fxcrt/fx_basic_wstring.cpp @@ -205,41 +205,16 @@ const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& string) ConcatInPlace(string.GetLength(), string.GetPtr()); return *this; } -bool operator==(const CFX_WideString& s1, FX_LPCWSTR s2) +bool CFX_WideString::Equal(const wchar_t* ptr) const { - return s1.Equal(s2); -} -bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2) -{ - return s2.Equal(s1); -} -bool operator==(const CFX_WideString& s1, const CFX_WideString& s2) -{ - return s1.Equal(s2); -} -bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2) -{ - return s1.Equal(s2); -} -bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2) -{ - return s2.Equal(s1); -} -bool operator != (const CFX_WideString& s1, FX_LPCWSTR s2) -{ - return !s1.Equal(s2); -} -bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2) -{ - return !s1.Equal(s2); -} -bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2) -{ - return !s1.Equal(s2); -} -bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2) -{ - return !s2.Equal(s1); + if (!m_pData) { + return !ptr; + } + if (!ptr) { + return false; + } + return wcslen(ptr) == m_pData->m_nDataLength && + wmemcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; } bool CFX_WideString::Equal(const CFX_WideStringC& str) const { @@ -247,7 +222,20 @@ bool CFX_WideString::Equal(const CFX_WideStringC& str) const return str.IsEmpty(); } return str.GetLength() == m_pData->m_nDataLength && - FXSYS_memcmp32(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength * sizeof(FX_WCHAR)) == 0; + wmemcmp(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength) == 0; +} +bool CFX_WideString::Equal(const CFX_WideString& other) const +{ + if (!m_pData) { + return other.IsEmpty(); + } + if (!other.m_pData) { + return false; + } + return other.m_pData->m_nDataLength == m_pData->m_nDataLength && + wmemcmp(other.m_pData->m_String, + m_pData->m_String, + m_pData->m_nDataLength) == 0; } void CFX_WideString::Empty() { diff --git a/core/src/fxcrt/fx_basic_wstring_unittest.cpp b/core/src/fxcrt/fx_basic_wstring_unittest.cpp index 16d71cb5b8..3b15006d65 100644 --- a/core/src/fxcrt/fx_basic_wstring_unittest.cpp +++ b/core/src/fxcrt/fx_basic_wstring_unittest.cpp @@ -83,11 +83,7 @@ TEST(fxcrt, WideStringOperatorEQ) { const wchar_t* c_string_same1 = L"hello"; ASSERT_TRUE(wide_string == c_string_same1); -#if 0 - // TODO(tsepez): Missing operator - there's a prototype but no actual - // implementation (at least we already removed implicit c_str() casting). ASSERT_TRUE(c_string_same1 == wide_string); -#endif const wchar_t* c_string1 = L"he"; const wchar_t* c_string2 = L"hellp"; @@ -95,12 +91,9 @@ TEST(fxcrt, WideStringOperatorEQ) { ASSERT_FALSE(wide_string == c_string1); ASSERT_FALSE(wide_string == c_string2); ASSERT_FALSE(wide_string == c_string3); -#if 0 - // See above TODO. ASSERT_FALSE(c_string1 == wide_string); ASSERT_FALSE(c_string2 == wide_string); ASSERT_FALSE(c_string3 == wide_string); -#endif } TEST(fxcrt, WideStringOperatorNE) { @@ -141,22 +134,18 @@ TEST(fxcrt, WideStringOperatorNE) { const wchar_t* c_string_same1 = L"hello"; ASSERT_FALSE(wide_string != c_string_same1); -#if 0 - // See above TODO. ASSERT_FALSE(c_string_same1 != wide_string); -#endif + const wchar_t* c_string1 = L"he"; const wchar_t* c_string2 = L"hellp"; const wchar_t* c_string3 = L"hellod"; ASSERT_TRUE(wide_string != c_string1); ASSERT_TRUE(wide_string != c_string2); ASSERT_TRUE(wide_string != c_string3); -#if 0 - // See above TODO. + ASSERT_TRUE(c_string1 != wide_string); ASSERT_TRUE(c_string2 != wide_string); ASSERT_TRUE(c_string3 != wide_string); -#endif } #define ByteStringLiteral(str) CFX_ByteString(FX_BSTRC(str)) @@ -256,8 +245,6 @@ TEST(fxcrt, WideStringCOperatorEQ) { ASSERT_FALSE(wide_string2 == wide_string_c); ASSERT_FALSE(wide_string3 == wide_string_c); -#if 0 - // TODO(tsepez): ambiguos overload prevents compilation const wchar_t* c_string_same1 = L"hello"; ASSERT_TRUE(wide_string_c == c_string_same1); ASSERT_TRUE(c_string_same1 == wide_string_c); @@ -272,7 +259,6 @@ TEST(fxcrt, WideStringCOperatorEQ) { ASSERT_FALSE(c_string1 == wide_string_c); ASSERT_FALSE(c_string2 == wide_string_c); ASSERT_FALSE(c_string3 == wide_string_c); -#endif } TEST(fxcrt, WideStringCOperatorNE) { @@ -311,8 +297,6 @@ TEST(fxcrt, WideStringCOperatorNE) { ASSERT_TRUE(wide_string2 != wide_string_c); ASSERT_TRUE(wide_string3 != wide_string_c); -#if 0 - // See above TODO. const wchar_t* c_string_same1 = L"hello"; ASSERT_FALSE(wide_string_c != c_string_same1); ASSERT_FALSE(c_string_same1 != wide_string_c); @@ -327,5 +311,4 @@ TEST(fxcrt, WideStringCOperatorNE) { ASSERT_TRUE(c_string1 != wide_string_c); ASSERT_TRUE(c_string2 != wide_string_c); ASSERT_TRUE(c_string3 != wide_string_c); -#endif } -- cgit v1.2.3