Coverage Report

Created: 2026-07-10 11:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/tools/multisel.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
#ifndef INCLUDED_TOOLS_MULTISEL_HXX
20
#define INCLUDED_TOOLS_MULTISEL_HXX
21
22
#include <tools/toolsdllapi.h>
23
#include <tools/gen.hxx>
24
25
#include <vector>
26
#include <o3tl/sorted_vector.hxx>
27
28
13.7k
#define SFX_ENDOFSELECTION      (-1)
29
30
class SAL_WARN_UNUSED TOOLS_DLLPUBLIC MultiSelection
31
{
32
private:
33
    std::vector< Range >
34
                    aSels;      // array of SV-selections
35
    Range           aTotRange;  // total range of indexes
36
    std::size_t     nCurSubSel; // index in aSels of current selected index
37
    sal_Int32       nCurIndex;  // current selected entry
38
    sal_Int32       nSelCount;  // number of selected indexes
39
    bool            bCurValid;  // are nCurIndex and nCurSubSel valid
40
41
    TOOLS_DLLPRIVATE void           ImplClear();
42
    TOOLS_DLLPRIVATE std::size_t    ImplFindSubSelection( sal_Int32 nIndex ) const;
43
    TOOLS_DLLPRIVATE void           ImplMergeSubSelections( sal_Int32 nPos1, std::size_t nPos2 );
44
45
public:
46
                    MultiSelection();
47
                    MultiSelection( const MultiSelection& rOrig );
48
                    MultiSelection( const Range& rRange );
49
                    ~MultiSelection();
50
51
    MultiSelection& operator= ( const MultiSelection& rOrig );
52
53
    void            SelectAll( bool bSelect = true );
54
    bool            Select( sal_Int32 nIndex, bool bSelect = true );
55
    void            Select( const Range& rIndexRange, bool bSelect = true );
56
    bool            IsSelected( sal_Int32 nIndex ) const;
57
    bool            IsAllSelected() const
58
0
                        { return nSelCount == aTotRange.Len(); }
59
0
    sal_Int32       GetSelectCount() const { return nSelCount; }
60
61
    void            SetTotalRange( const Range& rTotRange );
62
    void            Insert( sal_Int32 nIndex, sal_Int32 nCount = 1 );
63
    void            Remove( sal_Int32 nIndex );
64
    void            Reset();
65
66
0
    const Range&    GetTotalRange() const { return aTotRange; }
67
    sal_Int32       FirstSelected();
68
    sal_Int32       LastSelected();
69
    sal_Int32       NextSelected();
70
71
436k
    sal_Int32       GetRangeCount() const { return aSels.size(); }
72
142
    const Range&    GetRange( sal_Int32 nRange ) const { return aSels[nRange]; }
73
};
74
75
class SAL_WARN_UNUSED TOOLS_DLLPUBLIC StringRangeEnumerator
76
{
77
    struct Range
78
    {
79
        sal_Int32   nFirst;
80
        sal_Int32   nLast;
81
82
18.9k
        Range( sal_Int32 i_nFirst, sal_Int32 i_nLast ) : nFirst( i_nFirst ), nLast( i_nLast ) {}
83
    };
84
    std::vector< StringRangeEnumerator::Range >            maSequence;
85
    sal_Int32                                              mnCount;
86
    sal_Int32                                              mnMin;
87
    sal_Int32                                              mnMax;
88
    sal_Int32                                              mnOffset;
89
    bool                                                   mbValidInput;
90
91
    bool setRange( std::u16string_view i_rNewRange );
92
    bool insertRange( sal_Int32 nFirst, sal_Int32 nLast, bool bSequence );
93
    void insertJoinedRanges( const std::vector< sal_Int32 >& rNumbers );
94
    bool checkValue( sal_Int32, const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
95
public:
96
    class TOOLS_DLLPUBLIC Iterator
97
    {
98
        const StringRangeEnumerator*      pEnumerator;
99
        const o3tl::sorted_vector< sal_Int32 >* pPossibleValues;
100
        sal_Int32                         nRangeIndex;
101
        sal_Int32                         nCurrent;
102
103
        friend class StringRangeEnumerator;
104
        Iterator( const StringRangeEnumerator* i_pEnum,
105
                  const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues,
106
                  sal_Int32 i_nRange,
107
                  sal_Int32 i_nCurrent )
108
89.1k
        : pEnumerator( i_pEnum ), pPossibleValues( i_pPossibleValues )
109
89.1k
        , nRangeIndex( i_nRange ), nCurrent( i_nCurrent ) {}
110
111
    public:
112
        Iterator& operator++();
113
279k
        sal_Int32 operator*() const { return nCurrent;}
114
        bool operator==(const Iterator&) const;
115
        bool operator!=(const Iterator& i_rComp) const
116
219k
        { return ! (*this == i_rComp); }
117
    };
118
119
    friend class StringRangeEnumerator::Iterator;
120
121
    StringRangeEnumerator( std::u16string_view i_rInput,
122
                           sal_Int32 i_nMinNumber,
123
                           sal_Int32 i_nMaxNumber,
124
                           sal_Int32 i_nLogicalOffset = -1
125
                           );
126
127
6.44k
    sal_Int32 size() const { return mnCount; }
128
    Iterator begin( const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
129
    Iterator end( const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
130
131
    bool hasValue( sal_Int32 nValue, const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues = nullptr ) const;
132
133
    /**
134
    i_rPageRange:     the string to be changed into a sequence of numbers
135
                      valid format example "5-3,9,9,7-8" ; instead of ',' ';' or ' ' are allowed as well
136
    o_rPageVector:    the output sequence of numbers
137
    i_nLogicalOffset: an offset to be applied to each number in the string before inserting it in the resulting sequence
138
                      example: a user enters page numbers from 1 to n (since that is logical)
139
                               of course usable page numbers in code would start from 0 and end at n-1
140
                               so the logical offset would be -1
141
    i_nMinNumber:     the minimum allowed number
142
    i_nMaxNumber:     the maximum allowed number
143
144
    @returns: true if the input string was valid, o_rPageVector will contain the resulting sequence
145
              false if the input string was invalid, o_rPageVector will contain
146
                    the sequence that parser is able to extract
147
148
    behavior:
149
    - only non-negative sequence numbers are allowed
150
    - only non-negative values in the input string are allowed
151
    - the string "-3" means the sequence i_nMinNumber to 3
152
    - the string "3-" means the sequence 3 to i_nMaxNumber
153
    - the string "-" means the sequence i_nMinNumber to i_nMaxNumber
154
    - single number that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be ignored
155
    - range that doesn't fit in [i_nMinNumber,i_nMaxNumber] will be adjusted
156
    */
157
    static bool getRangesFromString( std::u16string_view i_rPageRange,
158
                                     std::vector< sal_Int32 >& o_rPageVector,
159
                                     sal_Int32 i_nMinNumber,
160
                                     sal_Int32 i_nMaxNumber,
161
                                     sal_Int32 i_nLogicalOffset = -1,
162
                                     o3tl::sorted_vector< sal_Int32 > const * i_pPossibleValues = nullptr
163
                                    );
164
};
165
166
#endif
167
168
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */