From d5fd8e4aa5041b2402b703d824d8736bd5407231 Mon Sep 17 00:00:00 2001 From: dsinclair Date: Thu, 30 Jun 2016 19:55:30 -0700 Subject: Convert some methods to std::lower_bound and std::find_if. This CL updates XFA_GetElementTypeForName() to use std::lower_bound instead of a custom binary search implementation to find the element info. XFA_GetPropertyOfElement() is changed to use std::find_if. Previously it was using a custom binary search implementation. Changing this to non-binary search will allow having the items in g_XFAElementPropertyData be out of XFA_Element order. There are at most 19 items that need to be searched and often 1 or 2. Review-Url: https://codereview.chromium.org/2107093004 --- xfa/fxfa/parser/xfa_basic_imp.cpp | 54 ++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/xfa/fxfa/parser/xfa_basic_imp.cpp b/xfa/fxfa/parser/xfa_basic_imp.cpp index 3d630d9247..23592830d0 100644 --- a/xfa/fxfa/parser/xfa_basic_imp.cpp +++ b/xfa/fxfa/parser/xfa_basic_imp.cpp @@ -180,20 +180,16 @@ XFA_Element XFA_GetElementTypeForName(const CFX_WideStringC& wsName) { return XFA_Element::Unknown; uint32_t uHash = FX_HashCode_GetW(wsName, false); - int32_t iStart = 0; - int32_t iEnd = g_iXFAElementCount - 1; - do { - int32_t iMid = (iStart + iEnd) / 2; - const XFA_ELEMENTINFO* pInfo = g_XFAElementData + iMid; - if (uHash == pInfo->uHash) - return pInfo->eName; - if (uHash < pInfo->uHash) - iEnd = iMid - 1; - else - iStart = iMid + 1; - } while (iStart <= iEnd); + const XFA_ELEMENTINFO* pEnd = g_XFAElementData + g_iXFAElementCount; + auto pInfo = std::lower_bound(g_XFAElementData, pEnd, uHash, + [](const XFA_ELEMENTINFO& info, uint32_t hash) { + return info.uHash < hash; + }); + if (pInfo < pEnd && pInfo->uHash == uHash) + return pInfo->eName; return XFA_Element::Unknown; } + const XFA_ELEMENTINFO* XFA_GetElementByID(XFA_Element eName) { return eName == XFA_Element::Unknown ? nullptr @@ -238,35 +234,29 @@ const XFA_PROPERTY* XFA_GetElementProperties(XFA_Element eElement, iCount = pElement->wCount; return g_XFAElementPropertyData + pElement->wStart; } + const XFA_PROPERTY* XFA_GetPropertyOfElement(XFA_Element eElement, XFA_Element eProperty, uint32_t dwPacket) { int32_t iCount = 0; - const XFA_PROPERTY* pProperty = XFA_GetElementProperties(eElement, iCount); - if (!pProperty || iCount < 1) { + const XFA_PROPERTY* pProperties = XFA_GetElementProperties(eElement, iCount); + if (!pProperties || iCount < 1) return nullptr; - } - int32_t iStart = 0, iEnd = iCount - 1, iMid; - do { - iMid = (iStart + iEnd) / 2; - XFA_Element eName = pProperty[iMid].eName; - if (eProperty == eName) { - break; - } else if (eProperty < eName) { - iEnd = iMid - 1; - } else { - iStart = iMid + 1; - } - } while (iStart <= iEnd); - if (iStart > iEnd) { + + auto it = std::find_if(pProperties, pProperties + iCount, + [eProperty](const XFA_PROPERTY& prop) { + return prop.eName == eProperty; + }); + if (it == pProperties + iCount) return nullptr; - } + const XFA_ELEMENTINFO* pInfo = XFA_GetElementByID(eProperty); ASSERT(pInfo); - if (dwPacket == XFA_XDPPACKET_UNKNOWN) - return pProperty + iMid; - return (dwPacket & pInfo->dwPackets) ? (pProperty + iMid) : nullptr; + if (dwPacket != XFA_XDPPACKET_UNKNOWN && !(dwPacket & pInfo->dwPackets)) + return nullptr; + return it; } + const XFA_NOTSUREATTRIBUTE* XFA_GetNotsureAttribute(XFA_Element eElement, XFA_ATTRIBUTE eAttribute, XFA_ATTRIBUTETYPE eType) { -- cgit v1.2.3