From ea1a6d3ae0ab48410cfd69e2b115ccd2cce5a27f Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 21 Aug 2024 16:23:55 +0200 Subject: [PATCH] QLocale: add (in)equality operators with QLocale::Language 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 71bbc35a3774ba7411970ff74068f5211b73e425. As a drive-by: fix the order of the test slots in the unit test. Change-Id: I37f8f5881469b7734705e06e471746814dd2ddf0 Reviewed-by: Edward Welbourne Reviewed-by: Thiago Macieira (cherry picked from commit 403b1abcad5f437c51dfabd7095b814e99e197a1) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qlocale.cpp | 26 +++++++++++++++++++ src/corelib/text/qlocale.h | 3 +++ .../auto/corelib/text/qlocale/tst_qlocale.cpp | 25 ++++++++++++++---- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 82ea7ebc447..7b23800965c 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -913,6 +913,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 systemLocaleString(const QLocaleData *that, QSystemLocale::QueryType type) { diff --git a/src/corelib/text/qlocale.h b/src/corelib/text/qlocale.h index 95c9b4bf64e..55b9ffbeda3 100644 --- a/src/corelib/text/qlocale.h +++ b/src/corelib/text/qlocale.h @@ -1180,6 +1180,9 @@ private: } 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 d; }; Q_DECLARE_SHARED(QLocale) diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 63175f8c136..e956454a012 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -42,6 +42,7 @@ public: private slots: void initTestCase(); void compareCompiles(); + void compareWithLanguage(); #if defined(Q_OS_WIN) void windowsDefaultLocale(); #endif @@ -191,11 +192,6 @@ tst_QLocale::tst_QLocale() qRegisterMetaType("QLocale::FormatType"); } -void tst_QLocale::compareCompiles() -{ - QTestPrivate::testEqualityOperatorsCompile(); -} - void tst_QLocale::initTestCase() { #ifdef Q_OS_ANDROID @@ -227,6 +223,25 @@ void tst_QLocale::initTestCase() #endif // QT_CONFIG(process) } +void tst_QLocale::compareCompiles() +{ + QTestPrivate::testEqualityOperatorsCompile(); + QTestPrivate::testEqualityOperatorsCompile(); +} + +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("reqLang");