qcollator.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qcollator.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2016 The Qt Company Ltd.-
4** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>-
5** Contact: https://www.qt.io/licensing/-
6**-
7** This file is part of the QtCore module of the Qt Toolkit.-
8**-
9** $QT_BEGIN_LICENSE:LGPL$-
10** Commercial License Usage-
11** Licensees holding valid commercial Qt licenses may use this file in-
12** accordance with the commercial license agreement provided with the-
13** Software or, alternatively, in accordance with the terms contained in-
14** a written agreement between you and The Qt Company. For licensing terms-
15** and conditions see https://www.qt.io/terms-conditions. For further-
16** information use the contact form at https://www.qt.io/contact-us.-
17**-
18** GNU Lesser General Public License Usage-
19** Alternatively, this file may be used under the terms of the GNU Lesser-
20** General Public License version 3 as published by the Free Software-
21** Foundation and appearing in the file LICENSE.LGPL3 included in the-
22** packaging of this file. Please review the following information to-
23** ensure the GNU Lesser General Public License version 3 requirements-
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.-
25**-
26** GNU General Public License Usage-
27** Alternatively, this file may be used under the terms of the GNU-
28** General Public License version 2.0 or (at your option) the GNU General-
29** Public license version 3 or any later version approved by the KDE Free-
30** Qt Foundation. The licenses are as published by the Free Software-
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3-
32** included in the packaging of this file. Please review the following-
33** information to ensure the GNU General Public License requirements will-
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and-
35** https://www.gnu.org/licenses/gpl-3.0.html.-
36**-
37** $QT_END_LICENSE$-
38**-
39****************************************************************************/-
40-
41#include "qcollator_p.h"-
42#include "qstringlist.h"-
43#include "qstring.h"-
44-
45#include "qdebug.h"-
46-
47QT_BEGIN_NAMESPACE-
48-
49-
50/*!-
51 \class QCollator-
52 \inmodule QtCore-
53 \brief The QCollator class compares strings according to a localized collation algorithm.-
54-
55 \since 5.2-
56-
57 \reentrant-
58 \ingroup i18n-
59 \ingroup string-processing-
60 \ingroup shared-
61-
62 QCollator is initialized with a QLocale and an optional collation strategy. It tries to-
63 initialize the collator with the specified values. The collator can then be used to compare-
64 and sort strings in a locale dependent fashion.-
65-
66 A QCollator object can be used together with template based sorting algorithms such as std::sort-
67 to sort a list of QStrings.-
68-
69 In addition to the locale and collation strategy, several optional flags can be set that influence-
70 the result of the collation.-
71*/-
72-
73/*!-
74 Constructs a QCollator from \a locale. If \a locale is not specified-
75 the system's default locale is used.-
76-
77 \sa setLocale()-
78 */-
79QCollator::QCollator(const QLocale &locale)-
80 : d(new QCollatorPrivate)-
81{-
82 d->locale = locale;-
83 d->init();-
84}
executed 193 times by 13 tests: end of block
Executed by:
  • tst_ModelTest
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QItemModel
  • tst_QListWidget
  • tst_QPrinter
  • tst_QTableWidget
  • tst_QTreeWidget
  • tst_QTreeWidgetItemIterator
  • tst_languageChange
193
85-
86/*!-
87 Creates a copy of \a other.-
88 */-
89QCollator::QCollator(const QCollator &other)-
90 : d(other.d)-
91{-
92 d->ref.ref();-
93}
executed 4777 times by 12 tests: end of block
Executed by:
  • tst_ModelTest
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QItemModel
  • tst_QListWidget
  • tst_QPrinter
  • tst_QTableWidget
  • tst_QTreeWidget
  • tst_QTreeWidgetItemIterator
  • tst_languageChange
