Skip to content

Commit

Permalink
QStringRef: add chop()
Browse files Browse the repository at this point in the history
chop() was missing in the API.

Change-Id: I15af86c8f218cf159b8ce19bbeb2ffa6201f98cf
Reviewed-by: Edward Welbourne <[email protected]>
  • Loading branch information
Anton Kudryavtsev committed Jul 7, 2016
1 parent a13398d commit e91c412
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/corelib/tools/qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5009,7 +5009,7 @@ void QString::truncate(int pos)
If you want to remove characters from the \e beginning of the
string, use remove() instead.
\sa truncate(), resize(), remove()
\sa truncate(), resize(), remove(), QStringRef::chop()
*/
void QString::chop(int n)
{
Expand Down Expand Up @@ -9659,6 +9659,18 @@ QStringRef QString::midRef(int position, int n) const
\sa QString::truncate()
*/

/*!
\fn void QStringRef::chop(int n)
\since 5.8
Removes \a n characters from the end of the string.
If \a n is greater than or equal to size(), the result is an
empty string; if \a n is negative, it is equivalent to passing zero.
\sa QString::chop(), truncate()
*/

/*!
\since 4.8
Expand Down
6 changes: 6 additions & 0 deletions src/corelib/tools/qstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,12 @@ class Q_CORE_EXPORT QStringRef {
QStringRef mid(int pos, int n = -1) const Q_REQUIRED_RESULT;

void truncate(int pos) Q_DECL_NOTHROW { m_size = qBound(0, pos, m_size); }
void chop(int n) Q_DECL_NOTHROW {
if (n >= m_size)
m_size = 0;
else if (n > 0)
m_size -= n;
}

bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
bool startsWith(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
Expand Down
43 changes: 43 additions & 0 deletions tests/auto/corelib/tools/qstringref/tst_qstringref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private slots:
void integer_conversion();
void trimmed();
void truncate();
void chop();
void left();
void right();
void mid();
Expand Down Expand Up @@ -1904,6 +1905,48 @@ void tst_QStringRef::truncate()
}
}

void tst_QStringRef::chop()
{
const QString originalString = QStringLiteral("OriginalString~");
const QStringRef cref(&originalString);
{
const int n = 1;
QStringRef ref = cref;
QString str = originalString;
ref.chop(n);
str.chop(n);
QCOMPARE(ref.toString(), QLatin1String("OriginalString"));
QCOMPARE(ref.toString(), str);
}
{
const int n = -1;
QStringRef ref = cref;
QString str = originalString;
ref.chop(n);
str.chop(n);
QCOMPARE(ref.toString(), originalString);
QCOMPARE(ref.toString(), str);
}
{
const int n = 0;
QStringRef ref = cref;
QString str = originalString;
ref.chop(n);
str.chop(n);
QCOMPARE(ref.toString(), originalString);
QCOMPARE(ref.toString(), str);
}
{
const int n = 1000;
QStringRef ref = cref;
QString str = originalString;
ref.chop(n);
str.chop(n);
QCOMPARE(ref.toString(), str);
QVERIFY(ref.isEmpty());
}
}

void tst_QStringRef::left()
{
QString originalString = "OrginalString~";
Expand Down

0 comments on commit e91c412

Please sign in to comment.