From 8f53f54a9ccada2ea8651f2786f1bbee323f09b7 Mon Sep 17 00:00:00 2001 From: tsepez Date: Thu, 15 Sep 2016 11:55:00 -0700 Subject: Add short-cut in CFX_{Byte,Wide}String::Operator<() Strings are never less than themselves, and this will occur given the shared CoW nature of these strings. Review-Url: https://codereview.chromium.org/2347433004 --- core/fxcrt/fx_basic_bstring.cpp | 9 +++++++++ core/fxcrt/fx_basic_wstring.cpp | 9 +++++++++ core/fxcrt/include/fx_string.h | 12 ++---------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp index 0b9d9873d8..c5979a7466 100644 --- a/core/fxcrt/fx_basic_bstring.cpp +++ b/core/fxcrt/fx_basic_bstring.cpp @@ -216,6 +216,15 @@ bool CFX_ByteString::operator==(const CFX_ByteString& other) const { m_pData->m_nDataLength) == 0; } +bool CFX_ByteString::operator<(const CFX_ByteString& str) const { + if (m_pData == str.m_pData) + return false; + + int result = FXSYS_memcmp(c_str(), str.c_str(), + std::min(GetLength(), str.GetLength())); + return result < 0 || (result == 0 && GetLength() < str.GetLength()); +} + bool CFX_ByteString::EqualNoCase(const CFX_ByteStringC& str) const { if (!m_pData) return str.IsEmpty(); diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp index 88fc588947..29e915f30c 100644 --- a/core/fxcrt/fx_basic_wstring.cpp +++ b/core/fxcrt/fx_basic_wstring.cpp @@ -194,6 +194,15 @@ bool CFX_WideString::operator==(const CFX_WideString& other) const { m_pData->m_nDataLength) == 0; } +bool CFX_WideString::operator<(const CFX_WideString& str) const { + if (m_pData == str.m_pData) + return false; + + int result = + wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); + return result < 0 || (result == 0 && GetLength() < str.GetLength()); +} + void CFX_WideString::AssignCopy(const FX_WCHAR* pSrcData, FX_STRSIZE nSrcLen) { AllocBeforeWrite(nSrcLen); m_pData->CopyContents(pSrcData, nSrcLen); diff --git a/core/fxcrt/include/fx_string.h b/core/fxcrt/include/fx_string.h index c1bd82dd74..48378586d3 100644 --- a/core/fxcrt/include/fx_string.h +++ b/core/fxcrt/include/fx_string.h @@ -88,11 +88,7 @@ class CFX_ByteString { return !(*this == other); } - bool operator<(const CFX_ByteString& str) const { - int result = FXSYS_memcmp(c_str(), str.c_str(), - std::min(GetLength(), str.GetLength())); - return result < 0 || (result == 0 && GetLength() < str.GetLength()); - } + bool operator<(const CFX_ByteString& str) const; const CFX_ByteString& operator=(const FX_CHAR* str); const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); @@ -295,11 +291,7 @@ class CFX_WideString { 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()); - } + bool operator<(const CFX_WideString& str) const; FX_WCHAR GetAt(FX_STRSIZE nIndex) const { return m_pData ? m_pData->m_String[nIndex] : 0; -- cgit v1.2.3