From 8d5779bef3cb36f0db6f61b6a9f711796d487d9d Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Tue, 11 Sep 2018 23:18:20 +0300 Subject: Simplify containers iterations in xmlsecurity Use range-based loop or replace with functions from std algorithm. Change-Id: I0146186b7c42405076dfce7de7805be4228cc6d3 Reviewed-on: https://gerrit.libreoffice.org/60360 Tested-by: Jenkins Reviewed-by: Noel Grandin --- xmlsecurity/source/framework/buffernode.cxx | 80 ++++++---------------- .../source/framework/saxeventkeeperimpl.cxx | 70 ++++++++----------- xmlsecurity/source/framework/signatureengine.cxx | 14 ++-- 3 files changed, 57 insertions(+), 107 deletions(-) (limited to 'xmlsecurity/source/framework') diff --git a/xmlsecurity/source/framework/buffernode.cxx b/xmlsecurity/source/framework/buffernode.cxx index fb58e7b6232a..d1cd8d17ed44 100644 --- a/xmlsecurity/source/framework/buffernode.cxx +++ b/xmlsecurity/source/framework/buffernode.cxx @@ -803,35 +803,18 @@ bool BufferNode::isECInSubTreeIncluded(sal_Int32 nIgnoredSecurityId) const * bExist - true if a match found, false otherwise. ******************************************************************************/ { - bool rc = false; - - std::vector< const ElementCollector* >::const_iterator jj = m_vElementCollectors.begin(); - - for( ; jj != m_vElementCollectors.end() ; ++jj ) - { - ElementCollector* pElementCollector = const_cast(*jj); - if (nIgnoredSecurityId == cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID || - pElementCollector->getSecurityId() != nIgnoredSecurityId) - { - rc = true; - break; - } - } + bool rc = std::any_of(m_vElementCollectors.begin(), m_vElementCollectors.end(), + [nIgnoredSecurityId](const ElementCollector* pElementCollector) { + return nIgnoredSecurityId == cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID || + pElementCollector->getSecurityId() != nIgnoredSecurityId; + }); if ( !rc ) { - std::vector< const BufferNode* >::const_iterator ii = m_vChildren.begin(); - - for( ; ii != m_vChildren.end() ; ++ii ) - { - BufferNode* pBufferNode = const_cast(*ii); - - if ( pBufferNode->isECInSubTreeIncluded(nIgnoredSecurityId)) - { - rc = true; - break; - } - } + rc = std::any_of(m_vChildren.begin(), m_vChildren.end(), + [nIgnoredSecurityId](const BufferNode* pBufferNode) { + return pBufferNode->isECInSubTreeIncluded(nIgnoredSecurityId); + }); } return rc; @@ -904,31 +887,14 @@ bool BufferNode::isBlockerInSubTreeIncluded(sal_Int32 nIgnoredSecurityId) const * bExist - true if a match found, false otherwise. ******************************************************************************/ { - bool rc = false; - - std::vector< const BufferNode* >::const_iterator ii = m_vChildren.begin(); - - for( ; ii != m_vChildren.end() ; ++ii ) - { - BufferNode* pBufferNode = const_cast(*ii); - ElementMark* pBlocker = pBufferNode->getBlocker(); - - if (pBlocker != nullptr && - (nIgnoredSecurityId == cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID || - pBlocker->getSecurityId() != nIgnoredSecurityId )) - { - rc = true; - break; - } - - if (rc || pBufferNode->isBlockerInSubTreeIncluded(nIgnoredSecurityId)) - { - rc = true; - break; - } - } - - return rc; + return std::any_of(m_vChildren.begin(), m_vChildren.end(), + [nIgnoredSecurityId](const BufferNode* pBufferNode) { + ElementMark* pBlocker = pBufferNode->getBlocker(); + return (pBlocker != nullptr && + (nIgnoredSecurityId == cssxc::sax::ConstOfSecurityId::UNDEFINEDSECURITYID || + pBlocker->getSecurityId() != nIgnoredSecurityId )) || + pBufferNode->isBlockerInSubTreeIncluded(nIgnoredSecurityId); + }); } const BufferNode* BufferNode::getNextChild(const BufferNode* pChild) const @@ -954,16 +920,15 @@ const BufferNode* BufferNode::getNextChild(const BufferNode* pChild) const BufferNode* rc = nullptr; bool bChildFound = false; - std::vector< const BufferNode* >::const_iterator ii = m_vChildren.begin(); - for( ; ii != m_vChildren.end() ; ++ii ) + for( const BufferNode* i : m_vChildren ) { if (bChildFound) { - rc = const_cast(*ii); + rc = const_cast(i); break; } - if( *ii == pChild ) + if( i == pChild ) { bChildFound = true; } @@ -992,10 +957,9 @@ void BufferNode::freeAllChildren() * empty ******************************************************************************/ { - std::vector< const BufferNode* >::const_iterator ii = m_vChildren.begin(); - for( ; ii != m_vChildren.end() ; ++ii ) + for( const BufferNode* i : m_vChildren ) { - BufferNode *pChild = const_cast(*ii); + BufferNode *pChild = const_cast(i); pChild->freeAllChildren(); delete pChild; } diff --git a/xmlsecurity/source/framework/saxeventkeeperimpl.cxx b/xmlsecurity/source/framework/saxeventkeeperimpl.cxx index 5fea1e76fe3b..2c60c950f605 100644 --- a/xmlsecurity/source/framework/saxeventkeeperimpl.cxx +++ b/xmlsecurity/source/framework/saxeventkeeperimpl.cxx @@ -176,10 +176,9 @@ BufferNode* SAXEventKeeperImpl::addNewElementMarkBuffers() if (!m_vNewElementCollectors.empty()) { - for( auto ii = m_vNewElementCollectors.begin(); - ii != m_vNewElementCollectors.end(); ++ii ) + for( const auto& i : m_vNewElementCollectors ) { - pBufferNode->addElementCollector(*ii); + pBufferNode->addElementCollector(i); } m_vNewElementCollectors.clear(); @@ -240,36 +239,30 @@ void SAXEventKeeperImpl::removeElementMarkBuffer(sal_Int32 nId) * nId - the Id of the ElementMark to be removed. ******************************************************************************/ { - for( auto ii = m_vElementMarkBuffers.begin(); - ii != m_vElementMarkBuffers.end(); ++ii ) - { - if ( nId == (*ii)->getBufferId()) - { - /* - * checks whether this ElementMark still in the new ElementCollect array - */ - for( auto jj = m_vNewElementCollectors.begin(); - jj != m_vNewElementCollectors.end(); ++jj ) - { - if (ii->get() == (*jj)) - { - m_vNewElementCollectors.erase(jj); - break; - } - } + auto ii = std::find_if(m_vElementMarkBuffers.begin(), m_vElementMarkBuffers.end(), + [nId](std::unique_ptr& rElementMark) { return nId == rElementMark->getBufferId(); } + ); + if (ii == m_vElementMarkBuffers.end()) + return; - /* - * checks whether this ElementMark is the new Blocker - */ - if (ii->get() == m_pNewBlocker) - { - m_pNewBlocker = nullptr; - } + /* + * checks whether this ElementMark still in the new ElementCollect array + */ + auto jj = std::find_if(m_vNewElementCollectors.begin(), m_vNewElementCollectors.end(), + [&ii](const ElementCollector* pElementCollector) { return ii->get() == pElementCollector; } + ); + if (jj != m_vNewElementCollectors.end()) + m_vNewElementCollectors.erase(jj); - m_vElementMarkBuffers.erase( ii ); - break; - } + /* + * checks whether this ElementMark is the new Blocker + */ + if (ii->get() == m_pNewBlocker) + { + m_pNewBlocker = nullptr; } + + m_vElementMarkBuffers.erase( ii ); } OUString SAXEventKeeperImpl::printBufferNode( @@ -338,10 +331,9 @@ OUString SAXEventKeeperImpl::printBufferNode( rc.append("\n"); std::vector< const BufferNode* >* vChildren = pBufferNode->getChildren(); - for( auto jj = vChildren->begin(); - jj != vChildren->end(); ++jj ) + for( const auto& jj : *vChildren ) { - rc.append(printBufferNode(*jj, nIndent+4)); + rc.append(printBufferNode(jj, nIndent+4)); } delete vChildren; @@ -373,10 +365,9 @@ cssu::Sequence< cssu::Reference< cssxw::XXMLElementWrapper > > cssxw::XXMLElementWrapper > > aChildrenCollection ( vChildren->size()); sal_Int32 nIndex = 0; - for( auto ii = vChildren->begin(); - ii != vChildren->end(); ++ii ) + for( const auto& i : *vChildren ) { - aChildrenCollection[nIndex] = (*ii)->getXMLElement(); + aChildrenCollection[nIndex] = i->getXMLElement(); nIndex++; } @@ -515,11 +506,10 @@ void SAXEventKeeperImpl::smashBufferNode( pParent->removeChild(pBufferNode); pBufferNode->setParent(nullptr); - for( auto ii = vChildren->begin(); - ii != vChildren->end(); ++ii ) + for( const auto& i : *vChildren ) { - const_cast(*ii)->setParent(pParent); - pParent->addChild(*ii, nIndex); + const_cast(i)->setParent(pParent); + pParent->addChild(i, nIndex); nIndex++; } diff --git a/xmlsecurity/source/framework/signatureengine.cxx b/xmlsecurity/source/framework/signatureengine.cxx index bffcb68bad51..b5a872deb168 100644 --- a/xmlsecurity/source/framework/signatureengine.cxx +++ b/xmlsecurity/source/framework/signatureengine.cxx @@ -98,11 +98,9 @@ void SignatureEngine::tryToPerform( ) xSignatureTemplate->setTemplate(xXMLElement); - std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin(); - - for( ; ii != m_vReferenceIds.end() ; ++ii ) + for( const auto i : m_vReferenceIds ) { - xXMLElement = m_xSAXEventKeeper->getElement( *ii ); + xXMLElement = m_xSAXEventKeeper->getElement( i ); xSignatureTemplate->setTarget(xXMLElement); } @@ -145,14 +143,12 @@ void SignatureEngine::clearUp( ) const m_xSAXEventKeeper->removeElementCollector(m_nIdOfTemplateEC); - std::vector< sal_Int32 >::const_iterator ii = m_vReferenceIds.begin(); - - for( ; ii != m_vReferenceIds.end() ; ++ii ) + for( const auto& i : m_vReferenceIds ) { xReferenceResolvedBroadcaster->removeReferenceResolvedListener( - *ii, + i, static_cast >(static_cast(const_cast(this)))); - m_xSAXEventKeeper->removeElementCollector(*ii); + m_xSAXEventKeeper->removeElementCollector(i); } if (m_nIdOfKeyEC != 0 && m_nIdOfKeyEC != -1) -- cgit