QLocale: port locale_data indexing to qsizetype

Not a bug, just porting to avoid the next reader having to wonder
whether the ints and uints are 64-bit safe.

As a drive-by, make a static variable constexpr and replace sizeof
foo/sizeof *foo with q20::ssize(foo).

Task-number: QTBUG-103531
Change-Id: Iccc5a5896ab87981f4535820cea7f274e568f325
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit 013574a5fdb7538acad1a9a5d8449efe62d868e9)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-07-20 14:23:03 +02:00 committed by Qt Cherry-pick Bot
parent de46f4aed2
commit c8218521a5
2 changed files with 17 additions and 15 deletions

View File

@ -46,6 +46,8 @@ QT_WARNING_DISABLE_GCC("-Wfree-nonheap-object") // false positive tracking
#include "private/qgregoriancalendar_p.h" #include "private/qgregoriancalendar_p.h"
#include "qcalendar.h" #include "qcalendar.h"
#include <q20iterator.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
QT_IMPL_METATYPE_EXTERN_TAGGED(QList<Qt::DayOfWeek>, QList_Qt__DayOfWeek) QT_IMPL_METATYPE_EXTERN_TAGGED(QList<Qt::DayOfWeek>, QList_Qt__DayOfWeek)
@ -461,9 +463,9 @@ QByteArray QLocalePrivate::bcp47Name(char separator) const
return m_data->id().withLikelySubtagsRemoved().name(separator); return m_data->id().withLikelySubtagsRemoved().name(separator);
} }
static int findLocaleIndexById(const QLocaleId &localeId) static qsizetype findLocaleIndexById(const QLocaleId &localeId)
{ {
quint16 idx = locale_index[localeId.language_id]; qsizetype idx = locale_index[localeId.language_id];
// If there are no locales for specified language (so we we've got the // If there are no locales for specified language (so we we've got the
// default language, which has no associated script or country), give up: // default language, which has no associated script or country), give up:
if (localeId.language_id && idx == 0) if (localeId.language_id && idx == 0)
@ -480,14 +482,14 @@ static int findLocaleIndexById(const QLocaleId &localeId)
return -1; return -1;
} }
int QLocaleData::findLocaleIndex(QLocaleId lid) qsizetype QLocaleData::findLocaleIndex(QLocaleId lid)
{ {
QLocaleId localeId = lid; QLocaleId localeId = lid;
QLocaleId likelyId = localeId.withLikelySubtagsAdded(); QLocaleId likelyId = localeId.withLikelySubtagsAdded();
const ushort fallback = likelyId.language_id; const ushort fallback = likelyId.language_id;
// Try a straight match with the likely data: // Try a straight match with the likely data:
int index = findLocaleIndexById(likelyId); qsizetype index = findLocaleIndexById(likelyId);
if (index >= 0) if (index >= 0)
return index; return index;
QVarLengthArray<QLocaleId, 6> tried; QVarLengthArray<QLocaleId, 6> tried;
@ -796,7 +798,7 @@ static const QLocaleData *defaultData()
return default_data; return default_data;
} }
static uint defaultIndex() static qsizetype defaultIndex()
{ {
const QLocaleData *const data = defaultData(); const QLocaleData *const data = defaultData();
#ifndef QT_NO_SYSTEMLOCALE #ifndef QT_NO_SYSTEMLOCALE
@ -834,7 +836,7 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l)
} }
#endif // QT_NO_DATASTREAM #endif // QT_NO_DATASTREAM
static const int locale_data_size = sizeof(locale_data)/sizeof(QLocaleData) - 1; static constexpr qsizetype locale_data_size = q20::ssize(locale_data) - 1; // trailing guard
Q_CONSTINIT QBasicAtomicInt QLocalePrivate::s_generation = Q_BASIC_ATOMIC_INITIALIZER(0); Q_CONSTINIT QBasicAtomicInt QLocalePrivate::s_generation = Q_BASIC_ATOMIC_INITIALIZER(0);
Q_GLOBAL_STATIC(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate, Q_GLOBAL_STATIC(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
@ -844,8 +846,8 @@ static QLocalePrivate *localePrivateByName(QStringView name)
{ {
if (name == u"C") if (name == u"C")
return c_private(); return c_private();
const int index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name)); const qsizetype index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name));
Q_ASSERT(index >= 0 && size_t(index) < std::size(locale_data) - 1); 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 locale_data[index].m_language_id == QLocale::C
? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions); ? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions);
@ -857,8 +859,8 @@ static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Sc
if (language == QLocale::C) if (language == QLocale::C)
return c_private(); return c_private();
int index = QLocaleData::findLocaleIndex(QLocaleId { language, script, territory }); qsizetype index = QLocaleData::findLocaleIndex(QLocaleId { language, script, territory });
Q_ASSERT(index >= 0 && size_t(index) < std::size(locale_data) - 1); Q_ASSERT(index >= 0 && index < locale_data_size);
const QLocaleData *data = locale_data + index; const QLocaleData *data = locale_data + index;
QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions; QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions;

View File

@ -101,7 +101,7 @@ public:
virtual QVariant query(QueryType type, QVariant in = QVariant()) const; virtual QVariant query(QueryType type, QVariant in = QVariant()) const;
virtual QLocale fallbackLocale() const; virtual QLocale fallbackLocale() const;
inline uint fallbackLocaleIndex() const; inline qsizetype fallbackLocaleIndex() const;
private: private:
QSystemLocale(bool); QSystemLocale(bool);
friend class QSystemLocaleSingleton; friend class QSystemLocaleSingleton;
@ -161,7 +161,7 @@ struct QLocaleData
public: public:
// Having an index for each locale enables us to have diverse sources of // Having an index for each locale enables us to have diverse sources of
// data, e.g. calendar locales, as well as the main CLDR-derived data. // data, e.g. calendar locales, as well as the main CLDR-derived data.
[[nodiscard]] static int findLocaleIndex(QLocaleId localeId); [[nodiscard]] static qsizetype findLocaleIndex(QLocaleId localeId);
[[nodiscard]] static const QLocaleData *c(); [[nodiscard]] static const QLocaleData *c();
enum DoubleForm { enum DoubleForm {
@ -372,7 +372,7 @@ public:
class QLocalePrivate class QLocalePrivate
{ {
public: public:
constexpr QLocalePrivate(const QLocaleData *data, const uint index, constexpr QLocalePrivate(const QLocaleData *data, qsizetype index,
QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions, QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions,
int refs = 0) int refs = 0)
: m_data(data), ref Q_BASIC_ATOMIC_INITIALIZER(refs), : m_data(data), ref Q_BASIC_ATOMIC_INITIALIZER(refs),
@ -410,14 +410,14 @@ public:
// System locale has an m_data all its own; all others have m_data = locale_data + m_index // System locale has an m_data all its own; all others have m_data = locale_data + m_index
const QLocaleData *const m_data; const QLocaleData *const m_data;
QBasicAtomicInt ref; QBasicAtomicInt ref;
const uint m_index; const qsizetype m_index;
QLocale::NumberOptions m_numberOptions; QLocale::NumberOptions m_numberOptions;
static QBasicAtomicInt s_generation; static QBasicAtomicInt s_generation;
}; };
#ifndef QT_NO_SYSTEMLOCALE #ifndef QT_NO_SYSTEMLOCALE
uint QSystemLocale::fallbackLocaleIndex() const { return fallbackLocale().d->m_index; } qsizetype QSystemLocale::fallbackLocaleIndex() const { return fallbackLocale().d->m_index; }
#endif #endif
template <> template <>