summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qlocale.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/text/qlocale.cpp')
-rw-r--r--src/corelib/text/qlocale.cpp164
1 files changed, 131 insertions, 33 deletions
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index 75a5bc802e..11e788c200 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2019 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -651,7 +651,6 @@ int qt_repeatCount(QStringView s)
}
static const QLocaleData *default_data = nullptr;
-static QLocale::NumberOptions default_number_options = QLocale::DefaultNumberOptions;
static const QLocaleData *const c_data = locale_data;
static QLocalePrivate *c_private()
@@ -661,6 +660,11 @@ static QLocalePrivate *c_private()
return &c_locale;
}
+static const QLocaleData *systemData();
+static QLocale::NumberOptions system_number_options = QLocale::DefaultNumberOptions;
+Q_GLOBAL_STATIC_WITH_ARGS(QExplicitlySharedDataPointer<QLocalePrivate>, systemLocalePrivate,
+ (QLocalePrivate::create(systemData(), system_number_options)))
+
#ifndef QT_NO_SYSTEMLOCALE
/******************************************************************************
** Default system locale behavior
@@ -712,6 +716,7 @@ static void updateSystemPrivate()
{
// This function is NOT thread-safe!
// It *should not* be called by anything but systemData()
+ // It *is* called before {system,default}LocalePrivate exist.
const QSystemLocale *sys_locale = systemLocale();
// tell the object that the system locale has changed.
@@ -719,11 +724,14 @@ static void updateSystemPrivate()
// Populate global with fallback as basis:
globalLocaleData = *sys_locale->fallbackUiLocaleData();
+ system_number_options = QLocale::DefaultNumberOptions;
QVariant res = sys_locale->query(QSystemLocale::LanguageId, QVariant());
if (!res.isNull()) {
globalLocaleData.m_language_id = res.toInt();
globalLocaleData.m_script_id = QLocale::AnyScript; // default for compatibility
+ if (globalLocaleData.m_language_id == QLocale::C)
+ system_number_options = QLocale::OmitGroupSeparator;
}
res = sys_locale->query(QSystemLocale::CountryId, QVariant());
if (!res.isNull()) {
@@ -735,24 +743,45 @@ static void updateSystemPrivate()
globalLocaleData.m_script_id = res.toInt();
res = sys_locale->query(QSystemLocale::DecimalPoint, QVariant());
- if (!res.isNull())
+ if (!res.isNull() && !res.toString().isEmpty())
globalLocaleData.m_decimal = res.toString().at(0).unicode();
+ // System may supply empty group separator to say we should omit grouping;
+ // and it makes no sense to use the same separator for decimal and grouping
+ // (which might happen by system supplying, as decimal, what CLDR has given
+ // us for grouping; or the other way round). Assume, at least, that each of
+ // system and CLDR has decimal != group, all the same.
res = sys_locale->query(QSystemLocale::GroupSeparator, QVariant());
- if (!res.isNull())
- globalLocaleData.m_group = res.toString().at(0).unicode();
+ if (res.isNull()) {
+ // The case where system over-rides decimal but not group, and its
+ // decimal clashes with CLDR's group.
+ if (globalLocaleData.m_group == globalLocaleData.m_decimal)
+ system_number_options |= QLocale::OmitGroupSeparator;
+ } else if (res.toString().isEmpty()) {
+ system_number_options |= QLocale::OmitGroupSeparator;
+ } else {
+ const ushort group = res.toString().at(0).unicode();
+ if (group != globalLocaleData.m_decimal)
+ globalLocaleData.m_group = group;
+ else if (group == globalLocaleData.m_group)
+ qWarning("System-supplied decimal and grouping character are both 0x%hx", group);
+ }
res = sys_locale->query(QSystemLocale::ZeroDigit, QVariant());
- if (!res.isNull())
+ if (!res.isNull() && !res.toString().isEmpty())
globalLocaleData.m_zero = res.toString().at(0).unicode();
res = sys_locale->query(QSystemLocale::NegativeSign, QVariant());
- if (!res.isNull())
+ if (!res.isNull() && !res.toString().isEmpty())
globalLocaleData.m_minus = res.toString().at(0).unicode();
res = sys_locale->query(QSystemLocale::PositiveSign, QVariant());
- if (!res.isNull())
+ if (!res.isNull() && !res.toString().isEmpty())
globalLocaleData.m_plus = res.toString().at(0).unicode();
+
+ if (systemLocalePrivate.exists())
+ systemLocalePrivate->data()->m_numberOptions = system_number_options;
+ // else: system_number_options will be passed to create() when constructing.
}
#endif // !QT_NO_SYSTEMLOCALE
@@ -834,9 +863,7 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l)
static const int locale_data_size = sizeof(locale_data)/sizeof(QLocaleData) - 1;
Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
- (QLocalePrivate::create(defaultData(), default_number_options)))
-Q_GLOBAL_STATIC_WITH_ARGS(QExplicitlySharedDataPointer<QLocalePrivate>, systemLocalePrivate,
- (QLocalePrivate::create(systemData())))
+ (QLocalePrivate::create(defaultData())))
static QLocalePrivate *localePrivateByName(const QString &name)
{
@@ -862,8 +889,9 @@ static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Sc
QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions;
// If not found, should default to system
- if (data->m_language_id == QLocale::C && language != QLocale::C) {
- numberOptions = default_number_options;
+ if (data->m_language_id == QLocale::C) {
+ if (defaultLocalePrivate.exists())
+ numberOptions = defaultLocalePrivate->data()->m_numberOptions;
data = defaultData();
}
return QLocalePrivate::create(data, offset, numberOptions);
@@ -1048,6 +1076,8 @@ uint qHash(const QLocale &key, uint seed) noexcept
Sets the \a options related to number conversions for this
QLocale instance.
+
+ \sa numberOptions()
*/
void QLocale::setNumberOptions(NumberOptions options)
{
@@ -1060,7 +1090,10 @@ void QLocale::setNumberOptions(NumberOptions options)
Returns the options related to number conversions for this
QLocale instance.
- By default, no options are set for the standard locales.
+ By default, no options are set for the standard locales, except
+ for the "C" locale, which has OmitGroupSeparator set by default.
+
+ \sa setNumberOptions(), toString(), groupSeparator()
*/
QLocale::NumberOptions QLocale::numberOptions() const
{
@@ -1170,12 +1203,9 @@ QString QLocale::createSeparatedList(const QStringList &list) const
void QLocale::setDefault(const QLocale &locale)
{
default_data = locale.d->m_data;
- default_number_options = locale.numberOptions();
- if (defaultLocalePrivate.exists()) {
- // update the cached private
+ if (defaultLocalePrivate.exists()) // update the cached private
*defaultLocalePrivate = locale.d;
- }
}
/*!
@@ -1962,14 +1992,15 @@ double QLocale::toDouble(QStringView s, bool *ok) const
/*!
Returns a localized string representation of \a i.
- \sa toLongLong()
+ \sa toLongLong(), numberOptions(), zeroDigit(), positiveSign()
*/
QString QLocale::toString(qlonglong i) const
{
int flags = d->m_numberOptions & OmitGroupSeparator
? 0
- : QLocaleData::ThousandsGroup;
+ : (d->m_data->m_country_id == Country::India)
+ ? QLocaleData::IndianNumberGrouping : QLocaleData::ThousandsGroup;
return d->m_data->longLongToString(i, -1, 10, -1, flags);
}
@@ -1977,14 +2008,15 @@ QString QLocale::toString(qlonglong i) const
/*!
\overload
- \sa toULongLong()
+ \sa toULongLong(), numberOptions(), zeroDigit(), positiveSign()
*/
QString QLocale::toString(qulonglong i) const
{
int flags = d->m_numberOptions & OmitGroupSeparator
? 0
- : QLocaleData::ThousandsGroup;
+ : (d->m_data->m_country_id == Country::India)
+ ? QLocaleData::IndianNumberGrouping : QLocaleData::ThousandsGroup;
return d->m_data->unsLongLongToString(i, -1, 10, -1, flags);
}
@@ -2523,6 +2555,12 @@ QDateTime QLocale::toDateTime(const QString &string, const QString &format, QCal
\since 4.1
Returns the decimal point character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa groupSeparator(), toString()
*/
QChar QLocale::decimalPoint() const
{
@@ -2533,6 +2571,12 @@ QChar QLocale::decimalPoint() const
\since 4.1
Returns the group separator character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa decimalPoint(), toString()
*/
QChar QLocale::groupSeparator() const
{
@@ -2543,6 +2587,12 @@ QChar QLocale::groupSeparator() const
\since 4.1
Returns the percent character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa toString()
*/
QChar QLocale::percent() const
{
@@ -2553,6 +2603,12 @@ QChar QLocale::percent() const
\since 4.1
Returns the zero digit character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa toString()
*/
QChar QLocale::zeroDigit() const
{
@@ -2563,6 +2619,12 @@ QChar QLocale::zeroDigit() const
\since 4.1
Returns the negative sign character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa positiveSign(), toString()
*/
QChar QLocale::negativeSign() const
{
@@ -2573,6 +2635,12 @@ QChar QLocale::negativeSign() const
\since 4.5
Returns the positive sign character of this locale.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa negativeSign(), toString()
*/
QChar QLocale::positiveSign() const
{
@@ -2582,7 +2650,14 @@ QChar QLocale::positiveSign() const
/*!
\since 4.1
- Returns the exponential character of this locale.
+ Returns the exponential character of this locale, used to separate exponent
+ from mantissa in some floating-point numeric representations.
+
+ \note This function shall change to return a QString instead of QChar in
+ Qt6. Callers are encouraged to exploit the QString(QChar) constructor to
+ convert early in preparation for this.
+
+ \sa toString(double, char, int)
*/
QChar QLocale::exponential() const
{
@@ -2607,7 +2682,7 @@ static char qToLower(char c)
\a f and \a prec have the same meaning as in QString::number(double, char, int).
- \sa toDouble()
+ \sa toDouble(), numberOptions(), exponential(), decimalPoint(), zeroDigit(), positiveSign(), percent()
*/
QString QLocale::toString(double i, char f, int prec) const
@@ -3626,10 +3701,19 @@ QT_WARNING_DISABLE_MSVC(4146)
QT_WARNING_POP
uint cnt_thousand_sep = 0;
- if (flags & ThousandsGroup && base == 10) {
- for (int i = num_str.length() - 3; i > 0; i -= 3) {
- num_str.insert(i, group);
- ++cnt_thousand_sep;
+ if (base == 10){
+ if (flags & ThousandsGroup) {
+ for (int i = num_str.length() - 3; i > 0; i -= 3) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
+ } else if (flags & IndianNumberGrouping) {
+ if (num_str.length() > 3)
+ num_str.insert(num_str.length() - 3 , group);
+ for (int i = num_str.length() - 6; i > 0; i -= 2) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
}
}
@@ -3713,10 +3797,19 @@ QString QLocaleData::unsLongLongToString(const QChar zero, const QChar group,
}
uint cnt_thousand_sep = 0;
- if (flags & ThousandsGroup && base == 10) {
- for (int i = num_str.length() - 3; i > 0; i -=3) {
- num_str.insert(i, group);
- ++cnt_thousand_sep;
+ if (base == 10) {
+ if (flags & ThousandsGroup) {
+ for (int i = num_str.length() - 3; i > 0; i -=3) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
+ } else if (flags & IndianNumberGrouping) {
+ if (num_str.length() > 3)
+ num_str.insert(num_str.length() - 3 , group);
+ for (int i = num_str.length() - 6; i > 0; i -= 2) {
+ num_str.insert(i, group);
+ ++cnt_thousand_sep;
+ }
}
}
@@ -3851,7 +3944,10 @@ bool QLocaleData::numberToCLocale(QStringView s, QLocale::NumberOptions number_o
// check distance from the last separator or from the beginning of the digits
// ### FIXME: Some locales allow other groupings!
// See https://en.wikipedia.org/wiki/Thousands_separator
- if (last_separator_idx != -1 && idx - last_separator_idx != 4)
+ if (m_country_id == QLocale::India) {
+ if (last_separator_idx != -1 && idx - last_separator_idx != 3)
+ return false;
+ } else if (last_separator_idx != -1 && idx - last_separator_idx != 4)
return false;
if (last_separator_idx == -1
&& (start_of_digits_idx == -1 || idx - start_of_digits_idx > 3)) {
@@ -4395,6 +4491,8 @@ QStringList QLocale::uiLanguages() const
for (const auto entry : qAsConst(uiLanguages))
locales.append(QLocale(entry));
}
+ if (locales.isEmpty())
+ locales.append(systemLocale()->fallbackUiLocale());
} else
#endif
{