Fix some nits in CXFA_NodeHelper.
- Pass WideStrings by const-ref.
- Use size_t for string lengths.
Change-Id: I52b3e027e734f5031e816eaff17fc1e409e4315a
Reviewed-on: https://pdfium-review.googlesource.com/c/47250
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fxfa/parser/cxfa_nodehelper.cpp b/xfa/fxfa/parser/cxfa_nodehelper.cpp
index 79c9834..0d4d09d 100644
--- a/xfa/fxfa/parser/cxfa_nodehelper.cpp
+++ b/xfa/fxfa/parser/cxfa_nodehelper.cpp
@@ -266,18 +266,18 @@
}
bool CXFA_NodeHelper::CreateNodeForCondition(const WideString& wsCondition) {
- int32_t iLen = wsCondition.GetLength();
+ size_t szLen = wsCondition.GetLength();
WideString wsIndex(L"0");
bool bAll = false;
- if (iLen == 0) {
+ if (szLen == 0) {
m_iCreateFlag = XFA_ResolveNode_RSType_CreateNodeOne;
return false;
}
if (wsCondition[0] != '[')
return false;
- int32_t i = 1;
- for (; i < iLen; ++i) {
+ size_t i = 1;
+ for (; i < szLen; ++i) {
wchar_t ch = wsCondition[i];
if (ch == ' ')
continue;
@@ -291,44 +291,46 @@
m_iCreateFlag = XFA_ResolveNode_RSType_CreateNodeAll;
} else {
m_iCreateFlag = XFA_ResolveNode_RSType_CreateNodeOne;
- wsIndex = wsCondition.Mid(i, iLen - 1 - i);
+ wsIndex = wsCondition.Mid(i, szLen - 1 - i);
}
- int32_t iIndex = wsIndex.GetInteger();
- m_iCreateCount = iIndex;
+ m_iCreateCount = wsIndex.GetInteger();
return true;
}
-bool CXFA_NodeHelper::CreateNode(WideString wsName,
- WideString wsCondition,
+bool CXFA_NodeHelper::CreateNode(const WideString& wsName,
+ const WideString& wsCondition,
bool bLastNode,
CFXJSE_Engine* pScriptContext) {
- if (!m_pCreateParent) {
+ ASSERT(!wsName.IsEmpty());
+
+ if (!m_pCreateParent)
return false;
- }
+
+ WideStringView wsNameView = wsName.AsStringView();
bool bIsClassName = false;
bool bResult = false;
- if (wsName[0] == '!') {
- wsName = wsName.Right(wsName.GetLength() - 1);
+ if (wsNameView[0] == '!') {
+ wsNameView = wsNameView.Right(wsNameView.GetLength() - 1);
m_pCreateParent = ToNode(
pScriptContext->GetDocument()->GetXFAObject(XFA_HASHCODE_Datasets));
}
- if (wsName[0] == '#') {
+ if (wsNameView[0] == '#') {
bIsClassName = true;
- wsName = wsName.Right(wsName.GetLength() - 1);
+ wsNameView = wsNameView.Right(wsNameView.GetLength() - 1);
}
- if (m_iCreateCount == 0) {
+ if (m_iCreateCount == 0)
CreateNodeForCondition(wsCondition);
- }
+
if (bIsClassName) {
- XFA_Element eType = XFA_GetElementByName(wsName.AsStringView());
+ XFA_Element eType = XFA_GetElementByName(wsNameView);
if (eType == XFA_Element::Unknown)
return false;
- for (int32_t iIndex = 0; iIndex < m_iCreateCount; iIndex++) {
+ for (int32_t i = 0; i < m_iCreateCount; ++i) {
CXFA_Node* pNewNode = m_pCreateParent->CreateSamePacketNode(eType);
if (pNewNode) {
m_pCreateParent->InsertChild(pNewNode, nullptr);
- if (iIndex == m_iCreateCount - 1) {
+ if (i == m_iCreateCount - 1) {
m_pCreateParent = pNewNode;
}
bResult = true;
@@ -339,23 +341,23 @@
if (bLastNode) {
eClassType = m_eLastCreateType;
}
- for (int32_t iIndex = 0; iIndex < m_iCreateCount; iIndex++) {
+ for (int32_t i = 0; i < m_iCreateCount; ++i) {
CXFA_Node* pNewNode = m_pCreateParent->CreateSamePacketNode(eClassType);
if (pNewNode) {
- pNewNode->JSObject()->SetAttribute(XFA_Attribute::Name,
- wsName.AsStringView(), false);
+ pNewNode->JSObject()->SetAttribute(XFA_Attribute::Name, wsNameView,
+ false);
pNewNode->CreateXMLMappingNode();
m_pCreateParent->InsertChild(pNewNode, nullptr);
- if (iIndex == m_iCreateCount - 1) {
+ if (i == m_iCreateCount - 1) {
m_pCreateParent = pNewNode;
}
bResult = true;
}
}
}
- if (!bResult) {
+ if (!bResult)
m_pCreateParent = nullptr;
- }
+
return bResult;
}
diff --git a/xfa/fxfa/parser/cxfa_nodehelper.h b/xfa/fxfa/parser/cxfa_nodehelper.h
index 5142e95..ba9a6ba 100644
--- a/xfa/fxfa/parser/cxfa_nodehelper.h
+++ b/xfa/fxfa/parser/cxfa_nodehelper.h
@@ -50,8 +50,8 @@
bool bIsClassIndex);
WideString GetNameExpression(CXFA_Node* refNode, bool bIsAllPath);
bool NodeIsTransparent(CXFA_Node* refNode);
- bool CreateNode(WideString wsName,
- WideString wsCondition,
+ bool CreateNode(const WideString& wsName,
+ const WideString& wsCondition,
bool bLastNode,
CFXJSE_Engine* pScriptContext);
bool CreateNodeForCondition(const WideString& wsCondition);