From 6ec5ab029194f350866211eccb7ee2a2a0d35bef Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 31 Jan 2018 16:57:32 +0000 Subject: Remove handrolled search from GetCSSPropertyValueByName BUG=pdfium:798 Change-Id: I1e50aeffa439063bc9eb2640af457e33c4e3c46c Reviewed-on: https://pdfium-review.googlesource.com/24713 Commit-Queue: Ryan Harrison Reviewed-by: dsinclair --- core/fxcrt/css/cfx_css.h | 5 ++-- core/fxcrt/css/cfx_cssdeclaration.cpp | 49 ++++++++++++++--------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/core/fxcrt/css/cfx_css.h b/core/fxcrt/css/cfx_css.h index 3703394d4f..0ccaaf2bc8 100644 --- a/core/fxcrt/css/cfx_css.h +++ b/core/fxcrt/css/cfx_css.h @@ -31,6 +31,8 @@ enum class CFX_CSSPrimitiveType : uint8_t { List, }; +// Any entries added/removed here, will need to be mirrored in +// propertyValueTable, in core/fxcrt/css/cfx_cssdeclaration.cpp. enum class CFX_CSSPropertyValue : uint8_t { Bolder = 0, None, @@ -76,11 +78,10 @@ enum class CFX_CSSPropertyValue : uint8_t { Large, Left, TextTop, - LAST_MARKER }; // Any entries added/removed here, will need to be mirrored in -// g_CFX_CSSProperties. +// propertyTable, in core/fxcrt/css/cfx_csspropertytable.cpp. enum class CFX_CSSProperty : uint8_t { BorderLeft = 0, Top, diff --git a/core/fxcrt/css/cfx_cssdeclaration.cpp b/core/fxcrt/css/cfx_cssdeclaration.cpp index e771f6277d..278ffabdca 100644 --- a/core/fxcrt/css/cfx_cssdeclaration.cpp +++ b/core/fxcrt/css/cfx_cssdeclaration.cpp @@ -24,12 +24,13 @@ uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { return (FXSYS_HexCharToInt(hexHigh) << 4) + FXSYS_HexCharToInt(hexLow); } -struct CFX_CSSPropertyValueTable { +struct CFX_CSSPropertyValueEntry { CFX_CSSPropertyValue eName; const wchar_t* pszName; uint32_t dwHash; }; -const CFX_CSSPropertyValueTable g_CFX_CSSPropertyValues[] = { + +const CFX_CSSPropertyValueEntry propertyValueTable[] = { {CFX_CSSPropertyValue::Bolder, L"bolder", 0x009F1058}, {CFX_CSSPropertyValue::None, L"none", 0x048B6670}, {CFX_CSSPropertyValue::Dot, L"dot", 0x0A48CB27}, @@ -75,11 +76,6 @@ const CFX_CSSPropertyValueTable g_CFX_CSSPropertyValues[] = { {CFX_CSSPropertyValue::Left, L"left", 0xF5AD782B}, {CFX_CSSPropertyValue::TextTop, L"text-top", 0xFCB58D45}, }; -const int32_t g_iCSSPropertyValueCount = - sizeof(g_CFX_CSSPropertyValues) / sizeof(CFX_CSSPropertyValueTable); -static_assert(g_iCSSPropertyValueCount == - static_cast(CFX_CSSPropertyValue::LAST_MARKER), - "Property value table differs in size from property value enum"); struct CFX_CSSLengthUnitTable { uint16_t wHash; @@ -112,24 +108,19 @@ const CFX_CSSColorTable g_CFX_CSSColors[] = { {0xF6EFFF31, 0xff008000}, }; -const CFX_CSSPropertyValueTable* GetCSSPropertyValueByName( - const WideStringView& wsName) { - ASSERT(!wsName.IsEmpty()); - uint32_t dwHash = FX_HashCode_GetW(wsName, true); - int32_t iEnd = g_iCSSPropertyValueCount; - int32_t iMid, iStart = 0; - uint32_t dwMid; - do { - iMid = (iStart + iEnd) / 2; - dwMid = g_CFX_CSSPropertyValues[iMid].dwHash; - if (dwHash == dwMid) { - return g_CFX_CSSPropertyValues + iMid; - } else if (dwHash > dwMid) { - iStart = iMid + 1; - } else { - iEnd = iMid - 1; - } - } while (iStart <= iEnd); +const CFX_CSSPropertyValueEntry* GetCSSPropertyValueByName( + WideStringView wsName) { + if (wsName.IsEmpty()) + return nullptr; + + uint32_t hash = FX_HashCode_GetW(wsName, true); + auto* result = std::lower_bound( + std::begin(propertyValueTable), std::end(propertyValueTable), hash, + [](const CFX_CSSPropertyValueEntry& iter, const uint32_t& hash) { + return iter.dwHash < hash; + }); + if (result != std::end(propertyValueTable) && result->dwHash == hash) + return result; return nullptr; } @@ -439,7 +430,7 @@ RetainPtr CFX_CSSDeclaration::ParseNumber(const wchar_t* pszValue, RetainPtr CFX_CSSDeclaration::ParseEnum(const wchar_t* pszValue, int32_t iValueLen) { - const CFX_CSSPropertyValueTable* pValue = + const CFX_CSSPropertyValueEntry* pValue = GetCSSPropertyValueByName(WideStringView(pszValue, iValueLen)); return pValue ? pdfium::MakeRetain(pValue->eName) : nullptr; } @@ -497,7 +488,7 @@ void CFX_CSSDeclaration::ParseValueListProperty( } } if (dwType & CFX_CSSVALUETYPE_MaybeEnum) { - const CFX_CSSPropertyValueTable* pValue = + const CFX_CSSPropertyValueEntry* pValue = GetCSSPropertyValueByName(WideStringView(pszValue, iValueLen)); if (pValue) { list.push_back(pdfium::MakeRetain(pValue->eName)); @@ -614,7 +605,7 @@ bool CFX_CSSDeclaration::ParseBorderProperty( if (pColorItem) continue; - const CFX_CSSPropertyValueTable* pValue = + const CFX_CSSPropertyValueEntry* pValue = GetCSSPropertyValueByName(WideStringView(pszValue, iValueLen)); if (!pValue) continue; @@ -656,7 +647,7 @@ void CFX_CSSDeclaration::ParseFontProperty(const wchar_t* pszValue, while (parser.NextValue(&eType, &pszValue, &iValueLen)) { switch (eType) { case CFX_CSSPrimitiveType::String: { - const CFX_CSSPropertyValueTable* pValue = + const CFX_CSSPropertyValueEntry* pValue = GetCSSPropertyValueByName(WideStringView(pszValue, iValueLen)); if (pValue) { switch (pValue->eName) { -- cgit v1.2.3