From e57e2f3e32cca2bf592a49cd62eaf567f3795c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Abecasis?= Date: Mon, 12 Mar 2012 12:58:19 +0100 Subject: Fix QString:mid and midRef, again In commit 75286739 it was assumed that negative positions shouldn't influence the size of the returned substring. That however changes behaviour that was depended on even inside Qt. With this change, the old behaviour is reestablished. A negative value of n is still taken to mean "all the way to the end", regardless of position, and overflows are still avoided. Change-Id: I7d6ed17cc5e274c7c7ddf0eb0c3238e1159ec4f6 Reviewed-by: Kent Hansen Reviewed-by: Bradley T. Hughes --- src/corelib/tools/qstring.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src/corelib/tools/qstring.cpp') diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 58eb711168..79e3577727 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -3375,9 +3375,15 @@ QString QString::mid(int position, int n) const { if (position > d->size) return QString(); - if (position < 0) + if (position < 0) { + if (n < 0 || n + position >= d->size) + return *this; + if (n + position <= 0) + return QString(); + + n += position; position = 0; - if (n < 0 || n > d->size - position) + } else if (n < 0 || n > d->size - position) n = d->size - position; if (position == 0 && n == d->size) return *this; @@ -8040,9 +8046,15 @@ QStringRef QString::midRef(int position, int n) const { if (position > d->size) return QStringRef(); - if (position < 0) + 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; - if (n < 0 || n > d->size - position) + } else if (n < 0 || n > d->size - position) n = d->size - position; return QStringRef(this, position, n); } -- cgit v1.2.3