From 5fd344389e163f1833f3d58c3cc994ac74063440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Wed, 11 Sep 2013 15:14:07 +0000 Subject: Remove redundant checks in some tools classes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/corelib/tools/qbytearray.cpp | 2 +- src/corelib/tools/qstring.cpp | 36 ++++++++++++++++++------------------ src/corelib/tools/qvarlengtharray.h | 4 ++-- src/corelib/tools/qvector.h | 4 ++-- 4 files changed, 23 insertions(+), 23 deletions(-) (limited to 'src/corelib') diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index a2d3891f00..68fb3df618 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -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) { diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 48d96e0ed0..b909e9e09b 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -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(haystack); diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 34a64dafe2..309d2a2dca 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -318,7 +318,7 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray::realloc(int asize, int a template Q_OUTOFLINE_TEMPLATE T QVarLengthArray::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::value(int i) const template Q_OUTOFLINE_TEMPLATE T QVarLengthArray::value(int i, const T &defaultValue) const { - return (i < 0 || i >= size()) ? defaultValue : at(i); + return (uint(i) >= uint(size())) ? defaultValue : at(i); } template diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index caa291b3d9..3d86440fcd 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -538,7 +538,7 @@ void QVector::reallocData(const int asize, const int aalloc, QArrayData::Allo template Q_OUTOFLINE_TEMPLATE T QVector::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::value(int i) const template Q_OUTOFLINE_TEMPLATE T QVector::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 -- cgit v1.2.3