Remove redundant checks in some tools classes

This is a simple optimisation allowed to us by the fact that all platforms
we run on use two's complement for the signed integers. The trick works as
long as one of the two signed integers is known beforehand to be
non-negative:

 - by definition, for any signed integer i, i <= INT_MAX
 - by definition, for any unsigned integer u, u >= 0
 - given a signed integer x >= 0, 0U <= uint(x) <= uint(INT_MAX)
 - therefore, given another signed integer y of whatever
   value, uint(x) < uint(y) ←→ x < y && y >= 0

The trick is an optimisation because the compiler doesn't know that one of
the two sides is always non-negative. Otherwise, it would do the
same optimisation.

Change-Id: If256ec0df4e06335805af8010bb67ce5fd3e065a
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Jędrzej Nowacki 2013-09-11 15:14:07 +00:00 committed by The Qt Project
parent a26f47756b
commit 5fd344389e
4 changed files with 23 additions and 23 deletions

View File

@ -1813,7 +1813,7 @@ QByteArray &QByteArray::insert(int i, char ch)
QByteArray &QByteArray::remove(int pos, int len)
{
if (len <= 0 || pos >= d->size || pos < 0)
if (len <= 0 || uint(pos) >= uint(d->size))
return *this;
detach();
if (pos + len >= d->size) {

View File

@ -1680,7 +1680,7 @@ QString &QString::remove(int pos, int len)
{
if (pos < 0) // count from end of string
pos += d->size;
if (pos < 0 || pos >= d->size) {
if (uint(pos) >= uint(d->size)) {
// range problems
} else if (len >= d->size - pos) {
resize(pos); // truncate
@ -1804,7 +1804,7 @@ QString &QString::replace(int pos, int len, const QString &after)
*/
QString &QString::replace(int pos, int len, const QChar *unicode, int size)
{
if (pos < 0 || pos > d->size)
if (uint(pos) > uint(d->size))
return *this;
if (len > d->size - pos)
len = d->size - pos;
@ -2691,7 +2691,7 @@ int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) c
int delta = l-sl;
if (from == l && sl == 0)
return from;
if (from < 0 || from >= l || delta < 0)
if (uint(from) >= uint(l) || delta < 0)
return -1;
if (from > delta)
from = delta;
@ -2730,7 +2730,7 @@ int QString::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) co
int delta = l-sl;
if (from == l && sl == 0)
return from;
if (from < 0 || from >= l || delta < 0)
if (uint(from) >= uint(l) || delta < 0)
return -1;
if (from > delta)
from = delta;
@ -2780,7 +2780,7 @@ int QString::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs
int delta = l - sl;
if (from == l && sl == 0)
return from;
if (from < 0 || from >= l || delta < 0)
if (uint(from) >= uint(l) || delta < 0)
return -1;
if (from > delta)
from = delta;
@ -3696,7 +3696,7 @@ QString QString::section(const QRegularExpression &re, int start, int end, Secti
*/
QString QString::left(int n) const
{
if (n >= d->size || n < 0)
if (uint(n) >= uint(d->size))
return *this;
return QString((const QChar*) d->data(), n);
}
@ -3714,7 +3714,7 @@ QString QString::left(int n) const
*/
QString QString::right(int n) const
{
if (n >= d->size || n < 0)
if (uint(n) >= uint(d->size))
return *this;
return QString((const QChar*) d->data() + d->size - n, n);
}
@ -3748,7 +3748,7 @@ QString QString::mid(int position, int n) const
n += position;
position = 0;
} else if (n < 0 || n > d->size - position)
} else if (uint(n) > uint(d->size - position))
n = d->size - position;
if (position == 0 && n == d->size)
return *this;
@ -8490,7 +8490,7 @@ QString &QString::append(const QStringRef &str)
*/
QStringRef QStringRef::left(int n) const
{
if (n >= m_size || n < 0)
if (uint(n) >= uint(m_size))
return *this;
return QStringRef(m_string, m_position, n);
}
@ -8510,7 +8510,7 @@ QStringRef QStringRef::left(int n) const
*/
QStringRef QString::leftRef(int n) const
{
if (n >= d->size || n < 0)
if (uint(n) >= uint(d->size))
n = d->size;
return QStringRef(this, 0, n);
}
@ -8529,7 +8529,7 @@ QStringRef QString::leftRef(int n) const
*/
QStringRef QStringRef::right(int n) const
{
if (n >= m_size || n < 0)
if (uint(n) >= uint(m_size))
return *this;
return QStringRef(m_string, n + m_position, m_size - n);
}
@ -8549,7 +8549,7 @@ QStringRef QStringRef::right(int n) const
*/
QStringRef QString::rightRef(int n) const
{
if (n >= d->size || n < 0)
if (uint(n) >= uint(d->size))
n = d->size;
return QStringRef(this, d->size - n, n);
}
@ -8582,7 +8582,7 @@ QStringRef QStringRef::mid(int pos, int n) const
return QStringRef();
n += pos;
pos = 0;
} else if (n < 0 || n > m_size - pos) {
} else if (uint(n) > uint(m_size - pos)) {
n = m_size - pos;
}
return QStringRef(m_string, pos + m_position, n);
@ -8620,7 +8620,7 @@ QStringRef QString::midRef(int position, int n) const
n += position;
position = 0;
} else if (n < 0 || n > d->size - position)
} else if (uint(n) > uint(d->size - position))
n = d->size - position;
return QStringRef(this, position, n);
}
@ -8725,7 +8725,7 @@ int QStringRef::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs
int delta = l - sl;
if (from == l && sl == 0)
return from;
if (from < 0 || from >= l || delta < 0)
if (uint(from) >= uint(l) || delta < 0)
return -1;
if (from > delta)
from = delta;
@ -8775,7 +8775,7 @@ int QStringRef::lastIndexOf(QLatin1String str, int from, Qt::CaseSensitivity cs)
int delta = l - sl;
if (from == l && sl == 0)
return from;
if (from < 0 || from >= l || delta < 0)
if (uint(from) >= uint(l) || delta < 0)
return -1;
if (from > delta)
from = delta;
@ -8814,7 +8814,7 @@ int QStringRef::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity
int delta = l - sl;
if (from == l && sl == 0)
return from;
if (from < 0 || from >= l || delta < 0)
if (uint(from) >= uint(l) || delta < 0)
return -1;
if (from > delta)
from = delta;
@ -9055,7 +9055,7 @@ static inline int qt_last_index_of(const QChar *haystack, int haystackLen, QChar
ushort c = needle.unicode();
if (from < 0)
from += haystackLen;
if (from < 0 || from >= haystackLen)
if (uint(from) >= uint(haystackLen))
return -1;
if (from >= 0) {
const ushort *b = reinterpret_cast<const ushort*>(haystack);

View File

@ -318,7 +318,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a
template <class T, int Prealloc>
Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
{
if (i < 0 || i >= size()) {
if (uint(i) >= uint(size())) {
return T();
}
return at(i);
@ -326,7 +326,7 @@ Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
template <class T, int Prealloc>
Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const
{
return (i < 0 || i >= size()) ? defaultValue : at(i);
return (uint(i) >= uint(size())) ? defaultValue : at(i);
}
template <class T, int Prealloc>

View File

@ -538,7 +538,7 @@ void QVector<T>::reallocData(const int asize, const int aalloc, QArrayData::Allo
template<typename T>
Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
{
if (i < 0 || i >= d->size) {
if (uint(i) >= uint(d->size)) {
return T();
}
return d->begin()[i];
@ -546,7 +546,7 @@ Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const
template<typename T>
Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const
{
return ((i < 0 || i >= d->size) ? defaultValue : d->begin()[i]);
return uint(i) >= uint(d->size) ? defaultValue : d->begin()[i];
}
template <typename T>