diff options
author | Jędrzej Nowacki <jedrzej.nowacki@digia.com> | 2014-02-14 16:32:31 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2014-05-16 09:51:38 +0200 |
commit | 71fb3633e8d909e9a91e1bee6eaf53c146f25998 (patch) | |
tree | 93995d7c6965b3cc944ed461dfcb4c5bbf5ac6f1 /src/corelib/tools/qstring.cpp | |
parent | 8f3a393e412fccd6606af5ef8d909da6435e16e2 (diff) |
Unify all mid() functions in QtBase.
Up to now, Qt had at least 3 different implementations of the mid().
Only QString::mid implementation was not crashing on edge cases and
was protected against overflows, therefore I picked that one as the
base implementation, even if it has weird semantics for an invalid
input.
As a side effect QVector::mid was slightly optimized to not detach in
all cases (which follows current QList behavior). Documentation of
QVector::mid and QList::mid was updated to not mention "copy of data"
which could suggest that the mid() result is detached.
QStringRef::mid was fixed and now it follows general Qt behavior, by
returning a null value for a null input.
Change-Id: Ie9ff5d98372bd193d66508e6dd92b6ed1180ad9b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index aac9c493c3..541a853487 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -4090,21 +4090,22 @@ QString QString::right(int n) const QString QString::mid(int position, int n) const { - if (position > d->size) + using namespace QtPrivate; + switch (QContainerImplHelper::mid(d->size, &position, &n)) { + case QContainerImplHelper::Null: return QString(); - if (position < 0) { - if (n < 0 || n + position >= d->size) - return *this; - if (n + position <= 0) - return QString(); - - n += position; - position = 0; - } else if (uint(n) > uint(d->size - position)) - n = d->size - position; - if (position == 0 && n == d->size) + case QContainerImplHelper::Empty: + { + QStringDataPtr empty = { Data::allocate(0) }; + return QString(empty); + } + case QContainerImplHelper::Full: return *this; - return QString((const QChar*) d->data() + position, n); + case QContainerImplHelper::Subset: + return QString((const QChar*)d->data() + position, n); + } + Q_UNREACHABLE(); + return QString(); } /*! @@ -8794,19 +8795,19 @@ QStringRef QString::rightRef(int n) const */ QStringRef QStringRef::mid(int pos, int n) const { - if (pos > m_size) + using namespace QtPrivate; + switch (QContainerImplHelper::mid(m_size, &pos, &n)) { + case QContainerImplHelper::Null: return QStringRef(); - if (pos < 0) { - if (n < 0 || n + pos >= m_size) - return QStringRef(m_string, m_position, m_size); - if (n + pos <= 0) - return QStringRef(); - n += pos; - pos = 0; - } else if (uint(n) > uint(m_size - pos)) { - n = m_size - pos; + case QContainerImplHelper::Empty: + return QStringRef(m_string, 0, 0); + case QContainerImplHelper::Full: + return *this; + case QContainerImplHelper::Subset: + return QStringRef(m_string, pos + m_position, n); } - return QStringRef(m_string, pos + m_position, n); + Q_UNREACHABLE(); + return QStringRef(); } /*! @@ -8831,19 +8832,19 @@ QStringRef QStringRef::mid(int pos, int n) const */ QStringRef QString::midRef(int position, int n) const { - if (position > d->size) + using namespace QtPrivate; + switch (QContainerImplHelper::mid(d->size, &position, &n)) { + case QContainerImplHelper::Null: return QStringRef(); - if (position < 0) { - if (n < 0 || n + position >= d->size) - return QStringRef(this, 0, d->size); - if (n + position <= 0) - return QStringRef(); - - n += position; - position = 0; - } else if (uint(n) > uint(d->size - position)) - n = d->size - position; - return QStringRef(this, position, n); + case QContainerImplHelper::Empty: + return QStringRef(this, 0, 0); + case QContainerImplHelper::Full: + return QStringRef(this, 0, d->size); + case QContainerImplHelper::Subset: + return QStringRef(this, position, n); + } + Q_UNREACHABLE(); + return QStringRef(); } /*! |