4777
94-
95/*!-
96 Destroys the collator.-
97 */-
98QCollator::~QCollator()-
99{-
100 if (d && !d->ref.deref())
dDescription
TRUEevaluated 4970 times by 19 tests
Evaluated by:
  • tst_ModelTest
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QItemModel
  • tst_QListWidget
  • tst_QPrinter
  • tst_QTableWidget
  • tst_QTreeWidget
  • tst_QTreeWidgetItemIterator
  • tst_languageChange
  • tst_modeltest - unknown status
  • tst_qitemmodel - unknown status
  • tst_qlistwidget - unknown status
  • tst_qtablewidget - unknown status
  • tst_qtreewidget - unknown status
  • tst_qtreewidgetitemiterator - unknown status
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCollator
!d->ref.deref()Description
TRUEevaluated 193 times by 13 tests
Evaluated by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
  • tst_modeltest - unknown status
  • tst_qitemmodel - unknown status
  • tst_qlistwidget - unknown status
  • tst_qtablewidget - unknown status
  • tst_qtreewidget - unknown status
  • tst_qtreewidgetitemiterator - unknown status
FALSEevaluated 4777 times by 12 tests
Evaluated by:
  • tst_ModelTest
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QItemModel
  • tst_QListWidget
  • tst_QPrinter
  • tst_QTableWidget
  • tst_QTreeWidget
  • tst_QTreeWidgetItemIterator
  • tst_languageChange
1-4970
101 delete d;
executed 193 times by 13 tests: delete d;
Executed by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
  • tst_modeltest - unknown status
  • tst_qitemmodel - unknown status
  • tst_qlistwidget - unknown status
  • tst_qtablewidget - unknown status
  • tst_qtreewidget - unknown status
  • tst_qtreewidgetitemiterator - unknown status
193
102}
executed 4971 times by 19 tests: end of block
Executed by:
  • tst_ModelTest
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QItemModel
  • tst_QListWidget
  • tst_QPrinter
  • tst_QTableWidget
  • tst_QTreeWidget
  • tst_QTreeWidgetItemIterator
  • tst_languageChange
  • tst_modeltest - unknown status
  • tst_qitemmodel - unknown status
  • tst_qlistwidget - unknown status
  • tst_qtablewidget - unknown status
  • tst_qtreewidget - unknown status
  • tst_qtreewidgetitemiterator - unknown status
4971
103-
104/*!-
105 Assigns \a other to this collator.-
106 */-
107QCollator &QCollator::operator=(const QCollator &other)-
108{-
109 if (this != &other) {
this != &otherDescription
TRUEnever evaluated
FALSEnever evaluated
0
110 if (d && !d->ref.deref())
dDescription
TRUEnever evaluated
FALSEnever evaluated
!d->ref.deref()Description
TRUEnever evaluated
FALSEnever evaluated
0
111 delete d;
never executed: delete d;
0
112 d = other.d;-
113 if (d) d->ref.ref();
never executed: d->ref.ref();
dDescription
TRUEnever evaluated
FALSEnever evaluated
0
114 }
never executed: end of block
0
115 return *this;
never executed: return *this;
0
116}-
117-
118/*!-
119 \fn QCollator::QCollator(QCollator &&other)-
120-
121 Move constructor. Moves from \a other into this collator.-
122-
123 Note that a moved-from QCollator can only be destroyed or assigned-
124 to. The effect of calling other functions than the destructor or-
125 one of the assignment operators is undefined.-
126*/-
127-
128/*!-
129 \fn QCollator & QCollator::operator=(QCollator && other)-
130-
131 Move-assigns from \a other to this collator.-
132-
133 Note that a moved-from QCollator can only be destroyed or assigned-
134 to. The effect of calling other functions than the destructor or-
135 one of the assignment operators is undefined.-
136*/-
137-
138/*!-
139 \fn void QCollator::swap(QCollator &other)-
140-
141 Swaps this collator with \a other. This function is very fast and-
142 never fails.-
143*/-
144-
145/*!-
146 \internal-
147 */-
148void QCollator::detach()-
149{-
150 if (d->ref.load() != 1) {
d->ref.load() != 1Description
TRUEnever evaluated
FALSEevaluated 352 times by 7 tests
Evaluated by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
0-352
151 QCollatorPrivate *x = new QCollatorPrivate;-
152 x->ref.store(1);-
153 x->locale = d->locale;-
154 x->collator = 0;-
155 if (!d->ref.deref())
!d->ref.deref()Description
TRUEnever evaluated
FALSEnever evaluated
0
156 delete d;
never executed: delete d;
0
157 d = x;-
158 d->init();-
159 }
never executed: end of block
0
160}
executed 352 times by 7 tests: end of block
Executed by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
352
161-
162/*!-
163 Sets the locale of the collator to \a locale.-
164 */-
165void QCollator::setLocale(const QLocale &locale)-
166{-
167 if (locale == d->locale)
locale == d->localeDescription
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • tst_QCollator
0-3
168 return;
never executed: return;
0
169-
170 detach();-
171 d->locale = locale;-
172 d->dirty = true;-
173}
executed 3 times by 1 test: end of block
Executed by:
  • tst_QCollator
