Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcollator.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qcollator_p.h"
6#include "qstringlist.h"
7#include "qstring.h"
8
9#include "qdebug.h"
10#include "qlocale_p.h"
11#include "qthreadstorage.h"
12
15
16namespace {
18{
20 int generation = QLocalePrivate::s_generation.loadRelaxed();
21public:
23 GenerationalCollator(const QCollator &copy) : theCollator(copy) {}
25 {
26 int currentGeneration = QLocalePrivate::s_generation.loadRelaxed();
27 if (Q_UNLIKELY(generation != currentGeneration)) {
28 // reinitialize the collator
29 generation = currentGeneration;
30 theCollator = QCollator();
31 }
32 return theCollator;
33 }
34};
35}
36Q_GLOBAL_STATIC(QThreadStorage<GenerationalCollator>, defaultCollator)
37
38
94 : d(new QCollatorPrivate(QLocale().collation()))
95{
96 d->init();
97}
98
105 : d(new QCollatorPrivate(locale))
106{
107}
108
113 : d(other.d)
114{
115 if (d) {
116 // Ensure clean, lest both copies try to init() at the same time:
118 d->ref.ref();
119 }
120}
121
126{
127 if (d && !d->ref.deref())
128 delete d;
129}
130
135{
136 if (this != &other) {
137 if (d && !d->ref.deref())
138 delete d;
139 d = other.d;
140 if (d) {
141 // Ensure clean, lest both copies try to init() at the same time:
143 d->ref.ref();
144 }
145 }
146 return *this;
147}
148
155
159
179void QCollator::detach()
180{
181 if (d->ref.loadRelaxed() != 1) {
183 if (!d->ref.deref())
184 delete d;
185 d = x;
186 }
187 // All callers need this, because about to modify the object:
188 d->dirty = true;
189}
190
196void QCollator::setLocale(const QLocale &locale)
197{
198 if (locale == d->locale)
199 return;
200
201 detach();
202 d->locale = locale;
203}
204
214{
215 return d->locale;
216}
217
224{
225 if (d->caseSensitivity == cs)
226 return;
227
228 detach();
229 d->caseSensitivity = cs;
230}
231
248
255{
256 if (d->numericMode == on)
257 return;
258
259 detach();
260 d->numericMode = on;
261}
262
274{
275 return d->numericMode;
276}
277
284{
285 if (d->ignorePunctuation == on)
286 return;
287
288 detach();
289 d->ignorePunctuation = on;
290}
291
301{
302 return d->ignorePunctuation;
303}
304
363{
364 return defaultCollator->localData().collator().compare(s1, s2);
365}
366
376{
377 return defaultCollator->localData().collator().sortKey(key.toString());
378}
379
418
426
441
446{
447 if (this != &other) {
448 d = other.d;
449 }
450 return *this;
451}
452
bool ref() noexcept
bool deref() noexcept
T loadRelaxed() const noexcept
QAtomicInt ref
Definition qcollator_p.h:56
void ensureInitialized()
Definition qcollator_p.h:77
Qt::CaseSensitivity caseSensitivity
Definition qcollator_p.h:61
\inmodule QtCore
Definition qcollator.h:19
QExplicitlySharedDataPointer< QCollatorSortKeyPrivate > d
Definition qcollator.h:37
QCollatorSortKey(const QCollatorSortKey &other)
Constructs a copy of the other collator key.
~QCollatorSortKey()
Destroys the collator key.
QCollatorSortKey & operator=(const QCollatorSortKey &other)
Assigns other to this collator key.
\inmodule QtCore
Definition qcollator.h:44
bool numericMode() const
Returns true if numeric sorting is enabled, false otherwise.
QLocale locale() const
Returns the locale of the collator.
void setIgnorePunctuation(bool on)
Ignores punctuation and symbols if on is true, attends to them if false.
QCollator & operator=(const QCollator &)
Assigns other to this collator.
void setNumericMode(bool on)
Enables numeric sorting mode when on is true.
bool ignorePunctuation() const
Returns whether punctuation and symbols are ignored when collating.
void setCaseSensitivity(Qt::CaseSensitivity cs)
Sets the case-sensitivity of the collator to cs.
void setLocale(const QLocale &locale)
Sets the locale of the collator to locale.
static int defaultCompare(QStringView s1, QStringView s2)
Qt::CaseSensitivity caseSensitivity() const
Returns case sensitivity of the collator.
static QCollatorSortKey defaultSortKey(QStringView key)
~QCollator()
Destroys this collator.
static QBasicAtomicInt s_generation
Definition qlocale_p.h:534
\inmodule QtCore
Definition qstringview.h:78
Combined button and popup list for selecting options.
CaseSensitivity
static jboolean copy(JNIEnv *, jobject)
#define Q_UNLIKELY(x)
#define Q_GLOBAL_STATIC(TYPE, NAME,...)
GLint GLint GLint GLint GLint x
[0]
GLuint64 key
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
#define s2
QSharedPointer< T > other(t)
[5]
GenerationalCollator(const QCollator &copy)
Definition qcollator.cpp:23