[Libreoffice-commits] core.git: vbahelper/source

Arkadiy Illarionov (via logerrit) logerrit at kemper.freedesktop.org
Thu Jun 20 07:46:10 UTC 2019


 vbahelper/source/msforms/vbacombobox.cxx           |    8 +-
 vbahelper/source/msforms/vbalistbox.cxx            |   31 ++--------
 vbahelper/source/msforms/vbalistcontrolhelper.cxx  |   27 ++------
 vbahelper/source/msforms/vbauserform.cxx           |    6 -
 vbahelper/source/vbahelper/vbacommandbarhelper.cxx |   16 ++---
 vbahelper/source/vbahelper/vbacommandbars.cxx      |   13 ----
 vbahelper/source/vbahelper/vbaeventshelperbase.cxx |    3 
 vbahelper/source/vbahelper/vbaglobalbase.cxx       |   20 ++----
 vbahelper/source/vbahelper/vbahelper.cxx           |   64 ++++++---------------
 vbahelper/source/vbahelper/vbashapes.cxx           |    9 +-
 10 files changed, 64 insertions(+), 133 deletions(-)

New commits:
commit 71a7e17dd7a6425a3df3c5dca25a3dac24ef6fa7
Author:     Arkadiy Illarionov <qarkai at gmail.com>
AuthorDate: Tue Jun 18 23:43:16 2019 +0300
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Thu Jun 20 09:44:52 2019 +0200

    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 at collabora.co.uk>

diff --git a/vbahelper/source/msforms/vbacombobox.cxx b/vbahelper/source/msforms/vbacombobox.cxx
index 851cee0a4098..3f4468d7d07a 100644
--- a/vbahelper/source/msforms/vbacombobox.cxx
+++ b/vbahelper/source/msforms/vbacombobox.cxx
@@ -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" );
diff --git a/vbahelper/source/msforms/vbalistbox.cxx b/vbahelper/source/msforms/vbalistbox.cxx
index 13c878440193..cb35e7b58b40 100644
--- a/vbahelper/source/msforms/vbalistbox.cxx
+++ b/vbahelper/source/msforms/vbalistbox.cxx
@@ -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
diff --git a/vbahelper/source/msforms/vbalistcontrolhelper.cxx b/vbahelper/source/msforms/vbalistcontrolhelper.cxx
index 9f72bda981c1..dfbbb19a3bde 100644
--- a/vbahelper/source/msforms/vbalistcontrolhelper.cxx
+++ b/vbahelper/source/msforms/vbalistcontrolhelper.cxx
@@ -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 ) );
diff --git a/vbahelper/source/msforms/vbauserform.cxx b/vbahelper/source/msforms/vbauserform.cxx
index 871630c9dd40..a22f15d41dae 100644
--- a/vbahelper/source/msforms/vbauserform.cxx
+++ b/vbahelper/source/msforms/vbauserform.cxx
@@ -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 ) );
diff --git a/vbahelper/source/vbahelper/vbacommandbarhelper.cxx b/vbahelper/source/vbahelper/vbacommandbarhelper.cxx
index e4237d356660..31dcb7d5667c 100644
--- a/vbahelper/source/vbahelper/vbacommandbarhelper.cxx
+++ b/vbahelper/source/vbahelper/vbacommandbarhelper.cxx
@@ -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;
diff --git a/vbahelper/source/vbahelper/vbacommandbars.cxx b/vbahelper/source/vbahelper/vbacommandbars.cxx
index 70772852f584..b771e3d541cf 100644
--- a/vbahelper/source/vbahelper/vbacommandbars.cxx
+++ b/vbahelper/source/vbahelper/vbacommandbars.cxx
@@ -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;
 }
 
diff --git a/vbahelper/source/vbahelper/vbaeventshelperbase.cxx b/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
index 99d49625c3e9..14c650f60de8 100644
--- a/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
+++ b/vbahelper/source/vbahelper/vbaeventshelperbase.cxx
@@ -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
         {
diff --git a/vbahelper/source/vbahelper/vbaglobalbase.cxx b/vbahelper/source/vbahelper/vbaglobalbase.cxx
index 4c65e9d4d398..902c07d82ffb 100644
--- a/vbahelper/source/vbahelper/vbaglobalbase.cxx
+++ b/vbahelper/source/vbahelper/vbaglobalbase.cxx
@@ -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;
 }
 
 
diff --git a/vbahelper/source/vbahelper/vbahelper.cxx b/vbahelper/source/vbahelper/vbahelper.cxx
index 28459c87db78..b55ad65c4fae 100644
--- a/vbahelper/source/vbahelper/vbahelper.cxx
+++ b/vbahelper/source/vbahelper/vbahelper.cxx
@@ -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;
 }
diff --git a/vbahelper/source/vbahelper/vbashapes.cxx b/vbahelper/source/vbahelper/vbashapes.cxx
index c3eeeaaa7f99..59e0d9df1fb5 100644
--- a/vbahelper/source/vbahelper/vbashapes.cxx
+++ b/vbahelper/source/vbahelper/vbashapes.cxx
@@ -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 );
 


More information about the Libreoffice-commits mailing list