From cc42afc14b6e54750a95b0e5fd7f01664280c36e Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 20 Dec 2017 19:05:00 +0000 Subject: Combine operator< and ByteString::Compare(). Fix or optimize some ByteString::Compare() callers. Change-Id: I0fde91afc3d17fe160b46d00a441ad05e56377e7 Reviewed-on: https://pdfium-review.googlesource.com/20851 Reviewed-by: Henrique Nakashima Commit-Queue: Lei Zhang --- core/fxcrt/bytestring.cpp | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index 324a2801e4..4c4bd57393 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -317,16 +317,7 @@ bool ByteString::operator<(const char* ptr) const { } bool ByteString::operator<(const ByteStringView& str) const { - if (!m_pData && !str.unterminated_c_str()) - return false; - if (c_str() == str.unterminated_c_str()) - return false; - - size_t len = GetLength(); - size_t other_len = str.GetLength(); - int result = - memcmp(c_str(), str.unterminated_c_str(), std::min(len, other_len)); - return result < 0 || (result == 0 && len < other_len); + return Compare(str) < 0; } bool ByteString::operator<(const ByteString& other) const { @@ -717,17 +708,12 @@ int ByteString::Compare(const ByteStringView& str) const { size_t this_len = m_pData->m_nDataLength; size_t that_len = str.GetLength(); size_t min_len = std::min(this_len, that_len); - for (size_t i = 0; i < min_len; i++) { - if (static_cast(m_pData->m_String[i]) < str[i]) - return -1; - if (static_cast(m_pData->m_String[i]) > str[i]) - return 1; - } - if (this_len < that_len) - return -1; - if (this_len > that_len) - return 1; - return 0; + int result = memcmp(m_pData->m_String, str.unterminated_c_str(), min_len); + if (result != 0) + return result; + if (this_len == that_len) + return 0; + return this_len < that_len ? -1 : 1; } void ByteString::Trim() { -- cgit v1.2.3