summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qcollator.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/text/qcollator.cpp')
-rw-r--r--src/corelib/text/qcollator.cpp167
1 files changed, 105 insertions, 62 deletions
diff --git a/src/corelib/text/qcollator.cpp b/src/corelib/text/qcollator.cpp
index 425bfc82ff..1f7e7459e7 100644
--- a/src/corelib/text/qcollator.cpp
+++ b/src/corelib/text/qcollator.cpp
@@ -1,50 +1,39 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2021 The Qt Company Ltd.
+// Copyright (C) 2013 Aleix Pol Gonzalez <aleixpol@kde.org>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qcollator_p.h"
#include "qstringlist.h"
#include "qstring.h"
#include "qdebug.h"
+#include "qlocale_p.h"
+#include "qthreadstorage.h"
QT_BEGIN_NAMESPACE
+QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QCollatorSortKeyPrivate)
+
+namespace {
+struct GenerationalCollator
+{
+ QCollator theCollator;
+ int generation = QLocalePrivate::s_generation.loadRelaxed();
+public:
+ GenerationalCollator() = default;
+ GenerationalCollator(const QCollator &copy) : theCollator(copy) {}
+ QCollator &collator()
+ {
+ int currentGeneration = QLocalePrivate::s_generation.loadRelaxed();
+ if (Q_UNLIKELY(generation != currentGeneration)) {
+ // reinitialize the collator
+ generation = currentGeneration;
+ theCollator = QCollator();
+ }
+ return theCollator;
+ }
+};
+}
+Q_GLOBAL_STATIC(QThreadStorage<GenerationalCollator>, defaultCollator)
/*!
\class QCollator
@@ -59,7 +48,7 @@ QT_BEGIN_NAMESPACE
\ingroup shared
QCollator is initialized with a QLocale. It can then be used to compare and
- sort strings in using the ordering appropriate to the locale.
+ sort strings by using the ordering appropriate for that locale.
A QCollator object can be used together with template-based sorting
algorithms, such as std::sort(), to sort a list with QString entries.
@@ -69,9 +58,25 @@ QT_BEGIN_NAMESPACE
In addition to the locale, several optional flags can be set that influence
the result of the collation.
- \note On Linux, Qt is normally compiled to use ICU. When it isn't, all
- options are ignored and the only supported locales are the system default
- (that \c{setlocale(LC_COLLATE, nullptr)} would report) and the "C" locale.
+ \section1 POSIX fallback implementation
+
+ On Unix systems, Qt is normally compiled to use ICU (except for \macos,
+ where Qt defaults to using an equivalent Apple API). However, if ICU was
+ not available at compile time or explicitly disabled, Qt will use a
+ fallback backend that uses the POSIX API only. This backend has several
+ limitations:
+
+ \list
+ \li Only the QLocale::c() and QLocale::system() locales are supported.
+ Consult the POSIX and C Standard Library manuals for the
+ \c{<locale.h>} header for more information on the system locale.
+ \li caseSensitivity() is not supported: only case-sensitive collation
+ can be performed.
+ \li numericMode() and ignorePunctuation() are not supported.
+ \endlist
+
+ The use of any of the unsupported options will cause a warning to be
+ printed to the application's output.
*/
/*!
@@ -109,8 +114,7 @@ QCollator::QCollator(const QCollator &other)
{
if (d) {
// Ensure clean, lest both copies try to init() at the same time:
- if (d->dirty)
- d->init();
+ d->ensureInitialized();
d->ref.ref();
}
}
@@ -135,8 +139,7 @@ QCollator &QCollator::operator=(const QCollator &other)
d = other.d;
if (d) {
// Ensure clean, lest both copies try to init() at the same time:
- if (d->dirty)
- d->init();
+ d->ensureInitialized();
d->ref.ref();
}
}
@@ -148,19 +151,19 @@ QCollator &QCollator::operator=(const QCollator &other)
Move constructor. Moves from \a other into this collator.
- Note that a moved-from QCollator can only be destroyed or assigned to.
- The effect of calling other functions than the destructor or one of the
- assignment operators is undefined.
+//! [partially-formed]
+ \note The moved-from object \a other is placed in a partially-formed state,
+ in which the only valid operations are destruction and assignment of a new
+ value.
+//! [partially-formed]
*/
/*!
\fn QCollator & QCollator::operator=(QCollator && other)
- Move-assigns from \a other to this collator.
+ Move-assigns \a other to this QCollator instance.
- Note that a moved-from QCollator can only be destroyed or assigned to.
- The effect of calling other functions than the destructor or one of the
- assignment operators is undefined.
+ \include qcollator.cpp partially-formed
*/
/*!
@@ -315,10 +318,10 @@ bool QCollator::ignorePunctuation() const
Compares \a s1 with \a s2.
- Returns an integer less than, equal to, or greater than zero depending on
- whether \a s1 sorts before, with or after \a s2.
+ Returns a negative integer if \a s1 is less than \a s2, a positive integer
+ if it is greater than \a s2, and zero if they are equal.
*/
-#if QT_STRINGVIEW_LEVEL < 2
+
/*!
\fn bool QCollator::operator()(const QString &s1, const QString &s2) const
\overload
@@ -332,17 +335,47 @@ bool QCollator::ignorePunctuation() const
*/
/*!
- \fn int QCollator::compare(const QChar *s1, int len1, const QChar *s2, int len2) const
+ \fn int QCollator::compare(const QChar *s1, qsizetype len1, const QChar *s2, qsizetype len2) const
\overload
\since 5.2
Compares \a s1 with \a s2. \a len1 and \a len2 specify the lengths of the
QChar arrays pointed to by \a s1 and \a s2.
- Returns an integer less than, equal to, or greater than zero depending on
- whether \a s1 sorts before, with or after \a s2.
+ Returns a negative integer if \a s1 is less than \a s2, a positive integer
+ if it is greater than \a s2, and zero if they are equal.
+
+
+ \note In Qt versions prior to 6.4, the length arguments were of type
+ \c{int}, not \c{qsizetype}.
+*/
+
+/*!
+ \since 6.3
+
+ Compares the strings \a s1 and \a s2, returning their sorting order. This
+ function performs the same operation as compare() on a default-constructed
+ QCollator object.
+
+ \sa compare(), defaultSortKey()
*/
-#endif // QT_STRINGVIEW_LEVEL < 2
+int QCollator::defaultCompare(QStringView s1, QStringView s2)
+{
+ return defaultCollator->localData().collator().compare(s1, s2);
+}
+
+/*!
+ \since 6.3
+
+ Returns the sort key for the string \a key. This function performs the same
+ operation as sortKey() on a default-constructed QCollator object.
+
+ \sa sortKey(), defaultCompare()
+*/
+QCollatorSortKey QCollator::defaultSortKey(QStringView key)
+{
+ return defaultCollator->localData().collator().sortKey(key.toString());
+}
/*!
\fn QCollatorSortKey QCollator::sortKey(const QString &string) const
@@ -392,6 +425,14 @@ QCollatorSortKey::QCollatorSortKey(const QCollatorSortKey &other)
}
/*!
+ \since 6.8
+ \fn QCollatorSortKey::QCollatorSortKey(QCollatorSortKey &&other)
+ Move-constructs a new QCollatorSortKey from \a other.
+
+ \include qcollator.cpp partially-formed
+*/
+
+/*!
Destroys the collator key.
*/
QCollatorSortKey::~QCollatorSortKey()
@@ -412,7 +453,9 @@ QCollatorSortKey& QCollatorSortKey::operator=(const QCollatorSortKey &other)
/*!
\fn QCollatorSortKey &QCollatorSortKey::operator=(QCollatorSortKey && other)
- Move-assigns \a other to this collator key.
+ Move-assigns \a other to this QCollatorSortKey instance.
+
+ \include qcollator.cpp partially-formed
*/
/*!