Qt 6.x
The Qt SDK
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
14
15namespace {
17{
20public:
22 GenerationalCollator(const QCollator &copy) : theCollator(copy) {}
24 {
25 int currentGeneration = QLocalePrivate::s_generation.loadRelaxed();
26 if (Q_UNLIKELY(generation != currentGeneration)) {
27 // reinitialize the collator
28 generation = currentGeneration;
29 theCollator = QCollator();
30 }
31 return theCollator;
32 }
33};
34}
36
37
77 : d(new QCollatorPrivate(QLocale().collation()))
78{
79 d->init();
80}
81
88 : d(new QCollatorPrivate(locale))
89{
90}
91
96 : d(other.d)
97{
98 if (d) {
99 // Ensure clean, lest both copies try to init() at the same time:
101 d->ref.ref();
102 }
103}
104
109{
110 if (d && !d->ref.deref())
111 delete d;
112}
113
118{
119 if (this != &other) {
120 if (d && !d->ref.deref())
121 delete d;
122 d = other.d;
123 if (d) {
124 // Ensure clean, lest both copies try to init() at the same time:
126 d->ref.ref();
127 }
128 }
129 return *this;
130}
131
162void QCollator::detach()
163{
164 if (d->ref.loadRelaxed() != 1) {
166 if (!d->ref.deref())
167 delete d;
168 d = x;
169 }
170 // All callers need this, because about to modify the object:
171 d->dirty = true;
172}
173
179void QCollator::setLocale(const QLocale &locale)
180{
181 if (locale == d->locale)
182 return;
183
184 detach();
185 d->locale = locale;
186}
187
197{
198 return d->locale;
199}
200
207{
208 if (d->caseSensitivity == cs)
209 return;
210
211 detach();
212 d->caseSensitivity = cs;
213}
214
228{
229 return d->caseSensitivity;
230}
231
238{
239 if (d->numericMode == on)
240 return;
241
242 detach();
243 d->numericMode = on;
244}
245
257{
258 return d->numericMode;
259}
260
267{
268 if (d->ignorePunctuation == on)
269 return;
270
271 detach();
272 d->ignorePunctuation = on;
273}
274
284{
285 return d->ignorePunctuation;
286}
287
345{
346 return defaultCollator->localData().collator().compare(s1, s2);
347}
348
358{
359 return defaultCollator->localData().collator().sortKey(key.toString());
360}
361
397 : d(d)
398{
399}
400
405 : d(other.d)
406{
407}
408
413{
414}
415
420{
421 if (this != &other) {
422 d = other.d;
423 }
424 return *this;
425}
426
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:18
QExplicitlySharedDataPointer< QCollatorSortKeyPrivate > d
Definition qcollator.h:35
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:42
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:517
\inmodule QtCore
Definition qstringview.h:76
\inmodule QtCore
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 s2
QSharedPointer< T > other(t)
[5]
GenerationalCollator(const QCollator &copy)
Definition qcollator.cpp:22