From 042d22d98efb5cb17723d23f54b0322d2d576897 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 4 Nov 2024 17:55:00 +0100 Subject: [PATCH] Ensure system/default locale sets number options suitably when it's C In the process, introduce inline helpers to avoid repetition of the ternary that selects the options based on language C or otherwise. Note that, if the system locale changes under our feet, the default locale's number options may be left out of sync with it. It's possible, though kludgy, to work round that; but doing so creates the inverse problem where the caller has created a copy of the system locale, set its number options to something other than the default and set that as default locale. Auto-syncing the default with changes to the system would then be apt to stomp the carefully set number options. Given that the system locale (currently, at least) only changes on program start-up and exit, take the path less kludged. Change-Id: I23ab38b6e8a6936f85c88ca66245a89612e3c3fc Reviewed-by: Magdalena Stojek Reviewed-by: Thiago Macieira --- src/corelib/text/qlocale.cpp | 17 ++++++++++++++--- tests/auto/corelib/text/qlocale/tst_qlocale.cpp | 11 ++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp index 09226e97992..48996eaacbb 100644 --- a/src/corelib/text/qlocale.cpp +++ b/src/corelib/text/qlocale.cpp @@ -770,6 +770,16 @@ static QLocalePrivate *c_private() noexcept return &c_locale; } +static constexpr QLocale::NumberOptions defaultNumberOptions(QLocale::Language forLanguage) +{ + return forLanguage == QLocale::C ? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions; +} + +static constexpr QLocale::NumberOptions defaultNumberOptions(quint16 forLanguage) +{ + return defaultNumberOptions(QLocale::Language(forLanguage)); +} + #ifndef QT_NO_SYSTEMLOCALE /****************************************************************************** ** Default system locale behavior @@ -954,7 +964,8 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l) #endif // QT_NO_DATASTREAM Q_GLOBAL_STATIC(QSharedDataPointer, defaultLocalePrivate, - new QLocalePrivate(defaultData(), defaultIndex())) + new QLocalePrivate(defaultData(), defaultIndex(), + defaultNumberOptions(defaultData()->m_language_id))) static QLocalePrivate *localePrivateByName(QStringView name) { @@ -963,8 +974,7 @@ static QLocalePrivate *localePrivateByName(QStringView name) const qsizetype index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name)); Q_ASSERT(index >= 0 && index < locale_data_size); return new QLocalePrivate(locale_data + index, index, - locale_data[index].m_language_id == QLocale::C - ? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions); + defaultNumberOptions(locale_data[index].m_language_id)); } static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Script script, @@ -2971,6 +2981,7 @@ QLocale QLocale::system() // to ensure that locale's index stays up to date: systemData(&locale.m_index); Q_ASSERT(locale.m_index >= 0 && locale.m_index < locale_data_size); + locale.m_numberOptions = defaultNumberOptions(locale.m_data->m_language_id); return QLocale(locale); } diff --git a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp index 2c13af1f4a1..d421aecb6b8 100644 --- a/tests/auto/corelib/text/qlocale/tst_qlocale.cpp +++ b/tests/auto/corelib/text/qlocale/tst_qlocale.cpp @@ -4232,17 +4232,22 @@ void tst_QLocale::mySystemLocale() QFETCH(QLocale::Language, language); QFETCH(QStringList, uiLanguages); + const QLocale::NumberOptions eno + = language == QLocale::C ? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions; + { MySystemLocale sLocale(name); QCOMPARE(QLocale().language(), language); - QCOMPARE(QLocale::system().language(), language); + const QLocale sys = QLocale::system(); + QCOMPARE(sys.language(), language); auto reporter = qScopeGuard([]() { qDebug("Actual entries:\n\t%s", qPrintable(QLocale::system().uiLanguages().join(u"\n\t"))); }); - QCOMPARE(QLocale::system().uiLanguages(), uiLanguages); - QCOMPARE(QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore), + QCOMPARE(sys.uiLanguages(), uiLanguages); + QCOMPARE(sys.uiLanguages(QLocale::TagSeparator::Underscore), uiLanguages.replaceInStrings(u"-", u"_")); + QCOMPARE(sys.numberOptions(), eno); reporter.dismiss(); }