Simplify Sequence iterations in vbahelper

Use range-based loops or replace with comphelper or STL functions

Change-Id: I7613057ba7063e04ca39a654f8a15c0354694783
Reviewed-on: https://gerrit.libreoffice.org/74309
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Arkadiy Illarionov
2019-06-20 09:44:52 +02:00
committed by Noel Grandin
parent 2a06796fa6
commit 71a7e17dd7
10 changed files with 64 additions and 133 deletions
+4 -4
View File
@@ -21,6 +21,7 @@
#include <filter/msfilter/msvbahelper.hxx>
#include <basic/sbstar.hxx>
#include <basic/sbmod.hxx>
#include <comphelper/sequence.hxx>
#include "vbanewfont.hxx"
#include <ooo/vba/msforms/fmStyle.hpp>
#include <ooo/vba/msforms/fmDropButtonStyle.hpp>
@@ -96,15 +97,14 @@ ScVbaComboBox::getListIndex()
if ( sItems.hasElements() )
{
OUString sText = getText();
sal_Int32 nLen = sItems.getLength();
for ( sal_Int32 index = 0; !sText.isEmpty() && index < nLen; ++index )
if (!sText.isEmpty())
{
if ( sItems[ index ] == sText )
sal_Int32 index = comphelper::findValue(sItems, sText);
if (index != -1)
{
SAL_INFO("vbahelper", "getListIndex returning " << index );
return uno::makeAny( index );
}
}
}
SAL_INFO("vbahelper", "getListIndex returning -1" );
+7 -24
View File
@@ -20,6 +20,7 @@
#include "vbalistbox.hxx"
#include "vbanewfont.hxx"
#include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
#include <comphelper/sequence.hxx>
#include <ooo/vba/msforms/fmMultiSelect.hpp>
using namespace com::sun::star;
@@ -77,17 +78,7 @@ ScVbaListBox::setValue( const uno::Any& _value )
OUString sValue = getAnyAsString( _value );
uno::Sequence< OUString > sList;
m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
sal_Int16 nValue = -1;
sal_Int16 i = 0;
for( i = 0; i < nLength; i++ )
{
if( sList[i] == sValue )
{
nValue = i;
break;
}
}
sal_Int16 nValue = static_cast<sal_Int16>(comphelper::findValue(sList, sValue));
if( nValue == -1 )
throw uno::RuntimeException( "Attribute use invalid." );
@@ -164,7 +155,7 @@ void SAL_CALL
ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex )
{
mpListHelper->AddItem( pvargItem, pvargIndex );
}
}
void SAL_CALL
ScVbaListBox::removeItem( const uno::Any& index )
@@ -196,9 +187,7 @@ ScVbaListBox::setValueEvent( const uno::Any& value )
{
if( nList[i] == nIndex )
{
if( bValue )
return;
else
if( !bValue )
{
for( ; i < nLength - 1; i++ )
{
@@ -208,8 +197,8 @@ ScVbaListBox::setValueEvent( const uno::Any& value )
//m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
fireClickEvent();
m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
return;
}
return;
}
}
if( bValue )
@@ -239,16 +228,10 @@ ScVbaListBox::getValueEvent()
{
uno::Sequence< sal_Int16 > nList;
m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
sal_Int32 nLength = nList.getLength();
sal_Int32 nIndex = m_nIndex;
bool bRet = std::find(nList.begin(), nList.end(), nIndex) != nList.end();
for( sal_Int32 i = 0; i < nLength; i++ )
{
if( nList[i] == nIndex )
return uno::makeAny( true );
}
return uno::makeAny( false );
return uno::makeAny( bRet );
}
void SAL_CALL
@@ -21,6 +21,7 @@
#include <vector>
#include <vbahelper/vbapropvalue.hxx>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <comphelper/sequence.hxx>
using namespace com::sun::star;
using namespace ooo::vba;
@@ -108,30 +109,21 @@ ListControlHelper::AddItem( const uno::Any& pvargItem, const uno::Any& pvargInde
// just copy those elements above the one to be inserted
std::vector< OUString > sVec;
// reserve just the amount we need to copy
sVec.reserve( sList.getLength() - nIndex );
sVec.reserve( sList.getLength() - nIndex + 1);
// point at first element to copy
OUString* pString = sList.getArray() + nIndex;
const OUString* pEndString = sList.getArray() + sList.getLength();
// insert the new element
sVec.push_back( sString );
// copy elements
for ( ; pString != pEndString; ++pString )
sVec.push_back( *pString );
// point at first element to copy
std::copy(std::next(sList.begin(), nIndex), sList.end(), std::back_inserter(sVec));
sList.realloc( sList.getLength() + 1 );
// point at first element to be overwritten
pString = sList.getArray() + nIndex;
pEndString = sList.getArray() + sList.getLength();
std::vector< OUString >::iterator it = sVec.begin();
for ( ; pString != pEndString; ++pString, ++it)
*pString = *it;
std::copy(sVec.begin(), sVec.end(), std::next(sList.begin(), nIndex));
}
m_xProps->setPropertyValue( "StringItemList", uno::makeAny( sList ) );
}
}
@@ -153,11 +145,8 @@ ListControlHelper::removeItem( const uno::Any& index )
Clear();
return;
}
for( sal_Int32 i = nIndex; i < ( sList.getLength()-1 ); i++ )
{
sList[i] = sList[i+1];
}
sList.realloc( sList.getLength() - 1 );
comphelper::removeElementAt(sList, nIndex);
}
m_xProps->setPropertyValue( "StringItemList", uno::makeAny( sList ) );
+2 -4
View File
@@ -234,12 +234,10 @@ ScVbaUserForm::nestedSearch( const OUString& aPropertyName, uno::Reference< awt:
if ( !xControl.is() )
{
uno::Sequence< uno::Reference< awt::XControl > > aControls = xContainer->getControls();
const uno::Reference< awt::XControl >* pCtrl = aControls.getConstArray();
const uno::Reference< awt::XControl >* pCtrlsEnd = pCtrl + aControls.getLength();
for ( ; pCtrl < pCtrlsEnd; ++pCtrl )
for ( const auto& rCtrl : aControls )
{
uno::Reference< awt::XControlContainer > xC( *pCtrl, uno::UNO_QUERY );
uno::Reference< awt::XControlContainer > xC( rCtrl, uno::UNO_QUERY );
if ( xC.is() )
{
xControl.set( nestedSearch( aPropertyName, xC ) );
@@ -186,15 +186,13 @@ OUString VbaCommandBarHelper::findToolbarByName( const css::uno::Reference< css:
return sResourceUrl;
uno::Sequence< OUString > allNames = xNameAccess->getElementNames();
for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
{
sResourceUrl = allNames[i];
if(sResourceUrl.startsWith( ITEM_TOOLBAR_URL ) )
{
if( hasToolbar( sResourceUrl, sName ) )
return sResourceUrl;
}
}
auto pName = std::find_if(allNames.begin(), allNames.end(),
[this, &sName](const OUString& rName) {
return rName.startsWith( ITEM_TOOLBAR_URL )
&& hasToolbar( rName, sName );
});
if (pName != allNames.end())
return *pName;
// the customize toolbars creating during importing, should found there.
sResourceUrl = "private:resource/toolbar/custom_" + sName;
+3 -10
View File
@@ -48,9 +48,7 @@ public:
}
virtual sal_Bool SAL_CALL hasMoreElements() override
{
if( m_nCurrentPosition < m_sNames.getLength() )
return true;
return false;
return m_nCurrentPosition < m_sNames.getLength();
}
virtual uno::Any SAL_CALL nextElement() override
{
@@ -188,13 +186,8 @@ ScVbaCommandBars::getCount()
// Filter out all toolbars from the window collection
sal_Int32 nCount = 1; // there is a Menubar in OOo
uno::Sequence< ::OUString > allNames = m_xNameAccess->getElementNames();
for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
{
if(allNames[i].indexOf( "private:resource/toolbar/" ) != -1 )
{
nCount++;
}
}
nCount += std::count_if(allNames.begin(), allNames.end(),
[](const OUString& rName) { return rName.indexOf( "private:resource/toolbar/" ) != -1; });
return nCount;
}
@@ -158,9 +158,8 @@ void SAL_CALL VbaEventsHelperBase::changesOccurred( const util::ChangesEvent& rE
return;
// process all changed modules
for( sal_Int32 nIndex = 0, nLength = rEvent.Changes.getLength(); nIndex < nLength; ++nIndex )
for( const util::ElementChange& rChange : rEvent.Changes )
{
const util::ElementChange& rChange = rEvent.Changes[ nIndex ];
OUString aModuleName;
if( (rChange.Accessor >>= aModuleName) && !aModuleName.isEmpty() ) try
{
+7 -13
View File
@@ -19,6 +19,7 @@
#include <vbahelper/vbaglobalbase.hxx>
#include <sal/macros.h>
#include <comphelper/sequence.hxx>
#include <cppuhelper/component_context.hxx>
#include <cppuhelper/exc_hlp.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
@@ -103,18 +104,17 @@ VbaGlobalsBase::~VbaGlobalsBase()
void
VbaGlobalsBase::init( const uno::Sequence< beans::PropertyValue >& aInitArgs )
{
sal_Int32 nLen = aInitArgs.getLength();
for ( sal_Int32 nIndex = 0; nIndex < nLen; ++nIndex )
for ( const auto& rInitArg : aInitArgs )
{
uno::Reference< container::XNameContainer > xNameContainer( mxContext, uno::UNO_QUERY_THROW );
if ( aInitArgs[ nIndex ].Name == gsApplication )
if ( rInitArg.Name == gsApplication )
{
xNameContainer->replaceByName( gsApplication, aInitArgs[ nIndex ].Value );
uno::Reference< XHelperInterface > xParent( aInitArgs[ nIndex ].Value, uno::UNO_QUERY );
xNameContainer->replaceByName( gsApplication, rInitArg.Value );
uno::Reference< XHelperInterface > xParent( rInitArg.Value, uno::UNO_QUERY );
mxParent = xParent;
}
else
xNameContainer->replaceByName( aInitArgs[ nIndex ].Name, aInitArgs[ nIndex ].Value );
xNameContainer->replaceByName( rInitArg.Name, rInitArg.Value );
}
}
@@ -160,13 +160,7 @@ bool
VbaGlobalsBase::hasServiceName( const OUString& serviceName )
{
uno::Sequence< OUString > sServiceNames( getAvailableServiceNames() );
sal_Int32 nLen = sServiceNames.getLength();
for ( sal_Int32 index = 0; index < nLen; ++index )
{
if ( sServiceNames[ index ] == serviceName )
return true;
}
return false;
return comphelper::findValue(sServiceNames, serviceName) != -1;
}
+21 -43
View File
@@ -41,6 +41,7 @@
#include <comphelper/automationinvokedzone.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/sequence.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <sfx2/objsh.hxx>
@@ -164,11 +165,7 @@ dispatchRequests (const uno::Reference< frame::XModel>& xModel, const OUString &
if ( nProps )
{
dispatchProps.realloc( nProps + 1 );
// need to acquire pDest after realloc
beans::PropertyValue* pDest = dispatchProps.getArray();
const beans::PropertyValue* pSrc = sProps.getConstArray();
for ( sal_Int32 index=0; index<nProps; ++index, ++pSrc, ++pDest )
*pDest = *pSrc;
std::copy(sProps.begin(), sProps.end(), dispatchProps.begin());
}
if ( xDispatcher.is() )
@@ -517,21 +514,17 @@ ContainerUtilities::getUniqueName( const uno::Sequence< OUString >& _slist, con
OUString
ContainerUtilities::getUniqueName( const uno::Sequence< OUString >& _slist, const OUString& _sElementName, const OUString& _sSuffixSeparator, sal_Int32 _nStartSuffix)
{
sal_Int32 a = _nStartSuffix;
OUString scompname = _sElementName;
sal_Int32 nLen = _slist.getLength();
if ( nLen == 0 )
if ( !_slist.hasElements() )
return _sElementName;
OUString scompname = _sElementName;
sal_Int32 a = _nStartSuffix;
for (;;)
{
for (sal_Int32 i = 0; i < nLen; i++)
{
if (FieldInList(_slist, scompname) == -1)
{
return scompname;
}
}
if (FieldInList(_slist, scompname) == -1)
return scompname;
scompname = _sElementName + _sSuffixSeparator + OUString::number( a++ );
}
}
@@ -539,20 +532,9 @@ ContainerUtilities::getUniqueName( const uno::Sequence< OUString >& _slist, cons
sal_Int32
ContainerUtilities::FieldInList( const uno::Sequence< OUString >& SearchList, const OUString& SearchString )
{
sal_Int32 FieldLen = SearchList.getLength();
sal_Int32 retvalue = -1;
for (sal_Int32 i = 0; i < FieldLen; i++)
{
// I wonder why comparing lexicographically is done
// when it's a match, is it interesting?
if ( SearchList[i] == SearchString )
{
retvalue = i;
break;
}
}
return retvalue;
// I wonder why comparing lexicographically is done
// when it's a match, is it interesting?
return comphelper::findValue(SearchList, SearchString);
}
static bool NeedEsc(sal_Unicode cCode)
@@ -757,25 +739,21 @@ void setDefaultPropByIntrospection( const uno::Any& aObj, const uno::Any& aValue
uno::Any getPropertyValue( const uno::Sequence< beans::PropertyValue >& aProp, const OUString& aName )
{
for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
{
if ( aProp[i].Name == aName )
{
return aProp[i].Value;
}
}
auto pProp = std::find_if(aProp.begin(), aProp.end(),
[&aName](const beans::PropertyValue& rProp) { return rProp.Name == aName; });
if (pProp != aProp.end())
return pProp->Value;
return uno::Any();
}
bool setPropertyValue( uno::Sequence< beans::PropertyValue >& aProp, const OUString& aName, const uno::Any& aValue )
{
for ( sal_Int32 i = 0; i < aProp.getLength(); i++ )
auto pProp = std::find_if(aProp.begin(), aProp.end(),
[&aName](const beans::PropertyValue& rProp) { return rProp.Name == aName; });
if (pProp != aProp.end())
{
if ( aProp[i].Name == aName )
{
aProp[i].Value = aValue;
return true;
}
pProp->Value = aValue;
return true;
}
return false;
}
+4 -5
View File
@@ -144,20 +144,19 @@ ScVbaShapes::getShapesByArrayIndices( const uno::Any& Index )
uno::Sequence< uno::Any > sIndices;
aConverted >>= sIndices;
XNamedObjectCollectionHelper< drawing::XShape >::XNamedVec aShapes;
sal_Int32 nElems = sIndices.getLength();
for( sal_Int32 index = 0; index < nElems; ++index )
for( const auto& rIndex : sIndices )
{
uno::Reference< drawing::XShape > xShape;
if ( sIndices[ index ].getValueTypeClass() == uno::TypeClass_STRING )
if ( rIndex.getValueTypeClass() == uno::TypeClass_STRING )
{
OUString sName;
sIndices[ index ] >>= sName;
rIndex >>= sName;
xShape.set( m_xNameAccess->getByName( sName ), uno::UNO_QUERY );
}
else
{
sal_Int32 nIndex = 0;
sIndices[ index ] >>= nIndex;
rIndex >>= nIndex;
// adjust for 1 based mso indexing
xShape.set( m_xIndexAccess->getByIndex( nIndex - 1 ), uno::UNO_QUERY );