3
174-
175/*!-
176 Returns the locale of the collator.-
177 */-
178QLocale QCollator::locale() const-
179{-
180 return d->locale;
executed 5 times by 1 test: return d->locale;
Executed by:
  • tst_QCollator
5
181}-
182-
183/*!-
184 \fn void QCollator::setCaseSensitivity(Qt::CaseSensitivity sensitivity)-
185-
186 Sets the case \a sensitivity of the collator.-
187-
188 \sa caseSensitivity()-
189 */-
190void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs)-
191{-
192 if (d->caseSensitivity == cs)
d->caseSensitivity == csDescription
TRUEnever evaluated
FALSEevaluated 186 times by 7 tests
Evaluated by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
0-186
193 return;
never executed: return;
0
194-
195 detach();-
196 d->caseSensitivity = cs;-
197 d->dirty = true;-
198}
executed 186 times by 7 tests: end of block
Executed by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
186
199-
200/*!-
201 \fn Qt::CaseSensitivity QCollator::caseSensitivity() const-
202-
203 Returns case sensitivity of the collator.-
204-
205 \sa setCaseSensitivity()-
206 */-
207Qt::CaseSensitivity QCollator::caseSensitivity() const-
208{-
209 return d->caseSensitivity;
executed 2 times by 1 test: return d->caseSensitivity;
Executed by:
  • tst_QCollator
2
210}-
211-
212/*!-
213 \fn void QCollator::setNumericMode(bool on)-
214-
215 Enables numeric sorting mode when \a on is set to true.-
216-
217 This will enable proper sorting of numeric digits, so that e.g. 100 sorts after 99.-
218-
219 By default this mode is off.-
220-
221 \note On Windows, this functionality makes use of the \l{ICU} library. If Qt was-
222 compiled without ICU support, it falls back to code using native Windows API,-
223 which only works from Windows 7 onwards. On older versions of Windows, it will not work-
224 and a warning will be emitted at runtime.-
225-
226 \sa numericMode()-
227 */-
228void QCollator::setNumericMode(bool on)-
229{-
230 if (d->numericMode == on)
d->numericMode == onDescription
TRUEnever evaluated
FALSEevaluated 162 times by 7 tests
Evaluated by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
0-162
231 return;
never executed: return;
0
232-
233 detach();-
234 d->numericMode = on;-
235 d->dirty = true;-
236}
executed 162 times by 7 tests: end of block
Executed by:
  • tst_QCollator
  • tst_QCompleter
  • tst_QFileDialog2
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QPrinter
  • tst_languageChange
162
237-
238/*!-
239 \fn bool QCollator::numericMode() const-
240-
241 Returns \c true if numeric sorting is enabled, false otherwise.-
242-
243 \sa setNumericMode()-
244 */-
245bool QCollator::numericMode() const-
246{-
247 return d->numericMode;
executed 1 time by 1 test: return d->numericMode;
Executed by:
  • tst_QCollator
1
248}-
249-
250/*!-
251 \fn void QCollator::setIgnorePunctuation(bool on)-
252-
253 If \a on is set to true, punctuation characters and symbols are ignored when determining sort order.-
254-
255 The default is locale dependent.-
256-
257 \note This method is not currently supported on Apple platforms or if Qt is configured to not use ICU on Linux.-
258-
259 \sa ignorePunctuation()-
260 */-
261void QCollator::setIgnorePunctuation(bool on)-
262{-
263 if (d->ignorePunctuation == on)
d->ignorePunctuation == onDescription
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • tst_QCollator
0-1
264 return;
never executed: return;
0
265-
266 detach();-
267 d->ignorePunctuation = on;-
268 d->dirty = true;-
269}
executed 1 time by 1 test: end of block
Executed by:
  • tst_QCollator
