Skip to content

Commit

Permalink
QLocale: add (in)equality operators with QLocale::Language
Browse files Browse the repository at this point in the history
We do have an implicit constructor which allows construction of a
QLocale from the QLocale::Language enum values. It will be used when
doing the comparison.
However, do not rely on the implicit conversions, and provide the
operators that explicitly do the right thing. This also allows us to
save an extra allocation of QLocalePrivate.

Found in Qt 6.8 API review.

Amends 71bbc35.

As a drive-by: fix the order of the test slots in the unit test.

Pick-to: 6.8
Change-Id: I37f8f5881469b7734705e06e471746814dd2ddf0
Reviewed-by: Edward Welbourne <[email protected]>
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
isolovev committed Sep 3, 2024
1 parent 205c64b commit 403b1ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/corelib/text/qlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,32 @@ static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Sc
return new QLocalePrivate(data, index, numberOptions);
}

bool comparesEqual(const QLocale &loc, QLocale::Language lang)
{
// Keep in sync with findLocalePrivate()!
auto compareWithPrivate = [&loc](const QLocaleData *data, QLocale::NumberOptions opts)
{
return loc.d->m_data == data && loc.d->m_numberOptions == opts;
};

if (lang == QLocale::C)
return compareWithPrivate(c_private()->m_data, c_private()->m_numberOptions);

qsizetype index = QLocaleData::findLocaleIndex(QLocaleId { lang });
Q_ASSERT(index >= 0 && index < locale_data_size);
const QLocaleData *data = locale_data + index;

QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions;

// If not found, should use default locale:
if (data->m_language_id == QLocale::C) {
if (defaultLocalePrivate.exists())
numberOptions = defaultLocalePrivate->data()->m_numberOptions;
data = defaultData();
}
return compareWithPrivate(data, numberOptions);
}

static std::optional<QString>
systemLocaleString(const QLocaleData *that, QSystemLocale::QueryType type)
{
Expand Down
3 changes: 3 additions & 0 deletions src/corelib/text/qlocale.h
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,9 @@ class Q_CORE_EXPORT QLocale
}
Q_DECLARE_EQUALITY_COMPARABLE(QLocale)

friend Q_CORE_EXPORT bool comparesEqual(const QLocale &lhs, Language rhs);
Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QLocale, Language)

QSharedDataPointer<QLocalePrivate> d;
};
Q_DECLARE_SHARED(QLocale)
Expand Down
25 changes: 20 additions & 5 deletions tests/auto/corelib/text/qlocale/tst_qlocale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class tst_QLocale : public QObject
private slots:
void initTestCase();
void compareCompiles();
void compareWithLanguage();
#if defined(Q_OS_WIN)
void windowsDefaultLocale();
#endif
Expand Down Expand Up @@ -191,11 +192,6 @@ tst_QLocale::tst_QLocale()
qRegisterMetaType<QLocale::FormatType>("QLocale::FormatType");
}

void tst_QLocale::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QLocale>();
}

void tst_QLocale::initTestCase()
{
#ifdef Q_OS_ANDROID
Expand Down Expand Up @@ -227,6 +223,25 @@ void tst_QLocale::initTestCase()
#endif // QT_CONFIG(process)
}

void tst_QLocale::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QLocale>();
QTestPrivate::testEqualityOperatorsCompile<QLocale, QLocale::Language>();
}

void tst_QLocale::compareWithLanguage()
{
QLocale de(QLocale::German);
QT_TEST_EQUALITY_OPS(de, QLocale::German, true);
QT_TEST_EQUALITY_OPS(de, QLocale::English, false);

QLocale en_DE(QLocale::English, QLocale::Germany);
QCOMPARE_EQ(en_DE.language(), QLocale::English);
QCOMPARE_EQ(en_DE.territory(), QLocale::Germany);
// Territory won't match
QT_TEST_EQUALITY_OPS(en_DE, QLocale::English, false);
}

void tst_QLocale::ctor_data()
{
QTest::addColumn<QLocale::Language>("reqLang");
Expand Down

0 comments on commit 403b1ab

Please sign in to comment.