summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2012-03-12 12:58:19 +0100
committerQt by Nokia <qt-info@nokia.com>2012-03-12 19:53:59 +0100
commite57e2f3e32cca2bf592a49cd62eaf567f3795c30 (patch)
tree4e1eb08b59b7251d74eb2bdf0598b84fe17a05e4 /src/corelib
parentd5a85940f785459d7b982d5fdf59a9fd18825092 (diff)
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 <kent.hansen@nokia.com> Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qstring.cpp20
1 files changed, 16 insertions, 4 deletions
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);
}