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

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