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 <edward.welbourne@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> (cherry picked from commit 403b1abcad5f437c51dfabd7095b814e99e197a1) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
5c5bedd2a6
commit
ea1a6d3ae0
@ -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<QString>
|
||||
systemLocaleString(const QLocaleData *that, QSystemLocale::QueryType type)
|
||||
{
|
||||
|
@ -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<QLocalePrivate> d;
|
||||
};
|
||||
Q_DECLARE_SHARED(QLocale)
|
||||
|
@ -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>("QLocale::FormatType");
|
||||
}
|
||||
|
||||
void tst_QLocale::compareCompiles()
|
||||
{
|
||||
QTestPrivate::testEqualityOperatorsCompile<QLocale>();
|
||||
}
|
||||
|
||||
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<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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user