diff options
author | Tom Sepez <tsepez@chromium.org> | 2017-04-18 16:18:07 -0700 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-04-18 23:29:38 +0000 |
commit | 3420ba852babe24c7b03fede1b3b7c474c148c0d (patch) | |
tree | cd89ec87588122fbe5eee4646dedd05aec6589c2 | |
parent | ac07d340069e2f6e50d1e9aeae7140ce4d20a7de (diff) | |
download | pdfium-3420ba852babe24c7b03fede1b3b7c474c148c0d.tar.xz |
Remove hand-written bsearch from XFA_GetAttributeByName()
Also fix potential for collisions by checking exact name.
Change-Id: I48cd609d28a23f738f7a6c946820a688a4163024
Reviewed-on: https://pdfium-review.googlesource.com/4314
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
-rw-r--r-- | xfa/fxfa/parser/xfa_utils.cpp | 23 | ||||
-rw-r--r-- | xfa/fxfa/parser/xfa_utils_unittest.cpp | 9 |
2 files changed, 19 insertions, 13 deletions
diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp index 6bd04c322f..75d017b070 100644 --- a/xfa/fxfa/parser/xfa_utils.cpp +++ b/xfa/fxfa/parser/xfa_utils.cpp @@ -6,6 +6,8 @@ #include "xfa/fxfa/parser/xfa_utils.h" +#include <algorithm> + #include "core/fxcrt/fx_ext.h" #include "xfa/fde/xml/cfde_xmlchardata.h" #include "xfa/fde/xml/cfde_xmlelement.h" @@ -432,19 +434,14 @@ const XFA_ATTRIBUTEINFO* XFA_GetAttributeByName(const CFX_WideStringC& wsName) { if (wsName.IsEmpty()) return nullptr; - uint32_t uHash = FX_HashCode_GetW(wsName, false); - int32_t iStart = 0; - int32_t iEnd = g_iXFAAttributeCount - 1; - do { - int32_t iMid = (iStart + iEnd) / 2; - const XFA_ATTRIBUTEINFO* pInfo = g_XFAAttributeData + iMid; - if (uHash == pInfo->uHash) - return pInfo; - if (uHash < pInfo->uHash) - iEnd = iMid - 1; - else - iStart = iMid + 1; - } while (iStart <= iEnd); + auto* it = std::lower_bound(g_XFAAttributeData, + g_XFAAttributeData + g_iXFAAttributeCount, + FX_HashCode_GetW(wsName, false), + [](const XFA_ATTRIBUTEINFO& arg, uint32_t hash) { + return arg.uHash < hash; + }); + if (it != g_XFAAttributeData + g_iXFAAttributeCount && wsName == it->pName) + return it; return nullptr; } diff --git a/xfa/fxfa/parser/xfa_utils_unittest.cpp b/xfa/fxfa/parser/xfa_utils_unittest.cpp index e4dd09419e..1898a797ec 100644 --- a/xfa/fxfa/parser/xfa_utils_unittest.cpp +++ b/xfa/fxfa/parser/xfa_utils_unittest.cpp @@ -273,3 +273,12 @@ TEST_F(XFANodeIteratorTest, ChildAsRootNext) { EXPECT_EQ(child6(), iter.MoveToNext()); EXPECT_EQ(nullptr, iter.MoveToNext()); } + +TEST(XFAUtilsTest, GetAttributeByName) { + EXPECT_EQ(nullptr, XFA_GetAttributeByName(L"")); + EXPECT_EQ(nullptr, XFA_GetAttributeByName(L"clams")); + EXPECT_EQ(XFA_ATTRIBUTE_H, XFA_GetAttributeByName(L"h")->eName); + EXPECT_EQ(XFA_ATTRIBUTE_Short, XFA_GetAttributeByName(L"short")->eName); + EXPECT_EQ(XFA_ATTRIBUTE_DecipherOnly, + XFA_GetAttributeByName(L"decipherOnly")->eName); +} |