summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qcollator.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-07-30 13:45:46 -0700
committerEdward Welbourne <edward.welbourne@qt.io>2021-11-01 22:30:23 +0200
commita9d8794cf0be617d9eedbf9bb0d6d5401735d6f2 (patch)
tree16fea80f32bebf3f815f0122371738403d9f015c /src/corelib/text/qcollator.cpp
parent25d807f6292e9918ccdbe06cf7f590f60ecae457 (diff)
QCollator: add public, static functions to do comparisons
Collation with the default QCollator object (no numeric, punctuation or case sensitivity changes) is a common-place occurrence, so add two functions to do this work. It's also what QString::localeAwareCompare() calls. The test ends up testing that default, static collator updates after the default QLocale changes too. Task-number: QTBUG-95050 Change-Id: I7e0b82c2d2fe464082d8fffd1696ac77f32840b2 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/corelib/text/qcollator.cpp')
-rw-r--r--src/corelib/text/qcollator.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/corelib/text/qcollator.cpp b/src/corelib/text/qcollator.cpp
index 425bfc82ff..8bffae83da 100644
--- a/src/corelib/text/qcollator.cpp
+++ b/src/corelib/text/qcollator.cpp
@@ -43,9 +43,33 @@
#include "qstring.h"
#include "qdebug.h"
+#include "qlocale_p.h"
+#include "qthreadstorage.h"
QT_BEGIN_NAMESPACE
+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
\inmodule QtCore
@@ -345,6 +369,33 @@ bool QCollator::ignorePunctuation() const
#endif // QT_STRINGVIEW_LEVEL < 2
/*!
+ \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()
+*/
+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
Returns a sortKey for \a string.