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/fpdfapi/font/cpdf_font.cpp | 3 +-- core/fxcrt/bytestring.cpp | 28 +++++++--------------------- fxjs/cfxjse_formcalc_context.cpp | 24 ++++++++++++------------ 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 12e5aaf3a8..8d35739d78 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -395,9 +395,8 @@ void CPDF_Font::LoadPDFEncoding(CPDF_Object* pEncoding, if (iBaseEncoding != PDFFONT_ENCODING_ADOBE_SYMBOL && iBaseEncoding != PDFFONT_ENCODING_ZAPFDINGBATS) { ByteString bsEncoding = pDict->GetStringFor("BaseEncoding"); - if (bsEncoding.Compare("MacExpertEncoding") == 0 && bTrueType) { + if (bTrueType && bsEncoding.Compare("MacExpertEncoding") == 0) bsEncoding = "WinAnsiEncoding"; - } GetPredefinedEncoding(bsEncoding, &iBaseEncoding); } if ((!bEmbedded || bTrueType) && iBaseEncoding == PDFFONT_ENCODING_BUILTIN) 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() { diff --git a/fxjs/cfxjse_formcalc_context.cpp b/fxjs/cfxjse_formcalc_context.cpp index b02e5a9d41..f9d130a119 100644 --- a/fxjs/cfxjse_formcalc_context.cpp +++ b/fxjs/cfxjse_formcalc_context.cpp @@ -4976,9 +4976,9 @@ void CFXJSE_FormCalcContext::less_operator(CFXJSE_Value* pThis, } if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) == - -1); + int result = + argFirst->ToString().Compare(argSecond->ToString().AsStringView()) < 0; + args.GetReturnValue()->SetInteger(result); return; } @@ -5006,9 +5006,9 @@ void CFXJSE_FormCalcContext::lessequal_operator( } if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) != - 1); + int result = + argFirst->ToString().Compare(argSecond->ToString().AsStringView()) <= 0; + args.GetReturnValue()->SetInteger(result); return; } @@ -5034,9 +5034,9 @@ void CFXJSE_FormCalcContext::greater_operator(CFXJSE_Value* pThis, } if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) == - 1); + int result = + argFirst->ToString().Compare(argSecond->ToString().AsStringView()) > 0; + args.GetReturnValue()->SetInteger(result); return; } @@ -5064,9 +5064,9 @@ void CFXJSE_FormCalcContext::greaterequal_operator( } if (argFirst->IsString() && argSecond->IsString()) { - args.GetReturnValue()->SetInteger( - argFirst->ToString().Compare(argSecond->ToString().AsStringView()) != - -1); + int result = + argFirst->ToString().Compare(argSecond->ToString().AsStringView()) >= 0; + args.GetReturnValue()->SetInteger(result); return; } -- cgit v1.2.3