Remove handrolled search from GetCSSLengthUnitByName
BUG=pdfium:798
Change-Id: I97d0dc566f95736ef12eda90ee0bf336cba42098
Reviewed-on: https://pdfium-review.googlesource.com/24715
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Ryan Harrison <rharrison@chromium.org>
diff --git a/core/fxcrt/css/cfx_cssdeclaration.cpp b/core/fxcrt/css/cfx_cssdeclaration.cpp
index 278ffab..d901a95 100644
--- a/core/fxcrt/css/cfx_cssdeclaration.cpp
+++ b/core/fxcrt/css/cfx_cssdeclaration.cpp
@@ -77,19 +77,15 @@
{CFX_CSSPropertyValue::TextTop, L"text-top", 0xFCB58D45},
};
-struct CFX_CSSLengthUnitTable {
- uint16_t wHash;
- CFX_CSSNumberType wValue;
+struct CFX_CSSLengthUnitEntry {
+ const wchar_t* value;
+ CFX_CSSNumberType type;
};
-const CFX_CSSLengthUnitTable g_CFX_CSSLengthUnits[] = {
- {0x0672, CFX_CSSNumberType::EMS},
- {0x067D, CFX_CSSNumberType::EXS},
- {0x1AF7, CFX_CSSNumberType::Inches},
- {0x2F7A, CFX_CSSNumberType::MilliMeters},
- {0x3ED3, CFX_CSSNumberType::Picas},
- {0x3EE4, CFX_CSSNumberType::Points},
- {0x3EE8, CFX_CSSNumberType::Pixels},
- {0xFC30, CFX_CSSNumberType::CentiMeters},
+const CFX_CSSLengthUnitEntry lengthUnitTable[] = {
+ {L"cm", CFX_CSSNumberType::CentiMeters}, {L"em", CFX_CSSNumberType::EMS},
+ {L"ex", CFX_CSSNumberType::EXS}, {L"in", CFX_CSSNumberType::Inches},
+ {L"mm", CFX_CSSNumberType::MilliMeters}, {L"pc", CFX_CSSNumberType::Picas},
+ {L"pt", CFX_CSSNumberType::Points}, {L"px", CFX_CSSNumberType::Pixels},
};
struct CFX_CSSColorTable {
@@ -124,25 +120,18 @@
return nullptr;
}
-const CFX_CSSLengthUnitTable* GetCSSLengthUnitByName(
- const WideStringView& wsName) {
- ASSERT(!wsName.IsEmpty());
- uint16_t wHash = FX_HashCode_GetW(wsName, true);
- int32_t iEnd =
- sizeof(g_CFX_CSSLengthUnits) / sizeof(CFX_CSSLengthUnitTable) - 1;
- int32_t iMid, iStart = 0;
- uint16_t wMid;
- do {
- iMid = (iStart + iEnd) / 2;
- wMid = g_CFX_CSSLengthUnits[iMid].wHash;
- if (wHash == wMid) {
- return g_CFX_CSSLengthUnits + iMid;
- } else if (wHash > wMid) {
- iStart = iMid + 1;
- } else {
- iEnd = iMid - 1;
- }
- } while (iStart <= iEnd);
+const CFX_CSSLengthUnitEntry* GetCSSLengthUnitByName(WideStringView wsName) {
+ if (wsName.IsEmpty() || wsName.GetLength() != 2)
+ return nullptr;
+
+ WideString lowerName = WideString(wsName);
+ lowerName.MakeLower();
+
+ for (auto* iter = std::begin(lengthUnitTable);
+ iter != std::end(lengthUnitTable); ++iter) {
+ if (lowerName.Compare(iter->value) == 0)
+ return iter;
+ }
return nullptr;
}
@@ -182,10 +171,10 @@
if (iValueLen >= 1 && *pszValue == '%') {
eUnit = CFX_CSSNumberType::Percent;
} else if (iValueLen == 2) {
- const CFX_CSSLengthUnitTable* pUnit =
+ const CFX_CSSLengthUnitEntry* pUnit =
GetCSSLengthUnitByName(WideStringView(pszValue, 2));
if (pUnit)
- eUnit = pUnit->wValue;
+ eUnit = pUnit->type;
}
return true;
}