From 0ae8e03cc2d310ba0ba19b878ea448f17a577cdb Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 30 Nov 2017 20:50:20 +0000 Subject: Create CXFA_Node::NameToAttributeEnum This CL removes XFA_GetAttributeEnumByName and moves the functionality into CXFA_Node::NameToAttriuteEnum. Change-Id: Id1484103c62bf7728d5406c22fb9d83fc0e032e1 Reviewed-on: https://pdfium-review.googlesource.com/19791 Reviewed-by: Henrique Nakashima Commit-Queue: dsinclair --- xfa/fxfa/parser/cxfa_node.cpp | 15 +++++++++++++++ xfa/fxfa/parser/cxfa_node.h | 3 +++ xfa/fxfa/parser/cxfa_node_unittest.cpp | 10 ++++++++++ xfa/fxfa/parser/cxfa_validatedata.cpp | 7 +++---- xfa/fxfa/parser/xfa_utils.cpp | 14 -------------- xfa/fxfa/parser/xfa_utils.h | 2 -- xfa/fxfa/parser/xfa_utils_unittest.cpp | 11 ----------- 7 files changed, 31 insertions(+), 31 deletions(-) (limited to 'xfa/fxfa') diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 99f0eea5a8..f24bddb990 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -141,6 +141,21 @@ WideString CXFA_Node::AttributeEnumToName(XFA_ATTRIBUTEENUM item) { return g_XFAEnumData[static_cast(item)].pName; } +// static +pdfium::Optional CXFA_Node::NameToAttributeEnum( + const WideStringView& name) { + if (name.IsEmpty()) + return {}; + + auto* it = std::lower_bound(g_XFAEnumData, g_XFAEnumData + g_iXFAEnumCount, + FX_HashCode_GetW(name, false), + [](const XFA_ATTRIBUTEENUMINFO& arg, + uint32_t hash) { return arg.uHash < hash; }); + if (it != g_XFAEnumData + g_iXFAEnumCount && name == it->pName) + return {it->eName}; + return {}; +} + CXFA_Node::CXFA_Node(CXFA_Document* pDoc, uint16_t ePacket, uint32_t validPackets, diff --git a/xfa/fxfa/parser/cxfa_node.h b/xfa/fxfa/parser/cxfa_node.h index 3d0b3db673..3e05f36fab 100644 --- a/xfa/fxfa/parser/cxfa_node.h +++ b/xfa/fxfa/parser/cxfa_node.h @@ -14,6 +14,7 @@ #include "core/fxcrt/fx_string.h" #include "fxjs/cjx_node.h" +#include "third_party/base/optional.h" #include "xfa/fxfa/parser/cxfa_object.h" class CFX_XMLNode; @@ -62,6 +63,8 @@ class CXFA_Node : public CXFA_Object { #endif // NDEBUG static WideString AttributeEnumToName(XFA_ATTRIBUTEENUM item); + static pdfium::Optional NameToAttributeEnum( + const WideStringView& name); static XFA_Attribute NameToAttribute(const WideStringView& name); static WideString AttributeToName(XFA_Attribute attr); static XFA_Element NameToElement(const WideString& name); diff --git a/xfa/fxfa/parser/cxfa_node_unittest.cpp b/xfa/fxfa/parser/cxfa_node_unittest.cpp index 93df084391..55360a6c47 100644 --- a/xfa/fxfa/parser/cxfa_node_unittest.cpp +++ b/xfa/fxfa/parser/cxfa_node_unittest.cpp @@ -15,3 +15,13 @@ TEST(CXFA_NodeTest, NameToAttribute) { EXPECT_EQ(XFA_Attribute::DecipherOnly, CXFA_Node::NameToAttribute(L"decipherOnly")); } + +TEST(CXFA_NodeTest, GetAttributeEnumByName) { + EXPECT_FALSE(!!CXFA_Node::NameToAttributeEnum(L"")); + EXPECT_FALSE(!!CXFA_Node::NameToAttributeEnum(L"nonesuch")); + EXPECT_EQ(XFA_ATTRIBUTEENUM_Asterisk, *CXFA_Node::NameToAttributeEnum(L"*")); + EXPECT_EQ(XFA_ATTRIBUTEENUM_Visible, + *CXFA_Node::NameToAttributeEnum(L"visible")); + EXPECT_EQ(XFA_ATTRIBUTEENUM_Lowered, + *CXFA_Node::NameToAttributeEnum(L"lowered")); +} diff --git a/xfa/fxfa/parser/cxfa_validatedata.cpp b/xfa/fxfa/parser/cxfa_validatedata.cpp index 8b3f539d48..dd45b5295b 100644 --- a/xfa/fxfa/parser/cxfa_validatedata.cpp +++ b/xfa/fxfa/parser/cxfa_validatedata.cpp @@ -24,11 +24,10 @@ XFA_ATTRIBUTEENUM CXFA_ValidateData::GetFormatTest() const { } void CXFA_ValidateData::SetNullTest(const WideString& wsValue) { - const XFA_ATTRIBUTEENUMINFO* pInfo = - XFA_GetAttributeEnumByName(wsValue.AsStringView()); + pdfium::Optional item = + CXFA_Node::NameToAttributeEnum(wsValue.AsStringView()); m_pNode->JSNode()->SetEnum(XFA_Attribute::NullTest, - pInfo ? pInfo->eName : XFA_ATTRIBUTEENUM_Disabled, - false); + item ? *item : XFA_ATTRIBUTEENUM_Disabled, false); } XFA_ATTRIBUTEENUM CXFA_ValidateData::GetNullTest() const { diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp index 9dc34407bd..e9315763b7 100644 --- a/xfa/fxfa/parser/xfa_utils.cpp +++ b/xfa/fxfa/parser/xfa_utils.cpp @@ -186,20 +186,6 @@ const XFA_SCRIPTATTRIBUTEINFO* XFA_GetScriptAttributeByName( return nullptr; } -const XFA_ATTRIBUTEENUMINFO* XFA_GetAttributeEnumByName( - const WideStringView& wsName) { - if (wsName.IsEmpty()) - return nullptr; - - auto* it = std::lower_bound(g_XFAEnumData, g_XFAEnumData + g_iXFAEnumCount, - FX_HashCode_GetW(wsName, false), - [](const XFA_ATTRIBUTEENUMINFO& arg, - uint32_t hash) { return arg.uHash < hash; }); - if (it != g_XFAEnumData + g_iXFAEnumCount && wsName == it->pName) - return it; - return nullptr; -} - const XFA_PACKETINFO* XFA_GetPacketByIndex(XFA_PacketType ePacket) { return g_XFAPacketData + static_cast(ePacket); } diff --git a/xfa/fxfa/parser/xfa_utils.h b/xfa/fxfa/parser/xfa_utils.h index 40e7d7440b..889dcc0f0a 100644 --- a/xfa/fxfa/parser/xfa_utils.h +++ b/xfa/fxfa/parser/xfa_utils.h @@ -42,8 +42,6 @@ const XFA_SCRIPTATTRIBUTEINFO* XFA_GetScriptAttributeByName( XFA_Element eElement, const WideStringView& wsAttributeName); -const XFA_ATTRIBUTEENUMINFO* XFA_GetAttributeEnumByName( - const WideStringView& wsName); const XFA_PACKETINFO* XFA_GetPacketByIndex(XFA_PacketType ePacket); const XFA_PACKETINFO* XFA_GetPacketByID(uint32_t dwPacket); diff --git a/xfa/fxfa/parser/xfa_utils_unittest.cpp b/xfa/fxfa/parser/xfa_utils_unittest.cpp index b27cdaa36e..c9f662eb8d 100644 --- a/xfa/fxfa/parser/xfa_utils_unittest.cpp +++ b/xfa/fxfa/parser/xfa_utils_unittest.cpp @@ -22,14 +22,3 @@ TEST(XfaUtilsImpTest, XFA_MapRotation) { XFA_MapRotation(TestCases[i].input)); } } - -TEST(XFAUtilsTest, GetAttributeEnumByName) { - EXPECT_EQ(nullptr, XFA_GetAttributeEnumByName(L"")); - EXPECT_EQ(nullptr, XFA_GetAttributeEnumByName(L"nonesuch")); - EXPECT_EQ(XFA_ATTRIBUTEENUM_Asterisk, - XFA_GetAttributeEnumByName(L"*")->eName); - EXPECT_EQ(XFA_ATTRIBUTEENUM_Visible, - XFA_GetAttributeEnumByName(L"visible")->eName); - EXPECT_EQ(XFA_ATTRIBUTEENUM_Lowered, - XFA_GetAttributeEnumByName(L"lowered")->eName); -} -- cgit v1.2.3