Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qcollator.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | - | |||||||||||||
41 | QT_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 | */ | - | ||||||||||||
73 | QCollator::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:
| 191 | ||||||||||||
79 | - | |||||||||||||
80 | /*! | - | ||||||||||||
81 | Creates a copy of \a other. | - | ||||||||||||
82 | */ | - | ||||||||||||
83 | QCollator::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:
| 4805 | ||||||||||||
88 | - | |||||||||||||
89 | /*! | - | ||||||||||||
90 | Destroys the collator. | - | ||||||||||||
91 | */ | - | ||||||||||||
92 | QCollator::~QCollator() | - | ||||||||||||
93 | { | - | ||||||||||||
94 | if (d && !d->ref.deref())
| 1-4996 | ||||||||||||
95 | delete d; executed 191 times by 13 tests: delete d; Executed by:
| 191 | ||||||||||||
96 | } executed 4997 times by 19 tests: end of block Executed by:
| 4997 | ||||||||||||
97 | - | |||||||||||||
98 | /*! | - | ||||||||||||
99 | Assigns \a other to this collator. | - | ||||||||||||
100 | */ | - | ||||||||||||
101 | QCollator &QCollator::operator=(const QCollator &other) | - | ||||||||||||
102 | { | - | ||||||||||||
103 | if (this != &other) {
| 0 | ||||||||||||
104 | if (d && !d->ref.deref())
| 0 | ||||||||||||
105 | delete d; never executed: delete d; | 0 | ||||||||||||
106 | d = other.d; | - | ||||||||||||
107 | if (d) d->ref.ref(); never executed: d->ref.ref();
| 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 | */ | - | ||||||||||||
142 | void QCollator::detach() | - | ||||||||||||
143 | { | - | ||||||||||||
144 | if (d->ref.load() != 1) {
| 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())
| 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:
| 348 | ||||||||||||
155 | - | |||||||||||||
156 | /*! | - | ||||||||||||
157 | Sets the locale of the collator to \a locale. | - | ||||||||||||
158 | */ | - | ||||||||||||
159 | void QCollator::setLocale(const QLocale &locale) | - | ||||||||||||
160 | { | - | ||||||||||||
161 | if (locale == d->locale)
| 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:
| 3 | ||||||||||||
168 | - | |||||||||||||
169 | /*! | - | ||||||||||||
170 | Returns the locale of the collator. | - | ||||||||||||
171 | */ | - | ||||||||||||
172 | QLocale QCollator::locale() const | - | ||||||||||||
173 | { | - | ||||||||||||
174 | return d->locale; executed 5 times by 1 test: return d->locale; Executed by:
| 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 | */ | - | ||||||||||||
184 | void QCollator::setCaseSensitivity(Qt::CaseSensitivity cs) | - | ||||||||||||
185 | { | - | ||||||||||||
186 | if (d->caseSensitivity == cs)
| 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:
| 184 | ||||||||||||
193 | - | |||||||||||||
194 | /*! | - | ||||||||||||
195 | \fn Qt::CaseSensitivity QCollator::caseSensitivity() const | - | ||||||||||||
196 | - | |||||||||||||
197 | Returns case sensitivity of the collator. | - | ||||||||||||
198 | - | |||||||||||||
199 | \sa setCaseSensitivity() | - | ||||||||||||
200 | */ | - | ||||||||||||
201 | Qt::CaseSensitivity QCollator::caseSensitivity() const | - | ||||||||||||
202 | { | - | ||||||||||||
203 | return d->caseSensitivity; executed 2 times by 1 test: return d->caseSensitivity; Executed by:
| 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 | */ | - | ||||||||||||
222 | void QCollator::setNumericMode(bool on) | - | ||||||||||||
223 | { | - | ||||||||||||
224 | if (d->numericMode == on)
| 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:
| 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 | */ | - | ||||||||||||
239 | bool QCollator::numericMode() const | - | ||||||||||||
240 | { | - | ||||||||||||
241 | return d->numericMode; executed 1 time by 1 test: return d->numericMode; Executed by:
| 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 | */ | - | ||||||||||||
255 | void QCollator::setIgnorePunctuation(bool on) | - | ||||||||||||
256 | { | - | ||||||||||||
257 | if (d->ignorePunctuation == on)
| 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:
| 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 | */ | - | ||||||||||||
272 | bool QCollator::ignorePunctuation() const | - | ||||||||||||
273 | { | - | ||||||||||||
274 | return d->ignorePunctuation; executed 1 time by 1 test: return d->ignorePunctuation; Executed by:
| 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 | */ | - | ||||||||||||
340 | QCollatorSortKey::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 | */ | - | ||||||||||||
348 | QCollatorSortKey::QCollatorSortKey(const QCollatorSortKey &other) | - | ||||||||||||
349 | : d(other.d) | - | ||||||||||||
350 | { | - | ||||||||||||
351 | } never executed: end of block | 0 | ||||||||||||
352 | - | |||||||||||||
353 | /*! | - | ||||||||||||
354 | Destroys the collator key. | - | ||||||||||||
355 | */ | - | ||||||||||||
356 | QCollatorSortKey::~QCollatorSortKey() | - | ||||||||||||
357 | { | - | ||||||||||||
358 | } | - | ||||||||||||
359 | - | |||||||||||||
360 | /*! | - | ||||||||||||
361 | Assigns \a other to this collator key. | - | ||||||||||||
362 | */ | - | ||||||||||||
363 | QCollatorSortKey& QCollatorSortKey::operator=(const QCollatorSortKey &other) | - | ||||||||||||
364 | { | - | ||||||||||||
365 | if (this != &other) {
| 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 | - | |||||||||||||
403 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |