QCollator: Extract Method Private::ensureInitialized()

Avoids repetition of if (d->dirty) d->init() all over the place.

Change-Id: Ifc819151b7c694e6cc1f48bbb837b37d108ca49a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit d4b7c81e9e77a38dd294e52f65ed5426b40f1396)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-07-18 15:22:16 +02:00 committed by Qt Cherry-pick Bot
parent a7cb167a7d
commit 21c1b8606f
6 changed files with 21 additions and 20 deletions

View File

@ -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();
}
}

View File

@ -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()));

View File

@ -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");

View File

@ -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();

View File

@ -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<wchar_t> 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<wchar_t> original;
stringToWCharArray(original, string);

View File

@ -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));