diff options
author | Giuseppe D'Angelo <dangelog@gmail.com> | 2012-01-23 12:02:36 +0000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-24 17:36:11 +0100 |
commit | 601d68584921a47d83d833228f5ec698e13b624c (patch) | |
tree | 235d066cefcb5a5f2a7d4029c9b5512fdb17b500 | |
parent | b39df8bf92a530783144dbcf5cae939742ff2d23 (diff) |
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 <lars.knoll@nokia.com>
-rw-r--r-- | dist/changes-5.0.0 | 5 | ||||
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 2 | ||||
-rw-r--r-- | src/corelib/tools/qstring.cpp | 6 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qstring/tst_qstring.cpp | 8 | ||||
-rw-r--r-- | tests/auto/other/collections/tst_collections.cpp | 8 |
5 files changed, 21 insertions, 8 deletions
diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index 1b67f878be..bbd1c2300d 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -221,6 +221,11 @@ QtCore altering the watchlist in both the singular and QStringList overloads of addPath and removePath. +* QString::mid, QString::midRef and QByteArray::mid, if the position passed + is equal to the length (that is, right after the last character/byte), + now return an empty QString, QStringRef or QByteArray respectively. + in Qt 4 they returned a null QString or a null QStringRef. + QtGui ----- * Accessibility has been refactored. The hierachy of accessible objects is implemented via 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; diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index eda34b0d99..b9951c1b5d 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -1417,6 +1417,10 @@ void tst_QString::mid() QCOMPARE(a.mid(3,3),(QString)"DEF"); QCOMPARE(a.mid(0,0),(QString)""); + QVERIFY(!a.mid(15,0).isNull()); + QVERIFY(a.mid(15,0).isEmpty()); + QVERIFY(!a.mid(15,1).isNull()); + QVERIFY(a.mid(15,1).isEmpty()); QVERIFY(a.mid(9999).isNull()); QVERIFY(a.mid(9999,1).isNull()); @@ -1439,6 +1443,10 @@ void tst_QString::midRef() QCOMPARE(a.midRef(3,3).toString(),(QString)"DEF"); QCOMPARE(a.midRef(0,0).toString(),(QString)""); + QVERIFY(!a.midRef(15,0).toString().isNull()); + QVERIFY(a.midRef(15,0).toString().isEmpty()); + QVERIFY(!a.midRef(15,1).toString().isNull()); + QVERIFY(a.midRef(15,1).toString().isEmpty()); QVERIFY(a.midRef(9999).toString().isEmpty()); QVERIFY(a.midRef(9999,1).toString().isEmpty()); diff --git a/tests/auto/other/collections/tst_collections.cpp b/tests/auto/other/collections/tst_collections.cpp index 4edb9d2f3a..528d3b1737 100644 --- a/tests/auto/other/collections/tst_collections.cpp +++ b/tests/auto/other/collections/tst_collections.cpp @@ -1310,8 +1310,8 @@ void tst_Collections::byteArray() QVERIFY(hello.mid(0, hello.size()+1) == hello); QVERIFY(hello.mid(hello.size()-0) == ""); - - QVERIFY(hello.mid(hello.size()-0).isNull()); // weird but valid 3.x semantics + QVERIFY(hello.mid(hello.size()-0).isEmpty()); + QVERIFY(!hello.mid(hello.size()-0).isNull()); QVERIFY(hello.mid(hello.size()-1) == "o"); QVERIFY(hello.mid(hello.size()-2) == "lo"); QVERIFY(hello.mid(hello.size()-200) == "hello"); @@ -2030,8 +2030,8 @@ void tst_Collections::qstring() QVERIFY(hello.mid(0, hello.size()+1) == hello); QVERIFY(hello.mid(hello.size()-0) == ""); - - QVERIFY(hello.mid(hello.size()-0).isNull()); + QVERIFY(hello.mid(hello.size()-0).isEmpty()); + QVERIFY(!hello.mid(hello.size()-0).isNull()); QVERIFY(hello.mid(hello.size()-1) == "o"); QVERIFY(hello.mid(hello.size()-2) == "lo"); QVERIFY(hello.mid(hello.size()-200) == "hello"); |