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).

Pick-to: 6.4
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>
This commit is contained in:
Marc Mutz 2022-07-20 14:23:03 +02:00
parent 62aec32dfa
commit 013574a5fd
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 "qcalendar.h"
#include <q20iterator.h>
QT_BEGIN_NAMESPACE
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);
}
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
// default language, which has no associated script or country), give up:
if (localeId.language_id && idx == 0)
@ -480,14 +482,14 @@ static int findLocaleIndexById(const QLocaleId &localeId)
return -1;
}
int QLocaleData::findLocaleIndex(QLocaleId lid)
qsizetype QLocaleData::findLocaleIndex(QLocaleId lid)
{
QLocaleId localeId = lid;
QLocaleId likelyId = localeId.withLikelySubtagsAdded();
const ushort fallback = likelyId.language_id;
// Try a straight match with the likely data:
int index = findLocaleIndexById(likelyId);
qsizetype index = findLocaleIndexById(likelyId);
if (index >= 0)
return index;
QVarLengthArray<QLocaleId, 6> tried;
@ -795,7 +797,7 @@ static const QLocaleData *defaultData()
return default_data;
}
static uint defaultIndex()
static qsizetype defaultIndex()
{
const QLocaleData *const data = defaultData();
#ifndef QT_NO_SYSTEMLOCALE
@ -833,7 +835,7 @@ QDataStream &operator>>(QDataStream &ds, QLocale &l)
}
#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_GLOBAL_STATIC(QSharedDataPointer<QLocalePrivate>, defaultLocalePrivate,
@ -843,8 +845,8 @@ static QLocalePrivate *localePrivateByName(QStringView name)
{
if (name == u"C")
return c_private();
const int index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name));
Q_ASSERT(index >= 0 && size_t(index) < std::size(locale_data) - 1);
const qsizetype index = QLocaleData::findLocaleIndex(QLocaleId::fromName(name));
Q_ASSERT(index >= 0 && index < locale_data_size);
return new QLocalePrivate(locale_data + index, index,
locale_data[index].m_language_id == QLocale::C
? QLocale::OmitGroupSeparator : QLocale::DefaultNumberOptions);
@ -856,8 +858,8 @@ static QLocalePrivate *findLocalePrivate(QLocale::Language language, QLocale::Sc
if (language == QLocale::C)
return c_private();
int index = QLocaleData::findLocaleIndex(QLocaleId { language, script, territory });
Q_ASSERT(index >= 0 && size_t(index) < std::size(locale_data) - 1);
qsizetype index = QLocaleData::findLocaleIndex(QLocaleId { language, script, territory });
Q_ASSERT(index >= 0 && index < locale_data_size);
const QLocaleData *data = locale_data + index;
QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions;

View File

@ -101,7 +101,7 @@ public:
virtual QVariant query(QueryType type, QVariant in = QVariant()) const;
virtual QLocale fallbackLocale() const;
inline uint fallbackLocaleIndex() const;
inline qsizetype fallbackLocaleIndex() const;
private:
QSystemLocale(bool);
friend class QSystemLocaleSingleton;
@ -161,7 +161,7 @@ struct QLocaleData
public:
// 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.
[[nodiscard]] static int findLocaleIndex(QLocaleId localeId);
[[nodiscard]] static qsizetype findLocaleIndex(QLocaleId localeId);
[[nodiscard]] static const QLocaleData *c();
enum DoubleForm {
@ -372,7 +372,7 @@ public:
class QLocalePrivate
{
public:
constexpr QLocalePrivate(const QLocaleData *data, const uint index,
constexpr QLocalePrivate(const QLocaleData *data, qsizetype index,
QLocale::NumberOptions numberOptions = QLocale::DefaultNumberOptions,
int refs = 0)
: 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
const QLocaleData *const m_data;
QBasicAtomicInt ref;
const uint m_index;
const qsizetype m_index;
QLocale::NumberOptions m_numberOptions;
static QBasicAtomicInt s_generation;
};
#ifndef QT_NO_SYSTEMLOCALE
uint QSystemLocale::fallbackLocaleIndex() const { return fallbackLocale().d->m_index; }
qsizetype QSystemLocale::fallbackLocaleIndex() const { return fallbackLocale().d->m_index; }
#endif
template <>