qlocale_win.cpp: distinguish empty QString from null QVariant

An empty string, when packaged as a QVariant, is non-null (as a
QVariant); and QSystemLocale::query()'s callers care about the
difference.

Some callers of the internal getLocaleInfo(LCTYPE type, int maxlen)
need an actual QString return, while others are what query() returns,
so need to return a QVariant; where the former want an empty string,
the latter need a null QVariant. So make that getLocaleInfo() into a
template, so callers can chose QString or QVariant as return type,
only affecting the failure returns.

Change-Id: I7b9a698badedc0e0d8aef8c6e85c22931c33297a
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Edward Welbourne 2020-01-24 16:01:33 +01:00
parent 2d265dce58
commit e0f9b57462

View File

@ -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,18 +213,19 @@ 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)
{
QVarLengthArray<wchar_t, 64> buf(maxlen ? maxlen : 64);
if (!getLocaleInfo(type, buf.data(), buf.size())) {
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
return QString();
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 {};
}
return QString::fromWCharArray(buf.data());
}
@ -298,7 +301,7 @@ QString &QSystemLocalePrivate::substituteDigits(QString &string)
return string;
}
QString QSystemLocalePrivate::zeroDigit()
QVariant QSystemLocalePrivate::zeroDigit()
{
if (zero.isEmpty()) {
/* Ten digits plus a terminator.
@ -317,24 +320,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 +395,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 +421,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 +488,7 @@ QVariant QSystemLocalePrivate::measurementSystem()
QVariant QSystemLocalePrivate::collation()
{
return getLocaleInfo(LOCALE_SSORTLOCALE);
return getLocaleInfo<QVariant>(LOCALE_SSORTLOCALE);
}
QVariant QSystemLocalePrivate::amText()
@ -687,12 +690,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);
}