Use qsizetype instead of int in QByteArray's helper methods

This is needed as a preperation for introducing QByteArrayView, which
will use qsizetype instead of int for size. Since these methods will be
reused by QByteArrayView, they need to use qsizetype.

Change-Id: Ia2d94ec70742d4f9326de9548fd7534d56d3a5ac
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Sona Kurazyan 2020-06-09 12:21:07 +02:00
parent 30ad4cf558
commit 08699a8273
2 changed files with 34 additions and 33 deletions

View File

@ -78,9 +78,9 @@ static constexpr inline uchar asciiLower(uchar c)
return c >= 'A' && c <= 'Z' ? c | 0x20 : c;
}
int qFindByteArray(
const char *haystack0, int haystackLen, int from,
const char *needle0, int needleLen);
qsizetype qFindByteArray(
const char *haystack0, qsizetype haystackLen, qsizetype from,
const char *needle0, qsizetype needleLen);
/*****************************************************************************
Safe and portable C string functions; extensions to standard string.h
@ -2463,7 +2463,7 @@ QByteArray QByteArray::repeated(int times) const
}
#define REHASH(a) \
if (ol_minus_1 < sizeof(uint) * CHAR_BIT) \
if (ol_minus_1 < sizeof(std::size_t) * CHAR_BIT) \
hashHaystack -= (a) << ol_minus_1; \
hashHaystack <<= 1
@ -2490,7 +2490,7 @@ int QByteArray::indexOf(const QByteArray &ba, int from) const
if (from > l || ol + from > l)
return -1;
return qFindByteArray(data(), size(), from, ba.data(), ol);
return static_cast<int>(qFindByteArray(data(), size(), from, ba.data(), ol));
}
/*! \fn int QByteArray::indexOf(const QString &str, int from) const
@ -2527,7 +2527,7 @@ int QByteArray::indexOf(const char *c, int from) const
if (ol == 0)
return from;
return qFindByteArray(data(), size(), from, c, ol);
return static_cast<int>(qFindByteArray(data(), size(), from, c, ol));
}
/*!
@ -2557,9 +2557,10 @@ int QByteArray::indexOf(char ch, int from) const
return -1;
}
static int lastIndexOfHelper(const char *haystack, int l, const char *needle, int ol, int from)
static qsizetype lastIndexOfHelper(const char *haystack, qsizetype l, const char *needle,
qsizetype ol, qsizetype from)
{
int delta = l - ol;
auto delta = l - ol;
if (from < 0)
from = delta;
if (from < 0 || from > l)
@ -2569,11 +2570,11 @@ static int lastIndexOfHelper(const char *haystack, int l, const char *needle, in
const char *end = haystack;
haystack += from;
const uint ol_minus_1 = ol - 1;
const auto ol_minus_1 = std::size_t(ol - 1);
const char *n = needle + ol_minus_1;
const char *h = haystack + ol_minus_1;
uint hashNeedle = 0, hashHaystack = 0;
int idx;
std::size_t hashNeedle = 0, hashHaystack = 0;
qsizetype idx;
for (idx = 0; idx < ol; ++idx) {
hashNeedle = ((hashNeedle<<1) + *(n-idx));
hashHaystack = ((hashHaystack<<1) + *(h-idx));

View File

@ -43,26 +43,26 @@
QT_BEGIN_NAMESPACE
static inline void bm_init_skiptable(const uchar *cc, int len, uchar *skiptable)
static inline void bm_init_skiptable(const uchar *cc, qsizetype len, uchar *skiptable)
{
int l = qMin(len, 255);
int l = int(qMin(len, qsizetype(255)));
memset(skiptable, l, 256*sizeof(uchar));
cc += len - l;
while (l--)
skiptable[*cc++] = l;
}
static inline int bm_find(const uchar *cc, int l, int index, const uchar *puc, uint pl,
const uchar *skiptable)
static inline qsizetype bm_find(const uchar *cc, qsizetype l, qsizetype index, const uchar *puc,
qsizetype pl, const uchar *skiptable)
{
if (pl == 0)
return index > l ? -1 : index;
const uint pl_minus_one = pl - 1;
const qsizetype pl_minus_one = pl - 1;
const uchar *current = cc + index + pl_minus_one;
const uchar *end = cc + l;
while (current < end) {
uint skip = skiptable[*current];
qsizetype skip = skiptable[*current];
if (!skip) {
// possible match
while (skip < pl) {
@ -229,12 +229,12 @@ int QByteArrayMatcher::indexIn(const char *str, int len, int from) const
*/
static int findChar(const char *str, int len, char ch, int from)
static qsizetype findChar(const char *str, qsizetype len, char ch, qsizetype from)
{
const uchar *s = (const uchar *)str;
uchar c = (uchar)ch;
if (from < 0)
from = qMax(from + len, 0);
from = qMax(from + len, qsizetype(0));
if (from < len) {
const uchar *n = s + from - 1;
const uchar *e = s + len;
@ -248,9 +248,9 @@ static int findChar(const char *str, int len, char ch, int from)
/*!
\internal
*/
static int qFindByteArrayBoyerMoore(
const char *haystack, int haystackLen, int haystackOffset,
const char *needle, int needleLen)
static qsizetype qFindByteArrayBoyerMoore(
const char *haystack, qsizetype haystackLen, qsizetype haystackOffset,
const char *needle, qsizetype needleLen)
{
uchar skiptable[256];
bm_init_skiptable((const uchar *)needle, needleLen, skiptable);
@ -261,22 +261,22 @@ static int qFindByteArrayBoyerMoore(
}
#define REHASH(a) \
if (sl_minus_1 < sizeof(uint) * CHAR_BIT) \
hashHaystack -= uint(a) << sl_minus_1; \
if (sl_minus_1 < sizeof(std::size_t) * CHAR_BIT) \
hashHaystack -= std::size_t(a) << sl_minus_1; \
hashHaystack <<= 1
/*!
\internal
*/
int qFindByteArray(
const char *haystack0, int haystackLen, int from,
const char *needle, int needleLen)
qsizetype qFindByteArray(
const char *haystack0, qsizetype haystackLen, qsizetype from,
const char *needle, qsizetype needleLen)
{
const int l = haystackLen;
const int sl = needleLen;
const auto l = haystackLen;
const auto sl = needleLen;
if (from < 0)
from += l;
if (uint(sl + from) > (uint)l)
if (std::size_t(sl + from) > std::size_t(l))
return -1;
if (!sl)
return from;
@ -302,9 +302,9 @@ int qFindByteArray(
*/
const char *haystack = haystack0 + from;
const char *end = haystack0 + (l - sl);
const uint sl_minus_1 = sl - 1;
uint hashNeedle = 0, hashHaystack = 0;
int idx;
const auto sl_minus_1 = std::size_t(sl - 1);
std::size_t hashNeedle = 0, hashHaystack = 0;
qsizetype idx;
for (idx = 0; idx < sl; ++idx) {
hashNeedle = ((hashNeedle<<1) + needle[idx]);
hashHaystack = ((hashHaystack<<1) + haystack[idx]);