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