From 601d68584921a47d83d833228f5ec698e13b624c Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 23 Jan 2012 12:02:36 +0000 Subject: Make mid() and midRef() properly return empty, non-null objects If we request a substring starting at the very end of the string, QString::mid should return an empty string, not a null string. For instance, QString("abc").mid(3, 0) used to return a null one, while this patch makes it return an empty one. The same thing applies to QString::midRef() and QByteArray::mid(). Change-Id: Ie9efd7a0622d429efd0fb682c19856c19e9469af Reviewed-by: Lars Knoll --- src/corelib/tools/qbytearray.cpp | 2 +- src/corelib/tools/qstring.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 0ca2f47923..364f58f42c 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -2660,7 +2660,7 @@ QByteArray QByteArray::right(int len) const QByteArray QByteArray::mid(int pos, int len) const { - if (d == &shared_null.ba || d == &shared_empty.ba || pos >= d->size) + if (d == &shared_null.ba || d == &shared_empty.ba || pos > d->size) return QByteArray(); if (len < 0) len = d->size - pos; diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 3e92df7299..d6090cc7b2 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -3385,7 +3385,7 @@ QString QString::right(int n) const QString QString::mid(int position, int n) const { - if (d == &shared_null.str || position >= d->size) + if (d == &shared_null.str || position > d->size) return QString(); if (n < 0) n = d->size - position; @@ -8024,7 +8024,7 @@ QStringRef QString::rightRef(int n) const Returns a substring reference to \a n characters of this string, starting at the specified \a position. - If the \a position exceeds the length of the string, an empty + If the \a position exceeds the length of the string, a null reference is returned. If there are less than \a n characters available in the string, @@ -8041,7 +8041,7 @@ QStringRef QString::rightRef(int n) const QStringRef QString::midRef(int position, int n) const { - if (d == &shared_null.str || position >= d->size) + if (d == &shared_null.str || position > d->size) return QStringRef(); if (n < 0) n = d->size - position; -- cgit v1.2.3