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 <magdalena.stojek@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2024-11-04 17:55:00 +01:00
parent f5e127c842
commit 042d22d98e
2 changed files with 22 additions and 6 deletions

View File

@ -770,6 +770,16 @@ static QLocalePrivate *c_private() noexcept
return &c_locale; 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 #ifndef QT_NO_SYSTEMLOCALE
/****************************************************************************** /******************************************************************************
** Default system locale behavior ** Default system locale behavior
@ -954,7 +964,8 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l)
#endif // QT_NO_DATASTREAM #endif // QT_NO_DATASTREAM
Q_GLOBAL_STATIC(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate, Q_GLOBAL_STATIC(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
new QLocalePrivate(defaultData(), defaultIndex())) new QLocalePrivate(defaultData(), defaultIndex(),
defaultNumberOptions(defaultData()->m_language_id)))
static QLocalePrivate *localePrivateByName(QStringView name) static QLocalePrivate *localePrivateByName(QStringView name)
{ {
@ -963,8 +974,7 @@ static QLocalePrivate *localePrivateByName(QStringView name)
const qsizetype index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name)); const qsizetype index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name));
Q_ASSERT(index >= 0 && index < locale_data_size); Q_ASSERT(index >= 0 && index < locale_data_size);
return new QLocalePrivate(locale_data + index, index, return new QLocalePrivate(locale_data + index, index,
locale_data[index].m_language_id == QLocale::C defaultNumberOptions(locale_data[index].m_language_id));
? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions);
} }
static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Script script, 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: // to ensure that locale's index stays up to date:
systemData(&locale.m_index); systemData(&locale.m_index);
Q_ASSERT(locale.m_index >= 0 && locale.m_index < locale_data_size); 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); return QLocale(locale);
} }

View File

@ -4232,17 +4232,22 @@ void tst_QLocale::mySystemLocale()
QFETCH(QLocale::Language, language); QFETCH(QLocale::Language, language);
QFETCH(QStringList, uiLanguages); QFETCH(QStringList, uiLanguages);
const QLocale::NumberOptions eno
= language == QLocale::C ? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions;
{ {
MySystemLocale sLocale(name); MySystemLocale sLocale(name);
QCOMPARE(QLocale().language(), language); QCOMPARE(QLocale().language(), language);
QCOMPARE(QLocale::system().language(), language); const QLocale sys = QLocale::system();
QCOMPARE(sys.language(), language);
auto reporter = qScopeGuard([]() { auto reporter = qScopeGuard([]() {
qDebug("Actual entries:\n\t%s", qDebug("Actual entries:\n\t%s",
qPrintable(QLocale::system().uiLanguages().join(u"\n\t"))); qPrintable(QLocale::system().uiLanguages().join(u"\n\t")));
}); });
QCOMPARE(QLocale::system().uiLanguages(), uiLanguages); QCOMPARE(sys.uiLanguages(), uiLanguages);
QCOMPARE(QLocale::system().uiLanguages(QLocale::TagSeparator::Underscore), QCOMPARE(sys.uiLanguages(QLocale::TagSeparator::Underscore),
uiLanguages.replaceInStrings(u"-", u"_")); uiLanguages.replaceInStrings(u"-", u"_"));
QCOMPARE(sys.numberOptions(), eno);
reporter.dismiss(); reporter.dismiss();
} }