/src/libreoffice/tools/source/memtools/multisel.cxx
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 | | |
20 | | #include <sal/config.h> |
21 | | |
22 | | #include <cstddef> |
23 | | |
24 | | #include <o3tl/string_view.hxx> |
25 | | #include <tools/debug.hxx> |
26 | | #include <tools/multisel.hxx> |
27 | | |
28 | | #include <rtl/ustrbuf.hxx> |
29 | | |
30 | | void MultiSelection::ImplClear() |
31 | 14.4k | { |
32 | | // no selected indexes |
33 | 14.4k | nSelCount = 0; |
34 | 14.4k | aSels.clear(); |
35 | 14.4k | } |
36 | | |
37 | | std::size_t MultiSelection::ImplFindSubSelection( sal_Int32 nIndex ) const |
38 | 6.31k | { |
39 | | // iterate through the sub selections |
40 | 6.31k | std::size_t n = 0; |
41 | 6.31k | for ( ; |
42 | 11.6k | n < aSels.size() && nIndex > aSels[ n ].Max(); |
43 | 6.31k | ++n ) {} /* empty loop */ |
44 | 6.31k | return n; |
45 | 6.31k | } |
46 | | |
47 | | void MultiSelection::ImplMergeSubSelections( sal_Int32 nPos1, std::size_t nPos2 ) |
48 | 0 | { |
49 | | // didn't a sub selection at nPos2 exist? |
50 | 0 | if ( nPos2 >= aSels.size() ) |
51 | 0 | return; |
52 | | |
53 | | // did the sub selections touch each other? |
54 | 0 | if ( (aSels[ nPos1 ].Max() + 1) == aSels[ nPos2 ].Min() ) |
55 | 0 | { |
56 | | // merge them |
57 | 0 | aSels[ nPos1 ].Max() = aSels[ nPos2 ].Max(); |
58 | 0 | aSels.erase( aSels.begin() + nPos2 ); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | MultiSelection::MultiSelection(): |
63 | 0 | aTotRange( 0, -1 ), |
64 | 0 | nCurSubSel(0), |
65 | 0 | nCurIndex(0), |
66 | 0 | nSelCount(0), |
67 | 0 | bCurValid(false) |
68 | 0 | { |
69 | 0 | } |
70 | | |
71 | | void MultiSelection::Reset() |
72 | 0 | { |
73 | 0 | aTotRange = Range(0, -1); |
74 | 0 | bCurValid = false; |
75 | | // clear the old sub selections |
76 | 0 | ImplClear(); |
77 | 0 | } |
78 | | |
79 | | MultiSelection::MultiSelection( const MultiSelection& rOrig ) : |
80 | 0 | aTotRange(rOrig.aTotRange), |
81 | 0 | nSelCount(rOrig.nSelCount), |
82 | 0 | bCurValid(rOrig.bCurValid) |
83 | 0 | { |
84 | 0 | if ( bCurValid ) |
85 | 0 | { |
86 | 0 | nCurSubSel = rOrig.nCurSubSel; |
87 | 0 | nCurIndex = rOrig.nCurIndex; |
88 | 0 | } |
89 | 0 | else |
90 | 0 | { |
91 | 0 | nCurSubSel = 0; |
92 | 0 | nCurIndex = 0; |
93 | 0 | } |
94 | | |
95 | | // copy the sub selections |
96 | 0 | aSels.insert( aSels.end(), rOrig.aSels.begin(), rOrig.aSels.end() ); |
97 | 0 | } |
98 | | |
99 | | MultiSelection::MultiSelection( const Range& rRange ): |
100 | 311k | aTotRange(rRange), |
101 | 311k | nCurSubSel(0), |
102 | 311k | nCurIndex(0), |
103 | 311k | nSelCount(0), |
104 | 311k | bCurValid(false) |
105 | 311k | { |
106 | 311k | } |
107 | | |
108 | | MultiSelection::~MultiSelection() |
109 | 311k | { |
110 | 311k | } |
111 | | |
112 | | MultiSelection& MultiSelection::operator= ( const MultiSelection& rOrig ) |
113 | 0 | { |
114 | 0 | aTotRange = rOrig.aTotRange; |
115 | 0 | bCurValid = rOrig.bCurValid; |
116 | 0 | if ( bCurValid ) |
117 | 0 | { |
118 | 0 | nCurSubSel = rOrig.nCurSubSel; |
119 | 0 | nCurIndex = rOrig.nCurIndex; |
120 | 0 | } |
121 | | |
122 | | // clear the old and copy the sub selections |
123 | 0 | ImplClear(); |
124 | 0 | aSels.insert( aSels.end(), rOrig.aSels.begin(), rOrig.aSels.end() ); |
125 | 0 | nSelCount = rOrig.nSelCount; |
126 | |
|
127 | 0 | return *this; |
128 | 0 | } |
129 | | |
130 | | void MultiSelection::SelectAll( bool bSelect ) |
131 | 759 | { |
132 | 759 | ImplClear(); |
133 | 759 | if ( bSelect ) |
134 | 759 | { |
135 | 759 | aSels.push_back( aTotRange ); |
136 | 759 | nSelCount = aTotRange.Len(); |
137 | 759 | } |
138 | 759 | } |
139 | | |
140 | | bool MultiSelection::Select( sal_Int32 nIndex, bool bSelect ) |
141 | 5.63k | { |
142 | 5.63k | DBG_ASSERT( aTotRange.Contains(nIndex), "selected index out of range" ); |
143 | | |
144 | | // out of range? |
145 | 5.63k | if ( !aTotRange.Contains(nIndex) ) |
146 | 0 | return false; |
147 | | |
148 | | // find the virtual target position |
149 | 5.63k | std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); |
150 | | |
151 | 5.63k | if ( bSelect ) |
152 | 5.63k | { |
153 | | // is it included in the found sub selection? |
154 | 5.63k | if ( nSubSelPos < aSels.size() && aSels[ nSubSelPos ].Contains( nIndex ) ) |
155 | | // already selected, nothing to do |
156 | 5.63k | return false; |
157 | | |
158 | | // it will become selected |
159 | 0 | ++nSelCount; |
160 | | |
161 | | // is it at the end of the previous sub selection |
162 | 0 | if ( nSubSelPos > 0 && |
163 | 0 | aSels[ nSubSelPos-1 ].Max() == (nIndex-1) ) |
164 | 0 | { |
165 | | // expand the previous sub selection |
166 | 0 | aSels[ nSubSelPos-1 ].Max() = nIndex; |
167 | | |
168 | | // try to merge the previous sub selection |
169 | 0 | ImplMergeSubSelections( nSubSelPos-1, nSubSelPos ); |
170 | 0 | } |
171 | | // is it at the beginning of the found sub selection |
172 | 0 | else if ( nSubSelPos < aSels.size() |
173 | 0 | && aSels[ nSubSelPos ].Min() == (nIndex+1) |
174 | 0 | ) |
175 | | // expand the found sub selection |
176 | 0 | aSels[ nSubSelPos ].Min() = nIndex; |
177 | 0 | else |
178 | 0 | { |
179 | | // create a new sub selection |
180 | 0 | if ( nSubSelPos < aSels.size() ) { |
181 | 0 | aSels.insert( aSels.begin() + nSubSelPos, Range( nIndex, nIndex ) ); |
182 | 0 | } else { |
183 | 0 | aSels.push_back( Range( nIndex, nIndex ) ); |
184 | 0 | } |
185 | 0 | if ( bCurValid && nCurSubSel >= nSubSelPos ) |
186 | 0 | ++nCurSubSel; |
187 | 0 | } |
188 | 0 | } |
189 | 0 | else |
190 | 0 | { |
191 | | // is it excluded from the found sub selection? |
192 | 0 | if ( nSubSelPos >= aSels.size() |
193 | 0 | || !aSels[ nSubSelPos ].Contains( nIndex ) |
194 | 0 | ) { |
195 | | // not selected, nothing to do |
196 | 0 | return false; |
197 | 0 | } |
198 | | |
199 | | // it will become deselected |
200 | 0 | --nSelCount; |
201 | | |
202 | | // is it the only index in the found sub selection? |
203 | 0 | if ( aSels[ nSubSelPos ].Len() == 1 ) |
204 | 0 | { |
205 | | // remove the complete sub selection |
206 | 0 | aSels.erase( aSels.begin() + nSubSelPos ); |
207 | 0 | return true; |
208 | 0 | } |
209 | | |
210 | | // is it at the beginning of the found sub selection? |
211 | 0 | if ( aSels[ nSubSelPos ].Min() == nIndex ) |
212 | 0 | ++aSels[ nSubSelPos ].Min(); |
213 | | // is it at the end of the found sub selection? |
214 | 0 | else if ( aSels[ nSubSelPos ].Max() == nIndex ) |
215 | 0 | --aSels[ nSubSelPos ].Max(); |
216 | | // it is in the middle of the found sub selection? |
217 | 0 | else |
218 | 0 | { |
219 | | // split the sub selection |
220 | | // we know nSubSelPos < aSels.size() since it's been tested some lines before |
221 | 0 | aSels.insert( aSels.begin() + nSubSelPos, Range( aSels[ nSubSelPos ].Min(), nIndex-1 ) ); |
222 | 0 | aSels[ nSubSelPos+1 ].Min() = nIndex + 1; |
223 | 0 | } |
224 | 0 | } |
225 | | |
226 | 0 | return true; |
227 | 5.63k | } |
228 | | |
229 | | void MultiSelection::Select( const Range& rIndexRange, bool bSelect ) |
230 | 19.1k | { |
231 | 19.1k | sal_Int32 nOld; |
232 | | |
233 | 19.1k | sal_Int32 nTmpMin = rIndexRange.Min(); |
234 | 19.1k | sal_Int32 nTmpMax = rIndexRange.Max(); |
235 | 19.1k | sal_Int32 nCurMin = FirstSelected(); |
236 | 19.1k | sal_Int32 nCurMax = LastSelected(); |
237 | 19.1k | DBG_ASSERT(aTotRange.Contains(nTmpMax), "selected index out of range" ); |
238 | 19.1k | DBG_ASSERT(aTotRange.Contains(nTmpMin), "selected index out of range" ); |
239 | | |
240 | | // replace whole selection? |
241 | 19.1k | if( aSels.empty() || (nTmpMin <= nCurMin && nTmpMax >= nCurMax ) ) |
242 | 13.6k | { |
243 | 13.6k | ImplClear(); |
244 | 13.6k | if ( bSelect ) |
245 | 4.79k | { |
246 | 4.79k | aSels.push_back( rIndexRange ); |
247 | 4.79k | nSelCount = rIndexRange.Len(); |
248 | 4.79k | } |
249 | 13.6k | return; |
250 | 13.6k | } |
251 | | // expand on left side? |
252 | 5.53k | if( nTmpMax < nCurMin ) |
253 | 0 | { |
254 | 0 | if( bSelect ) |
255 | 0 | { |
256 | | // extend first range? |
257 | 0 | if( nCurMin > (nTmpMax+1) ) |
258 | 0 | { |
259 | 0 | aSels.insert( aSels.begin(), rIndexRange ); |
260 | 0 | nSelCount += rIndexRange.Len(); |
261 | 0 | } |
262 | 0 | else |
263 | 0 | { |
264 | 0 | auto & rRange = aSels.front(); |
265 | 0 | nOld = rRange.Min(); |
266 | 0 | rRange.Min() = nTmpMin; |
267 | 0 | nSelCount += ( nOld - nTmpMin ); |
268 | 0 | } |
269 | 0 | bCurValid = false; |
270 | 0 | } |
271 | 0 | return; |
272 | 0 | } |
273 | | // expand on right side? |
274 | 5.53k | else if( nTmpMin > nCurMax ) |
275 | 5.44k | { |
276 | 5.44k | if( bSelect ) |
277 | 2.80k | { |
278 | | // extend last range? |
279 | 2.80k | if( nTmpMin > (nCurMax+1) ) |
280 | 797 | { |
281 | 797 | aSels.push_back( rIndexRange ); |
282 | 797 | nSelCount += rIndexRange.Len(); |
283 | 797 | } |
284 | 2.00k | else |
285 | 2.00k | { |
286 | 2.00k | auto & rRange = aSels.back(); |
287 | 2.00k | nOld = rRange.Max(); |
288 | 2.00k | rRange.Max() = nTmpMax; |
289 | 2.00k | nSelCount += ( nTmpMax - nOld ); |
290 | 2.00k | } |
291 | 2.80k | bCurValid = false; |
292 | 2.80k | } |
293 | 5.44k | return; |
294 | 5.44k | } |
295 | | |
296 | | // TODO here is potential for optimization |
297 | 5.72k | while( nTmpMin <= nTmpMax ) |
298 | 5.63k | { |
299 | 5.63k | Select( nTmpMin, bSelect ); |
300 | 5.63k | nTmpMin++; |
301 | 5.63k | } |
302 | 88 | } |
303 | | |
304 | | bool MultiSelection::IsSelected( sal_Int32 nIndex ) const |
305 | 680 | { |
306 | | // find the virtual target position |
307 | 680 | std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); |
308 | | |
309 | 680 | return nSubSelPos < aSels.size() && aSels[ nSubSelPos ].Contains(nIndex); |
310 | 680 | } |
311 | | |
312 | | void MultiSelection::Insert( sal_Int32 nIndex, sal_Int32 nCount ) |
313 | 0 | { |
314 | | // find the virtual target position |
315 | 0 | std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); |
316 | | |
317 | | // did we need to shift the sub selections? |
318 | 0 | if ( nSubSelPos < aSels.size() ) |
319 | 0 | { // did we insert an unselected into an existing sub selection? |
320 | 0 | if ( aSels[ nSubSelPos ].Min() != nIndex |
321 | 0 | && aSels[ nSubSelPos ].Contains(nIndex) |
322 | 0 | ) { // split the sub selection |
323 | 0 | aSels.insert( aSels.begin() + nSubSelPos, Range( aSels[ nSubSelPos ].Min(), nIndex-1 ) ); |
324 | 0 | ++nSubSelPos; |
325 | 0 | aSels[ nSubSelPos ].Min() = nIndex; |
326 | 0 | } |
327 | | |
328 | | // shift the sub selections behind the inserting position |
329 | 0 | for ( std::size_t nPos = nSubSelPos; nPos < aSels.size(); ++nPos ) |
330 | 0 | { |
331 | 0 | aSels[ nPos ].Min() += nCount; |
332 | 0 | aSels[ nPos ].Max() += nCount; |
333 | 0 | } |
334 | 0 | } |
335 | |
|
336 | 0 | bCurValid = false; |
337 | 0 | aTotRange.Max() += nCount; |
338 | 0 | } |
339 | | |
340 | | void MultiSelection::Remove( sal_Int32 nIndex ) |
341 | 0 | { |
342 | | // find the virtual target position |
343 | 0 | std::size_t nSubSelPos = ImplFindSubSelection( nIndex ); |
344 | | |
345 | | // did we remove from an existing sub selection? |
346 | 0 | if ( nSubSelPos < aSels.size() |
347 | 0 | && aSels[ nSubSelPos ].Contains(nIndex) |
348 | 0 | ) { |
349 | | // does this sub selection only contain the index to be deleted |
350 | 0 | if ( aSels[ nSubSelPos ].Len() == 1 ) { |
351 | | // completely remove the sub selection |
352 | 0 | aSels.erase( aSels.begin() + nSubSelPos ); |
353 | 0 | } else { |
354 | | // shorten this sub selection |
355 | 0 | --( aSels[ nSubSelPos++ ].Max() ); |
356 | 0 | } |
357 | | |
358 | | // adjust the selected counter |
359 | 0 | --nSelCount; |
360 | 0 | } |
361 | | |
362 | | // shift the sub selections behind the removed index |
363 | 0 | for ( std::size_t nPos = nSubSelPos; nPos < aSels.size(); ++nPos ) |
364 | 0 | { |
365 | 0 | --( aSels[ nPos ].Min() ); |
366 | 0 | --( aSels[ nPos ].Max() ); |
367 | 0 | } |
368 | |
|
369 | 0 | bCurValid = false; |
370 | 0 | aTotRange.Max() -= 1; |
371 | 0 | } |
372 | | |
373 | | sal_Int32 MultiSelection::FirstSelected() |
374 | 19.1k | { |
375 | 19.1k | nCurSubSel = 0; |
376 | | |
377 | 19.1k | bCurValid = !aSels.empty(); |
378 | 19.1k | if ( !bCurValid ) |
379 | 13.2k | return SFX_ENDOFSELECTION; |
380 | | |
381 | 5.91k | nCurIndex = aSels[ 0 ].Min(); |
382 | 5.91k | return nCurIndex; |
383 | 19.1k | } |
384 | | |
385 | | sal_Int32 MultiSelection::LastSelected() |
386 | 19.1k | { |
387 | 19.1k | bCurValid = !aSels.empty(); |
388 | | |
389 | 19.1k | if ( !bCurValid ) |
390 | 13.2k | return SFX_ENDOFSELECTION; |
391 | | |
392 | 5.91k | nCurSubSel = aSels.size() - 1; |
393 | 5.91k | nCurIndex = aSels[ nCurSubSel ].Max(); |
394 | 5.91k | return nCurIndex; |
395 | 19.1k | } |
396 | | |
397 | | sal_Int32 MultiSelection::NextSelected() |
398 | 0 | { |
399 | 0 | if ( !bCurValid ) |
400 | 0 | return SFX_ENDOFSELECTION; |
401 | | |
402 | | // is the next index in the current sub selection too? |
403 | 0 | if ( nCurIndex < aSels[ nCurSubSel ].Max() ) |
404 | 0 | return ++nCurIndex; |
405 | | |
406 | | // are there further sub selections? |
407 | 0 | if ( ++nCurSubSel >= aSels.size() ) |
408 | | // we are at the end! |
409 | 0 | return SFX_ENDOFSELECTION; |
410 | | |
411 | 0 | nCurIndex = aSels[ nCurSubSel ].Min(); |
412 | 0 | return nCurIndex; |
413 | 0 | } |
414 | | |
415 | | void MultiSelection::SetTotalRange( const Range& rTotRange ) |
416 | 0 | { |
417 | 0 | aTotRange = rTotRange; |
418 | | |
419 | | // adjust lower boundary |
420 | 0 | Range* pRange = aSels.empty() ? nullptr : &aSels.front(); |
421 | 0 | while( pRange ) |
422 | 0 | { |
423 | 0 | if( pRange->Max() < aTotRange.Min() ) |
424 | 0 | { |
425 | 0 | aSels.erase( aSels.begin() ); |
426 | 0 | } |
427 | 0 | else if( pRange->Min() < aTotRange.Min() ) |
428 | 0 | { |
429 | 0 | pRange->Min() = aTotRange.Min(); |
430 | 0 | break; |
431 | 0 | } |
432 | 0 | else |
433 | 0 | break; |
434 | | |
435 | 0 | pRange = aSels.empty() ? nullptr : &aSels.front(); |
436 | 0 | } |
437 | | |
438 | | // adjust upper boundary |
439 | 0 | sal_Int32 nCount = aSels.size(); |
440 | 0 | while( nCount ) |
441 | 0 | { |
442 | 0 | pRange = &aSels[ nCount - 1 ]; |
443 | 0 | if( pRange->Min() > aTotRange.Max() ) |
444 | 0 | { |
445 | 0 | aSels.pop_back(); |
446 | 0 | } |
447 | 0 | else if( pRange->Max() > aTotRange.Max() ) |
448 | 0 | { |
449 | 0 | pRange->Max() = aTotRange.Max(); |
450 | 0 | break; |
451 | 0 | } |
452 | 0 | else |
453 | 0 | break; |
454 | | |
455 | 0 | nCount = aSels.size(); |
456 | 0 | } |
457 | | |
458 | | // re-calculate selection count |
459 | 0 | nSelCount = 0; |
460 | 0 | for (Range const & rSel : aSels) |
461 | 0 | nSelCount += rSel.Len(); |
462 | |
|
463 | 0 | bCurValid = false; |
464 | 0 | nCurIndex = 0; |
465 | 0 | } |
466 | | |
467 | | // StringRangeEnumerator |
468 | | |
469 | | StringRangeEnumerator::StringRangeEnumerator( std::u16string_view i_rInput, |
470 | | sal_Int32 i_nMinNumber, |
471 | | sal_Int32 i_nMaxNumber, |
472 | | sal_Int32 i_nLogicalOffset |
473 | | ) |
474 | 37.8k | : mnCount( 0 ) |
475 | 37.8k | , mnMin( i_nMinNumber ) |
476 | 37.8k | , mnMax( i_nMaxNumber ) |
477 | 37.8k | , mnOffset( i_nLogicalOffset ) |
478 | 37.8k | , mbValidInput( false ) |
479 | 37.8k | { |
480 | | // Parse string only if boundaries are valid. |
481 | 37.8k | if( mnMin >= 0 && mnMax >= 0 && mnMin <= mnMax ) |
482 | 37.8k | mbValidInput = setRange( i_rInput ); |
483 | 37.8k | } |
484 | | |
485 | | bool StringRangeEnumerator::checkValue( sal_Int32 i_nValue, const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues ) const |
486 | 330k | { |
487 | 330k | if( i_nValue < 0 || i_nValue < mnMin || i_nValue > mnMax ) |
488 | 0 | return false; |
489 | 330k | if( i_pPossibleValues && i_pPossibleValues->find( i_nValue ) == i_pPossibleValues->end() ) |
490 | 534 | return false; |
491 | 329k | return true; |
492 | 330k | } |
493 | | |
494 | | bool StringRangeEnumerator::insertRange( sal_Int32 i_nFirst, sal_Int32 i_nLast, bool bSequence ) |
495 | 37.8k | { |
496 | 37.8k | bool bSuccess = true; |
497 | 37.8k | if( bSequence ) |
498 | 30.0k | { |
499 | | // Check if the range is completely outside of possible pages range |
500 | 30.0k | if ((i_nFirst < mnMin && i_nLast < mnMin) || |
501 | 30.0k | (i_nFirst > mnMax && i_nLast > mnMax)) |
502 | 0 | return false; |
503 | 30.0k | if( i_nFirst < mnMin ) |
504 | 0 | i_nFirst = mnMin; |
505 | 30.0k | if( i_nFirst > mnMax ) |
506 | 0 | i_nFirst = mnMax; |
507 | 30.0k | if( i_nLast < mnMin ) |
508 | 0 | i_nLast = mnMin; |
509 | 30.0k | if( i_nLast > mnMax ) |
510 | 25.9k | i_nLast = mnMax; |
511 | 30.0k | if( checkValue( i_nFirst ) && checkValue( i_nLast ) ) |
512 | 30.0k | { |
513 | 30.0k | maSequence.push_back( Range( i_nFirst, i_nLast ) ); |
514 | 30.0k | sal_Int32 nNumber = i_nLast - i_nFirst; |
515 | 30.0k | nNumber = nNumber < 0 ? -nNumber : nNumber; |
516 | 30.0k | mnCount += nNumber + 1; |
517 | 30.0k | } |
518 | 0 | else |
519 | 0 | bSuccess = false; |
520 | 30.0k | } |
521 | 7.77k | else |
522 | 7.77k | { |
523 | 7.77k | if( checkValue( i_nFirst ) ) |
524 | 7.77k | { |
525 | 7.77k | maSequence.push_back( Range( i_nFirst, i_nFirst ) ); |
526 | 7.77k | mnCount++; |
527 | 7.77k | } |
528 | 0 | else if( checkValue( i_nLast ) ) |
529 | 0 | { |
530 | 0 | maSequence.push_back( Range( i_nLast, i_nLast ) ); |
531 | 0 | mnCount++; |
532 | 0 | } |
533 | 0 | else |
534 | 0 | bSuccess = false; |
535 | 7.77k | } |
536 | | |
537 | 37.8k | return bSuccess; |
538 | 37.8k | } |
539 | | |
540 | | void StringRangeEnumerator::insertJoinedRanges( |
541 | | const std::vector< sal_Int32 >& rNumbers ) |
542 | 37.8k | { |
543 | 37.8k | size_t nCount = rNumbers.size(); |
544 | 37.8k | if( nCount == 0 ) |
545 | 0 | return; |
546 | | |
547 | 37.8k | if( nCount == 1 ) |
548 | 0 | { |
549 | 0 | insertRange( rNumbers[0], -1, false ); |
550 | 0 | return; |
551 | 0 | } |
552 | | |
553 | 75.6k | for( size_t i = 0; i < nCount - 1; i++ ) |
554 | 37.8k | { |
555 | 37.8k | sal_Int32 nFirst = rNumbers[i]; |
556 | 37.8k | sal_Int32 nLast = rNumbers[i + 1]; |
557 | 37.8k | if( i > 0 ) |
558 | 0 | { |
559 | 0 | if ( nFirst > nLast ) nFirst--; |
560 | 0 | else if( nFirst < nLast ) nFirst++; |
561 | 0 | } |
562 | | |
563 | 37.8k | insertRange( nFirst, nLast, nFirst != nLast ); |
564 | 37.8k | } |
565 | 37.8k | } |
566 | | |
567 | | bool StringRangeEnumerator::setRange( std::u16string_view aNewRange ) |
568 | 37.8k | { |
569 | 37.8k | mnCount = 0; |
570 | 37.8k | maSequence.clear(); |
571 | | |
572 | 37.8k | auto pInput = aNewRange.begin(); |
573 | 37.8k | auto pInputEnd = aNewRange.end(); |
574 | 37.8k | OUStringBuffer aNumberBuf( 16 ); |
575 | 37.8k | std::vector< sal_Int32 > aNumbers; |
576 | 37.8k | bool bSequence = false; |
577 | 75.6k | while( pInput != pInputEnd ) |
578 | 75.6k | { |
579 | 205k | while( pInput != pInputEnd && *pInput >= '0' && *pInput <= '9' ) |
580 | 130k | aNumberBuf.append( *pInput++ ); |
581 | 75.6k | if( !aNumberBuf.isEmpty() ) |
582 | 75.6k | { |
583 | 75.6k | sal_Int32 nNumber = o3tl::toInt32(aNumberBuf) + mnOffset; |
584 | 75.6k | aNumberBuf.setLength(0); |
585 | 75.6k | aNumbers.push_back( nNumber ); |
586 | 75.6k | bSequence = false; |
587 | 75.6k | } |
588 | 75.6k | if (pInput == pInputEnd) |
589 | 37.8k | break; |
590 | 37.8k | if( *pInput == '-' ) |
591 | 37.8k | { |
592 | 37.8k | bSequence = true; |
593 | 37.8k | if( aNumbers.empty() ) |
594 | 0 | { |
595 | | // push out-of-range small value, to exclude ranges totally outside of possible range |
596 | 0 | aNumbers.push_back( mnMin-1 ); |
597 | 0 | } |
598 | 37.8k | } |
599 | 0 | else if( *pInput == ',' || *pInput == ';' ) |
600 | 0 | { |
601 | 0 | if( bSequence && !aNumbers.empty() ) |
602 | 0 | { |
603 | | // push out-of-range large value, to exclude ranges totally outside of possible range |
604 | 0 | aNumbers.push_back( mnMax+1 ); |
605 | 0 | } |
606 | 0 | insertJoinedRanges( aNumbers ); |
607 | |
|
608 | 0 | aNumbers.clear(); |
609 | 0 | bSequence = false; |
610 | 0 | } |
611 | 0 | else if( *pInput != ' ' ) |
612 | 0 | return false; // parse error |
613 | | |
614 | 37.8k | pInput++; |
615 | 37.8k | } |
616 | | // insert last entries |
617 | 37.8k | if( bSequence && !aNumbers.empty() ) |
618 | 0 | { |
619 | | // push out-of-range large value, to exclude ranges totally outside of possible range |
620 | 0 | aNumbers.push_back( mnMax+1 ); |
621 | 0 | } |
622 | 37.8k | insertJoinedRanges( aNumbers ); |
623 | | |
624 | 37.8k | return true; |
625 | 37.8k | } |
626 | | |
627 | | bool StringRangeEnumerator::hasValue( sal_Int32 i_nValue, const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues ) const |
628 | 5.50k | { |
629 | 5.50k | if( i_pPossibleValues && i_pPossibleValues->find( i_nValue ) == i_pPossibleValues->end() ) |
630 | 0 | return false; |
631 | 5.50k | size_t n = maSequence.size(); |
632 | 5.54k | for( size_t i= 0; i < n; ++i ) |
633 | 5.50k | { |
634 | 5.50k | const StringRangeEnumerator::Range rRange( maSequence[i] ); |
635 | 5.50k | if( rRange.nFirst < rRange.nLast ) |
636 | 3.05k | { |
637 | 3.05k | if( i_nValue >= rRange.nFirst && i_nValue <= rRange.nLast ) |
638 | 3.01k | return true; |
639 | 3.05k | } |
640 | 2.44k | else |
641 | 2.44k | { |
642 | 2.44k | if( i_nValue >= rRange.nLast && i_nValue <= rRange.nFirst ) |
643 | 2.44k | return true; |
644 | 2.44k | } |
645 | 5.50k | } |
646 | 42 | return false; |
647 | 5.50k | } |
648 | | |
649 | | StringRangeEnumerator::Iterator& StringRangeEnumerator::Iterator::operator++() |
650 | 262k | { |
651 | 262k | if( nRangeIndex >= 0 && nCurrent >= 0 && pEnumerator ) |
652 | 262k | { |
653 | 262k | const StringRangeEnumerator::Range& rRange( pEnumerator->maSequence[nRangeIndex] ); |
654 | 262k | bool bRangeChange = false; |
655 | 262k | if( rRange.nLast < rRange.nFirst ) |
656 | 0 | { |
657 | | // backward range |
658 | 0 | if( nCurrent > rRange.nLast ) |
659 | 0 | nCurrent--; |
660 | 0 | else |
661 | 0 | bRangeChange = true; |
662 | 0 | } |
663 | 262k | else |
664 | 262k | { |
665 | | // forward range |
666 | 262k | if( nCurrent < rRange.nLast ) |
667 | 236k | nCurrent++; |
668 | 25.5k | else |
669 | 25.5k | bRangeChange = true; |
670 | 262k | } |
671 | 262k | if( bRangeChange ) |
672 | 25.5k | { |
673 | 25.5k | nRangeIndex++; |
674 | 25.5k | if( size_t(nRangeIndex) == pEnumerator->maSequence.size() ) |
675 | 25.5k | { |
676 | | // reached the end |
677 | 25.5k | nRangeIndex = nCurrent = -1; |
678 | 25.5k | } |
679 | 0 | else |
680 | 0 | nCurrent = pEnumerator->maSequence[nRangeIndex].nFirst; |
681 | 25.5k | } |
682 | 262k | if( nRangeIndex != -1 && nCurrent != -1 ) |
683 | 236k | { |
684 | 236k | if( ! pEnumerator->checkValue( nCurrent, pPossibleValues ) ) |
685 | 481 | return ++(*this); |
686 | 236k | } |
687 | 262k | } |
688 | 261k | return *this; |
689 | 262k | } |
690 | | |
691 | | |
692 | | bool StringRangeEnumerator::Iterator::operator==( const Iterator& i_rCompare ) const |
693 | 361k | { |
694 | 361k | return i_rCompare.pEnumerator == pEnumerator && i_rCompare.nRangeIndex == nRangeIndex && i_rCompare.nCurrent == nCurrent; |
695 | 361k | } |
696 | | |
697 | | StringRangeEnumerator::Iterator StringRangeEnumerator::begin( const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues ) const |
698 | 25.5k | { |
699 | 25.5k | StringRangeEnumerator::Iterator it( this, |
700 | 25.5k | i_pPossibleValues, |
701 | 25.5k | maSequence.empty() ? -1 : 0, |
702 | 25.5k | maSequence.empty() ? -1 : maSequence[0].nFirst ); |
703 | 25.5k | if( ! checkValue(*it, i_pPossibleValues ) ) |
704 | 53 | ++it; |
705 | 25.5k | return it; |
706 | 25.5k | } |
707 | | |
708 | | StringRangeEnumerator::Iterator StringRangeEnumerator::end( const o3tl::sorted_vector< sal_Int32 >* i_pPossibleValues ) const |
709 | 102k | { |
710 | 102k | return StringRangeEnumerator::Iterator( this, i_pPossibleValues, -1, -1 ); |
711 | 102k | } |
712 | | |
713 | | bool StringRangeEnumerator::getRangesFromString( std::u16string_view i_rPageRange, |
714 | | std::vector< sal_Int32 >& o_rPageVector, |
715 | | sal_Int32 i_nMinNumber, |
716 | | sal_Int32 i_nMaxNumber, |
717 | | sal_Int32 i_nLogicalOffset, |
718 | | o3tl::sorted_vector< sal_Int32 > const * i_pPossibleValues |
719 | | ) |
720 | 8.87k | { |
721 | 8.87k | o_rPageVector.clear(); |
722 | | |
723 | 8.87k | StringRangeEnumerator aEnum( i_rPageRange, i_nMinNumber, i_nMaxNumber, i_nLogicalOffset ) ; |
724 | | |
725 | | //Even if the input range wasn't completely valid, return what ranges could |
726 | | //be extracted from the input. |
727 | 8.87k | o_rPageVector.reserve( static_cast< size_t >( aEnum.size() ) ); |
728 | 8.87k | for( StringRangeEnumerator::Iterator it = aEnum.begin( i_pPossibleValues ); |
729 | 86.0k | it != aEnum.end( i_pPossibleValues ); ++it ) |
730 | 77.1k | { |
731 | 77.1k | o_rPageVector.push_back( *it ); |
732 | 77.1k | } |
733 | | |
734 | 8.87k | return aEnum.mbValidInput; |
735 | 8.87k | } |
736 | | |
737 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |