Make QStringRef::right() consistent with QString::right()

The implementation was inconsistent with QString::right(),
and did not return the N rightmost characters but actually did
the same as QString::mid(N) (returning the rightmost size - N
characters.)

Since this function is fairly recent (Qt 5.2), is documented to
behave the same as QString::right(), and since these APIs are
meant to be interchangeable, this needs to be fixed, even though
it changes behavior.

[ChangeLog][Important Behavior Changes] Changed QStringRef::right()
to be consistent with QString::right(). The function now returns
the N right-most characters, like the documentation already claimed.

Change-Id: I2d1cd6d958dfa9354aa09f16bd27b1ed209c2d11
Task-number: QTBUG-41858
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
Eskil Abrahamsen Blomfeldt 2014-10-13 08:49:16 +02:00
parent 5d9dcac0f2
commit 4bf0660ae4
3 changed files with 4 additions and 4 deletions

View File

@ -4419,7 +4419,7 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
if (size == 10) if (size == 10)
return QDateTime(date); return QDateTime(date);
isoString = isoString.right(11); isoString = isoString.right(isoString.length() - 11);
int offset = 0; int offset = 0;
// Check end of string for Time Zone definition, either Z for UTC or [+-]HH:MM for Offset // Check end of string for Time Zone definition, either Z for UTC or [+-]HH:MM for Offset
if (isoString.endsWith(QLatin1Char('Z'))) { if (isoString.endsWith(QLatin1Char('Z'))) {

View File

@ -9020,7 +9020,7 @@ QStringRef QStringRef::right(int n) const
{ {
if (uint(n) >= uint(m_size)) if (uint(n) >= uint(m_size))
return *this; return *this;
return QStringRef(m_string, n + m_position, m_size - n); return QStringRef(m_string, m_size - n + m_position, n);
} }
/*! /*!

View File

@ -1870,9 +1870,9 @@ void tst_QStringRef::right()
QStringRef ref = originalString.rightRef(originalString.size() - 1); QStringRef ref = originalString.rightRef(originalString.size() - 1);
QCOMPARE(ref.toString(), QLatin1String("OrginalString")); QCOMPARE(ref.toString(), QLatin1String("OrginalString"));
QCOMPARE(ref.right(ref.size() - 6).toString(), QLatin1String("String")); QCOMPARE(ref.right(6).toString(), QLatin1String("String"));
QCOMPARE(ref.right(ref.size()).toString(), QLatin1String("OrginalString")); QCOMPARE(ref.right(ref.size()).toString(), QLatin1String("OrginalString"));
QCOMPARE(ref.right(0).toString(), QLatin1String("OrginalString")); QCOMPARE(ref.right(0).toString(), QLatin1String(""));
QStringRef nullRef; QStringRef nullRef;
QVERIFY(nullRef.isNull()); QVERIFY(nullRef.isNull());