diff --git a/src/corelib/text/qcollator.cpp b/src/corelib/text/qcollator.cpp index 990c68c062..fd00082cf7 100644 --- a/src/corelib/text/qcollator.cpp +++ b/src/corelib/text/qcollator.cpp @@ -97,8 +97,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(); } } @@ -123,8 +122,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(); } } diff --git a/src/corelib/text/qcollator_icu.cpp b/src/corelib/text/qcollator_icu.cpp index fbc422c342..afd58b5d5e 100644 --- a/src/corelib/text/qcollator_icu.cpp +++ b/src/corelib/text/qcollator_icu.cpp @@ -78,8 +78,7 @@ int QCollator::compare(QStringView s1, QStringView s2) const if (!s2.size()) return +1; - if (d->dirty) - d->init(); + d->ensureInitialized(); if (d->collator) { return ucol_strcoll(d->collator, @@ -92,8 +91,8 @@ int QCollator::compare(QStringView s1, QStringView s2) const QCollatorSortKey QCollator::sortKey(const QString &string) const { - if (d->dirty) - d->init(); + d->ensureInitialized(); + if (d->isC()) return QCollatorSortKey(new QCollatorSortKeyPrivate(string.toUtf8())); diff --git a/src/corelib/text/qcollator_macx.cpp b/src/corelib/text/qcollator_macx.cpp index 9a57ef78fc..23c23bd53a 100644 --- a/src/corelib/text/qcollator_macx.cpp +++ b/src/corelib/text/qcollator_macx.cpp @@ -63,8 +63,8 @@ int QCollator::compare(QStringView s1, QStringView s2) const if (!s2.size()) return +1; - if (d->dirty) - d->init(); + d->ensureInitialized(); + if (!d->collator) return s1.compare(s2, caseSensitivity()); @@ -82,8 +82,8 @@ int QCollator::compare(QStringView s1, QStringView s2) const QCollatorSortKey QCollator::sortKey(const QString &string) const { - if (d->dirty) - d->init(); + d->ensureInitialized(); + if (!d->collator) { // What should (or even *can*) we do here ? (See init()'s comment.) qWarning("QCollator doesn't support sort keys for the C locale on Darwin"); diff --git a/src/corelib/text/qcollator_p.h b/src/corelib/text/qcollator_p.h index 6da153cdf0..b96cdbaa32 100644 --- a/src/corelib/text/qcollator_p.h +++ b/src/corelib/text/qcollator_p.h @@ -74,6 +74,12 @@ public: collator = NoCollator; } + void ensureInitialized() + { + if (dirty) + init(); + } + // Implemented by each back-end, in its own way: void init(); void cleanup(); diff --git a/src/corelib/text/qcollator_posix.cpp b/src/corelib/text/qcollator_posix.cpp index b409dbd96d..bddb748535 100644 --- a/src/corelib/text/qcollator_posix.cpp +++ b/src/corelib/text/qcollator_posix.cpp @@ -50,8 +50,8 @@ int QCollator::compare(QStringView s1, QStringView s2) const if (d->isC()) return s1.compare(s2, caseSensitivity()); - if (d->dirty) - d->init(); + + d->ensureInitialized(); QVarLengthArray array1, array2; stringToWCharArray(array1, s1); @@ -61,8 +61,7 @@ int QCollator::compare(QStringView s1, QStringView s2) const QCollatorSortKey QCollator::sortKey(const QString &string) const { - if (d->dirty) - d->init(); + d->ensureInitialized(); QVarLengthArray original; stringToWCharArray(original, string); diff --git a/src/corelib/text/qcollator_win.cpp b/src/corelib/text/qcollator_win.cpp index 45cf5488ad..405337924f 100644 --- a/src/corelib/text/qcollator_win.cpp +++ b/src/corelib/text/qcollator_win.cpp @@ -57,8 +57,7 @@ int QCollator::compare(QStringView s1, QStringView s2) const if (d->isC()) return s1.compare(s2, d->caseSensitivity); - if (d->dirty) - d->init(); + d->ensureInitialized(); //* from Windows documentation * // Returns one of the following values if successful. To maintain the C @@ -92,8 +91,8 @@ int QCollator::compare(QStringView s1, QStringView s2) const QCollatorSortKey QCollator::sortKey(const QString &string) const { - if (d->dirty) - d->init(); + d->ensureInitialized(); + if (d->isC()) return QCollatorSortKey(new QCollatorSortKeyPrivate(string));