From f3a7edc07aee2a12cf02ee33471c45b4c6b00307 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Tue, 12 Dec 2017 18:42:18 +0000 Subject: Combine WideString::operator< and Compare(). Change-Id: I3ea13ace6290e8fab41a3f65c9ac047407a74e4d Reviewed-on: https://pdfium-review.googlesource.com/20852 Reviewed-by: Ryan Harrison Commit-Queue: Lei Zhang --- core/fxcrt/widestring.cpp | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/core/fxcrt/widestring.cpp b/core/fxcrt/widestring.cpp index 2caa3b4213..3b72c8fa94 100644 --- a/core/fxcrt/widestring.cpp +++ b/core/fxcrt/widestring.cpp @@ -500,15 +500,7 @@ bool WideString::operator==(const WideString& other) const { } bool WideString::operator<(const wchar_t* ptr) const { - if (!m_pData && !ptr) - return false; - if (c_str() == ptr) - return false; - - size_t len = GetLength(); - size_t other_len = ptr ? wcslen(ptr) : 0; - int result = wmemcmp(c_str(), ptr, std::min(len, other_len)); - return result < 0 || (result == 0 && len < other_len); + return Compare(ptr) < 0; } bool WideString::operator<(const WideStringView& str) const { @@ -525,13 +517,7 @@ bool WideString::operator<(const WideStringView& str) const { } bool WideString::operator<(const WideString& other) const { - if (m_pData == other.m_pData) - return false; - - size_t len = GetLength(); - size_t other_len = other.GetLength(); - int result = wmemcmp(c_str(), other.c_str(), std::min(len, other_len)); - return result < 0 || (result == 0 && len < other_len); + return Compare(other) < 0; } void WideString::AssignCopy(const wchar_t* pSrcData, size_t nSrcLen) { @@ -933,17 +919,12 @@ int WideString::Compare(const WideString& str) const { size_t this_len = m_pData->m_nDataLength; size_t that_len = str.m_pData->m_nDataLength; size_t min_len = std::min(this_len, that_len); - for (size_t i = 0; i < min_len; i++) { - if (m_pData->m_String[i] < str.m_pData->m_String[i]) - return -1; - if (m_pData->m_String[i] > str.m_pData->m_String[i]) - return 1; - } - if (this_len < that_len) - return -1; - if (this_len > that_len) - return 1; - return 0; + int result = wmemcmp(m_pData->m_String, str.m_pData->m_String, min_len); + if (result != 0) + return result; + if (this_len == that_len) + return 0; + return this_len < that_len; } int WideString::CompareNoCase(const wchar_t* lpsz) const { -- cgit v1.2.3