1
270-
271/*!-
272 \fn bool QCollator::ignorePunctuation() const-
273-
274 Returns \c true if punctuation characters and symbols are ignored when determining sort order.-
275-
276 \sa setIgnorePunctuation()-
277 */-
278bool QCollator::ignorePunctuation() const-
279{-
280 return d->ignorePunctuation;
executed 1 time by 1 test: return d->ignorePunctuation;
Executed by:
  • tst_QCollator
1
281}-
282-
283/*!-
284 \fn int QCollator::compare(const QString &s1, const QString &s2) const-
285-
286 Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero-
287 depending on whether \a s1 is smaller, equal or larger than \a s2.-
288 */-
289-
290/*!-
291 \fn bool QCollator::operator()(const QString &s1, const QString &s2) const-
292 \internal-
293*/-
294-
295/*!-
296 \fn int QCollator::compare(const QStringRef &s1, const QStringRef &s2) const-
297 \overload-
298-
299 Compares \a s1 with \a s2. Returns an integer less than, equal to, or greater than zero-
300 depending on whether \a s1 is smaller, equal or larger than \a s2.-
301 */-
302-
303/*!-
304 \fn int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const-
305 \overload-
306-
307 Compares \a s1 with \a s2. \a len1 and \a len2 specify the length of the-
308 QChar arrays pointer to by \a s1 and \a s2.-
309-
310 Returns an integer less than, equal to, or greater than zero-
311 depending on whether \a s1 is smaller, equal or larger than \a s2.-
312*/-
313-
314/*!-
315 \fn QCollatorSortKey QCollator::sortKey(const QString &string) const-
316-
317 Returns a sortKey for \a string.-
318-
319 Creating the sort key is usually somewhat slower, than using the compare()-
320 methods directly. But if the string is compared repeatedly (e.g. when sorting-
321 a whole list of strings), it's usually faster to create the sort keys for each-
322 string and then sort using the keys.-
323 */-
324-
325/*!-
326 \class QCollatorSortKey-
327 \inmodule QtCore-
328 \brief The QCollatorSortKey class can be used to speed up string collation.-
329-
330 \since 5.2-
331-
332 The QCollatorSortKey class is always created by QCollator::sortKey()-
333 and is used for fast strings collation, for example when collating many strings.-
334-
335 \reentrant-
336 \ingroup i18n-
337 \ingroup string-processing-
338 \ingroup shared-
339-
340 \sa QCollator, QCollator::sortKey()-
341*/-
342-
343/*!-
344 \internal-
345 */-
346QCollatorSortKey::QCollatorSortKey(QCollatorSortKeyPrivate *d)-
347 : d(d)-
348{-
349}
never executed: end of block
0
350-
351/*!-
352 Constructs a copy of the \a other collator key.-
353*/-
354QCollatorSortKey::QCollatorSortKey(const QCollatorSortKey &other)-
355 : d(other.d)-
356{-
357}
never executed: end of block
0
358-
359/*!-
360 Destroys the collator key.-
361 */-
362QCollatorSortKey::~QCollatorSortKey()-
363{-
364}-
365-
366/*!-
367 Assigns \a other to this collator key.-
368 */-
369QCollatorSortKey& QCollatorSortKey::operator=(const QCollatorSortKey &other)-
370{-
371 if (this != &other) {
this != &otherDescription
TRUEnever evaluated
FALSEnever evaluated
0
372 d = other.d;-
373 }
never executed: end of block
0
374 return *this;
never executed: return *this;
0
375}-
376-
377/*!-
378 \fn QCollatorSortKey &QCollatorSortKey::operator=(QCollatorSortKey && other)-
379-
380 Move-assigns \a other to this collator key.-
381*/-
382-
383/*!-
384 \fn bool operator<(const QCollatorSortKey &lhs, const QCollatorSortKey &rhs)-
385 \relates QCollatorSortKey-
386-
387 According to the QCollator that created the keys, returns \c true if \a lhs-
388 should be sorted before \a rhs; otherwise returns \c false.-
389-
390 \sa QCollatorSortKey::compare()-
391 */-
392-
393/*!-
394 \fn void QCollatorSortKey::swap(QCollatorSortKey & other)-
395-
396 Swaps this collator key with \a other.-
397*/-
398-
399/*!-
400 \fn int QCollatorSortKey::compare(const QCollatorSortKey &otherKey) const-
401-
402 Compares the key to \a otherKey. Returns a negative value if the key-
403 is less than \a otherKey, 0 if the key is equal to \a otherKey or a-
404 positive value if the key is greater than \a otherKey.-
405-
406 \sa operator<()-
407 */-
408-
409QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9