include/tools/multisel.hxx | 7 +++-- tools/source/memtools/multisel.cxx | 48 ++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 25 deletions(-)
New commits: commit 6af99a90d059446d028cb6fe94c7c74140f2ed02 Author: Stephan Bergmann <sberg...@redhat.com> AuthorDate: Fri Jun 3 13:44:20 2022 +0200 Commit: Stephan Bergmann <sberg...@redhat.com> CommitDate: Fri Jun 3 15:10:12 2022 +0200 Use more appropriate type for MultiSelection::nCurSubSel et al (In MultiSelection::LastSelected, there is no need to set nCurSubSel (to a potentially negative value) before setting bCurValid to false, as nCurSubSel is only ever used after checking that bCurValid is true.) Change-Id: I3c23c89fbb7b4ef962436476f6576af9fe623fd0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135354 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sberg...@redhat.com> diff --git a/include/tools/multisel.hxx b/include/tools/multisel.hxx index a5c68ca0eb23..ea7a1343feeb 100644 --- a/include/tools/multisel.hxx +++ b/include/tools/multisel.hxx @@ -23,6 +23,7 @@ #include <tools/gen.hxx> #include <rtl/ustring.hxx> +#include <cstddef> #include <vector> #include <o3tl/sorted_vector.hxx> @@ -34,14 +35,14 @@ private: std::vector< Range > aSels; // array of SV-selections Range aTotRange; // total range of indexes - sal_Int32 nCurSubSel; // index in aSels of current selected index + std::size_t nCurSubSel; // index in aSels of current selected index sal_Int32 nCurIndex; // current selected entry sal_Int32 nSelCount; // number of selected indexes bool bCurValid; // are nCurIndex and nCurSubSel valid TOOLS_DLLPRIVATE void ImplClear(); - TOOLS_DLLPRIVATE sal_Int32 ImplFindSubSelection( sal_Int32 nIndex ) const; - TOOLS_DLLPRIVATE void ImplMergeSubSelections( sal_Int32 nPos1, sal_Int32 nPos2 ); + TOOLS_DLLPRIVATE std::size_t ImplFindSubSelection( sal_Int32 nIndex ) const; + TOOLS_DLLPRIVATE void ImplMergeSubSelections( sal_Int32 nPos1, std::size_t nPos2 ); public: MultiSelection(); diff --git a/tools/source/memtools/multisel.cxx b/tools/source/memtools/multisel.cxx index c6dd9d8c3146..ff81f6c14ccc 100644 --- a/tools/source/memtools/multisel.cxx +++ b/tools/source/memtools/multisel.cxx @@ -17,6 +17,10 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <sal/config.h> + +#include <cstddef> + #include <tools/debug.hxx> #include <tools/multisel.hxx> @@ -29,20 +33,20 @@ void MultiSelection::ImplClear() aSels.clear(); } -sal_Int32 MultiSelection::ImplFindSubSelection( sal_Int32 nIndex ) const +std::size_t MultiSelection::ImplFindSubSelection( sal_Int32 nIndex ) const { // iterate through the sub selections - sal_Int32 n = 0; + std::size_t n = 0; for ( ; - n < sal_Int32(aSels.size()) && nIndex > aSels[ n ].Max(); + n < aSels.size() && nIndex > aSels[ n ].Max(); ++n ) {} /* empty loop */ return n; } -void MultiSelection::ImplMergeSubSelections( sal_Int32 nPos1, sal_Int32 nPos2 ) +void MultiSelection::ImplMergeSubSelections( sal_Int32 nPos1, std::size_t nPos2 ) { // didn't a sub selection at nPos2 exist? - if ( nPos2 >= sal_Int32(aSels.size()) ) + if ( nPos2 >= aSels.size() ) return; // did the sub selections touch each other? @@ -141,12 +145,12 @@ bool MultiSelection::Select( sal_Int32 nIndex, bool bSelect ) return false; // find the virtual target position - sal_Int32 nSubSelPos = ImplFindSubSelection( nIndex ); + std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); if ( bSelect ) { // is it included in the found sub selection? - if ( nSubSelPos < sal_Int32(aSels.size()) && aSels[ nSubSelPos ].Contains( nIndex ) ) + if ( nSubSelPos < aSels.size() && aSels[ nSubSelPos ].Contains( nIndex ) ) // already selected, nothing to do return false; @@ -164,7 +168,7 @@ bool MultiSelection::Select( sal_Int32 nIndex, bool bSelect ) ImplMergeSubSelections( nSubSelPos-1, nSubSelPos ); } // is it at the beginning of the found sub selection - else if ( nSubSelPos < sal_Int32(aSels.size()) + else if ( nSubSelPos < aSels.size() && aSels[ nSubSelPos ].Min() == (nIndex+1) ) // expand the found sub selection @@ -172,7 +176,7 @@ bool MultiSelection::Select( sal_Int32 nIndex, bool bSelect ) else { // create a new sub selection - if ( nSubSelPos < sal_Int32(aSels.size()) ) { + if ( nSubSelPos < aSels.size() ) { aSels.insert( aSels.begin() + nSubSelPos, Range( nIndex, nIndex ) ); } else { aSels.push_back( Range( nIndex, nIndex ) ); @@ -184,7 +188,7 @@ bool MultiSelection::Select( sal_Int32 nIndex, bool bSelect ) else { // is it excluded from the found sub selection? - if ( nSubSelPos >= sal_Int32(aSels.size()) + if ( nSubSelPos >= aSels.size() || !aSels[ nSubSelPos ].Contains( nIndex ) ) { // not selected, nothing to do @@ -212,7 +216,7 @@ bool MultiSelection::Select( sal_Int32 nIndex, bool bSelect ) else { // split the sub selection - if ( nSubSelPos < sal_Int32(aSels.size()) ) { + if ( nSubSelPos < aSels.size() ) { aSels.insert( aSels.begin() + nSubSelPos, Range( aSels[ nSubSelPos ].Min(), nIndex-1 ) ); } else { aSels.push_back( Range( aSels[ nSubSelPos ].Min(), nIndex-1 ) ); @@ -302,23 +306,23 @@ void MultiSelection::Select( const Range& rIndexRange, bool bSelect ) bool MultiSelection::IsSelected( sal_Int32 nIndex ) const { // find the virtual target position - sal_Int32 nSubSelPos = ImplFindSubSelection( nIndex ); + std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); - return nSubSelPos < sal_Int32(aSels.size()) && aSels[ nSubSelPos ].Contains(nIndex); + return nSubSelPos < aSels.size() && aSels[ nSubSelPos ].Contains(nIndex); } void MultiSelection::Insert( sal_Int32 nIndex, sal_Int32 nCount ) { // find the virtual target position - sal_Int32 nSubSelPos = ImplFindSubSelection( nIndex ); + std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); // did we need to shift the sub selections? - if ( nSubSelPos < sal_Int32(aSels.size()) ) + if ( nSubSelPos < aSels.size() ) { // did we insert an unselected into an existing sub selection? if ( aSels[ nSubSelPos ].Min() != nIndex && aSels[ nSubSelPos ].Contains(nIndex) ) { // split the sub selection - if ( nSubSelPos < sal_Int32(aSels.size()) ) { + if ( nSubSelPos < aSels.size() ) { aSels.insert( aSels.begin() + nSubSelPos, Range( aSels[ nSubSelPos ].Min(), nIndex-1 ) ); } else { aSels.push_back( Range( aSels[ nSubSelPos ].Min(), nIndex-1 ) ); @@ -328,7 +332,7 @@ void MultiSelection::Insert( sal_Int32 nIndex, sal_Int32 nCount ) } // shift the sub selections behind the inserting position - for ( sal_Int32 nPos = nSubSelPos; nPos < sal_Int32(aSels.size()); ++nPos ) + for ( std::size_t nPos = nSubSelPos; nPos < aSels.size(); ++nPos ) { aSels[ nPos ].Min() += nCount; aSels[ nPos ].Max() += nCount; @@ -342,10 +346,10 @@ void MultiSelection::Insert( sal_Int32 nIndex, sal_Int32 nCount ) void MultiSelection::Remove( sal_Int32 nIndex ) { // find the virtual target position - sal_Int32 nSubSelPos = ImplFindSubSelection( nIndex ); + std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); // did we remove from an existing sub selection? - if ( nSubSelPos < sal_Int32(aSels.size()) + if ( nSubSelPos < aSels.size() && aSels[ nSubSelPos ].Contains(nIndex) ) { // does this sub selection only contain the index to be deleted @@ -362,7 +366,7 @@ void MultiSelection::Remove( sal_Int32 nIndex ) } // shift the sub selections behind the removed index - for ( sal_Int32 nPos = nSubSelPos; nPos < sal_Int32(aSels.size()); ++nPos ) + for ( std::size_t nPos = nSubSelPos; nPos < aSels.size(); ++nPos ) { --( aSels[ nPos ].Min() ); --( aSels[ nPos ].Max() ); @@ -386,12 +390,12 @@ sal_Int32 MultiSelection::FirstSelected() sal_Int32 MultiSelection::LastSelected() { - nCurSubSel = aSels.size() - 1; bCurValid = !aSels.empty(); if ( !bCurValid ) return SFX_ENDOFSELECTION; + nCurSubSel = aSels.size() - 1; nCurIndex = aSels[ nCurSubSel ].Max(); return nCurIndex; } @@ -406,7 +410,7 @@ sal_Int32 MultiSelection::NextSelected() return ++nCurIndex; // are there further sub selections? - if ( ++nCurSubSel >= sal_Int32(aSels.size()) ) + if ( ++nCurSubSel >= aSels.size() ) // we are at the end! return SFX_ENDOFSELECTION;