summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorQt Forward Merge Bot <qt_forward_merge_bot@qt-project.org>2020-02-04 01:00:59 +0100
committerEdward Welbourne <edward.welbourne@qt.io>2020-02-04 10:44:00 +0100
commit97417e8f2896bfbe1d9e9a703ddba980983d1442 (patch)
treeecee79f9b126bc695904e74000c0775d1ef34646 /src/corelib/text
parent02eb9df851093f11872c828869226903c81f1b60 (diff)
parent056230cc9c3309823a93df0e34c92affb29df9e4 (diff)
Merge remote-tracking branch 'origin/5.14' into 5.15
Conflicts: .qmake.conf examples/widgets/widgets/imageviewer/imageviewer.cpp src/corelib/text/qchar.cpp src/corelib/time/qdatetime.cpp Change-Id: I9762f5c4ff650799219729d6aee79ac07ce9024a
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qlocale.cpp36
-rw-r--r--src/corelib/text/qlocale_mac.mm20
-rw-r--r--src/corelib/text/qlocale_p.h4
-rw-r--r--src/corelib/text/qlocale_win.cpp69
4 files changed, 84 insertions, 45 deletions
diff --git a/src/corelib/text/qlocale.cpp b/src/corelib/text/qlocale.cpp
index 5fc9d9922c..6e33e27276 100644
--- a/src/corelib/text/qlocale.cpp
+++ b/src/corelib/text/qlocale.cpp
@@ -660,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
@@ -711,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.
@@ -718,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()) {
@@ -737,9 +746,26 @@ static void updateSystemPrivate()
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() && !res.toString().isEmpty())
- 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() && !res.toString().isEmpty())
@@ -752,6 +778,10 @@ static void updateSystemPrivate()
res = sys_locale->query(QSystemLocale::PositiveSign, QVariant());
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,8 +864,6 @@ static const int locale_data_size = sizeof(locale_data)/sizeof(QLocaleData) - 1;
Q_GLOBAL_STATIC_WITH_ARGS(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
(QLocalePrivate::create(defaultData())))
-Q_GLOBAL_STATIC_WITH_ARGS(QExplicitlySharedDataPointer<QLocalePrivate>, systemLocalePrivate,
- (QLocalePrivate::create(systemData())))
static QLocalePrivate *localePrivateByName(const QString &name)
{
diff --git a/src/corelib/text/qlocale_mac.mm b/src/corelib/text/qlocale_mac.mm
index 31ede1352b..5381f0f975 100644
--- a/src/corelib/text/qlocale_mac.mm
+++ b/src/corelib/text/qlocale_mac.mm
@@ -119,7 +119,7 @@ static QString macDayName(int day, bool short_format)
return QString();
}
-static QString macDateToString(const QDate &date, bool short_format)
+static QString macDateToString(QDate date, bool short_format)
{
QCFType<CFDateRef> myDate = QDateTime(date, QTime()).toCFDate();
QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
@@ -131,7 +131,7 @@ static QString macDateToString(const QDate &date, bool short_format)
return QCFString(CFDateFormatterCreateStringWithDate(0, myFormatter, myDate));
}
-static QString macTimeToString(const QTime &time, bool short_format)
+static QString macTimeToString(QTime time, bool short_format)
{
QCFType<CFDateRef> myDate = QDateTime(QDate::currentDate(), time).toCFDate();
QCFType<CFLocaleRef> mylocale = CFLocaleCopyCurrent();
@@ -283,10 +283,12 @@ static QString getMacTimeFormat(CFDateFormatterStyle style)
return macToQtFormat(QString::fromCFString(CFDateFormatterGetFormat(formatter)));
}
-static QString getCFLocaleValue(CFStringRef key)
+static QVariant getCFLocaleValue(CFStringRef key)
{
QCFType<CFLocaleRef> locale = CFLocaleCopyCurrent();
CFTypeRef value = CFLocaleGetValue(locale, key);
+ if (!value)
+ return QVariant();
return QString::fromCFString(CFStringRef(static_cast<CFTypeRef>(value)));
}
@@ -411,14 +413,10 @@ QVariant QSystemLocale::query(QueryType type, QVariant in) const
switch(type) {
// case Name:
// return getMacLocaleName();
- case DecimalPoint: {
- QString value = getCFLocaleValue(kCFLocaleDecimalSeparator);
- return value.isEmpty() ? QVariant() : value;
- }
- case GroupSeparator: {
- QString value = getCFLocaleValue(kCFLocaleGroupingSeparator);
- return value.isEmpty() ? QVariant() : value;
- }
+ case DecimalPoint:
+ return getCFLocaleValue(kCFLocaleDecimalSeparator);
+ case GroupSeparator:
+ return getCFLocaleValue(kCFLocaleGroupingSeparator);
case DateFormatLong:
case DateFormatShort:
return getMacDateFormat(type == DateFormatShort
diff --git a/src/corelib/text/qlocale_p.h b/src/corelib/text/qlocale_p.h
index bb24009523..7e26e86fdf 100644
--- a/src/corelib/text/qlocale_p.h
+++ b/src/corelib/text/qlocale_p.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2019 The Qt Company Ltd.
+** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2016 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
@@ -87,7 +87,7 @@ public:
LanguageId, // uint
CountryId, // uint
DecimalPoint, // QString
- GroupSeparator, // QString
+ GroupSeparator, // QString (empty QString means: don't group digits)
ZeroDigit, // QString
NegativeSign, // QString
DateFormatLong, // QString
diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp
index 4b4152c519..4a38adf309 100644
--- a/src/corelib/text/qlocale_win.cpp
+++ b/src/corelib/text/qlocale_win.cpp
@@ -106,11 +106,11 @@ struct QSystemLocalePrivate
{
QSystemLocalePrivate();
- QString zeroDigit();
- QString decimalPoint();
- QString groupSeparator();
- QString negativeSign();
- QString positiveSign();
+ QVariant zeroDigit();
+ QVariant decimalPoint();
+ QVariant groupSeparator();
+ QVariant negativeSign();
+ QVariant positiveSign();
QVariant dateFormat(QLocale::FormatType);
QVariant timeFormat(QLocale::FormatType);
QVariant dateTimeFormat(QLocale::FormatType);
@@ -150,7 +150,9 @@ private:
QString zero; // cached value for zeroDigit()
int getLocaleInfo(LCTYPE type, LPWSTR data, int size);
- QString getLocaleInfo(LCTYPE type, int maxlen = 0);
+ // Need to distinguish empty QString packaged as (non-null) QVariant from null QVariant:
+ template <typename T = QString>
+ T getLocaleInfo(LCTYPE type, int maxlen = 0);
int getLocaleInfo_int(LCTYPE type, int maxlen = 0);
int getCurrencyFormat(DWORD flags, LPCWSTR value, const CURRENCYFMTW *format, LPWSTR data, int size);
@@ -211,19 +213,30 @@ inline int QSystemLocalePrivate::getLocaleInfo(LCTYPE type, LPWSTR data, int siz
#endif
}
-QString QSystemLocalePrivate::getLocaleInfo(LCTYPE type, int maxlen)
+template<typename T>
+T QSystemLocalePrivate::getLocaleInfo(LCTYPE type, int maxlen)
{
+ // https://docs.microsoft.com/en-us/windows/win32/intl/locale-spositivesign
+ // says empty for LOCALE_SPOSITIVESIGN means "+", although GetLocaleInfo()
+ // is documented to return 0 only on failure, so it's not clear how it
+ // returns empty to mean this; hence the two checks for it below.
+ const QString plus = QStringLiteral("+");
QVarLengthArray<wchar_t, 64> buf(maxlen ? maxlen : 64);
- if (!getLocaleInfo(type, buf.data(), buf.size()))
- return QString();
- if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
+ if (!getLocaleInfo(type, buf.data(), buf.size())) {
+ const auto lastError = GetLastError();
+ if (type == LOCALE_SPOSITIVESIGN && lastError == ERROR_SUCCESS)
+ return plus;
+ if (lastError != ERROR_INSUFFICIENT_BUFFER)
+ return {};
int cnt = getLocaleInfo(type, 0, 0);
if (cnt == 0)
- return QString();
+ return {};
buf.resize(cnt);
if (!getLocaleInfo(type, buf.data(), buf.size()))
- return QString();
+ return {};
}
+ if (type == LOCALE_SPOSITIVESIGN && !buf[0])
+ return plus;
return QString::fromWCharArray(buf.data());
}
@@ -298,7 +311,7 @@ QString &QSystemLocalePrivate::substituteDigits(QString &string)
return string;
}
-QString QSystemLocalePrivate::zeroDigit()
+QVariant QSystemLocalePrivate::zeroDigit()
{
if (zero.isEmpty()) {
/* Ten digits plus a terminator.
@@ -317,24 +330,24 @@ QString QSystemLocalePrivate::zeroDigit()
return zero;
}
-QString QSystemLocalePrivate::decimalPoint()
+QVariant QSystemLocalePrivate::decimalPoint()
{
- return getLocaleInfo(LOCALE_SDECIMAL);
+ return getLocaleInfo<QVariant>(LOCALE_SDECIMAL);
}
-QString QSystemLocalePrivate::groupSeparator()
+QVariant QSystemLocalePrivate::groupSeparator()
{
- return getLocaleInfo(LOCALE_STHOUSAND);
+ return getLocaleInfo<QVariant>(LOCALE_STHOUSAND);
}
-QString QSystemLocalePrivate::negativeSign()
+QVariant QSystemLocalePrivate::negativeSign()
{
- return getLocaleInfo(LOCALE_SNEGATIVESIGN);
+ return getLocaleInfo<QVariant>(LOCALE_SNEGATIVESIGN);
}
-QString QSystemLocalePrivate::positiveSign()
+QVariant QSystemLocalePrivate::positiveSign()
{
- return getLocaleInfo(LOCALE_SPOSITIVESIGN);
+ return getLocaleInfo<QVariant>(LOCALE_SPOSITIVESIGN);
}
QVariant QSystemLocalePrivate::dateFormat(QLocale::FormatType type)
@@ -392,10 +405,10 @@ QVariant QSystemLocalePrivate::dayName(int day, QLocale::FormatType type)
day -= 1;
if (type == QLocale::LongFormat)
- return getLocaleInfo(long_day_map[day]);
+ return getLocaleInfo<QVariant>(long_day_map[day]);
if (type == QLocale::NarrowFormat)
- return getLocaleInfo(narrow_day_map[day]);
- return getLocaleInfo(short_day_map[day]);
+ return getLocaleInfo<QVariant>(narrow_day_map[day]);
+ return getLocaleInfo<QVariant>(short_day_map[day]);
}
QVariant QSystemLocalePrivate::monthName(int month, QLocale::FormatType type)
@@ -418,7 +431,7 @@ QVariant QSystemLocalePrivate::monthName(int month, QLocale::FormatType type)
LCTYPE lctype = (type == QLocale::ShortFormat || type == QLocale::NarrowFormat)
? short_month_map[month] : long_month_map[month];
- return getLocaleInfo(lctype);
+ return getLocaleInfo<QVariant>(lctype);
}
QVariant QSystemLocalePrivate::toString(QDate date, QLocale::FormatType type)
@@ -485,7 +498,7 @@ QVariant QSystemLocalePrivate::measurementSystem()
QVariant QSystemLocalePrivate::collation()
{
- return getLocaleInfo(LOCALE_SSORTLOCALE);
+ return getLocaleInfo<QVariant>(LOCALE_SSORTLOCALE);
}
QVariant QSystemLocalePrivate::amText()
@@ -687,12 +700,12 @@ QVariant QSystemLocalePrivate::uiLanguages()
QVariant QSystemLocalePrivate::nativeLanguageName()
{
- return getLocaleInfo(LOCALE_SNATIVELANGUAGENAME);
+ return getLocaleInfo<QVariant>(LOCALE_SNATIVELANGUAGENAME);
}
QVariant QSystemLocalePrivate::nativeCountryName()
{
- return getLocaleInfo(LOCALE_SNATIVECOUNTRYNAME);
+ return getLocaleInfo<QVariant>(LOCALE_SNATIVECOUNTRYNAME);
}