Add rvalue-ref qualified overload of QByteArray::to{Upper,Lower}

Those operations aren't very common with QByteArray but this is easy to
optimize.

                Qt                   Qt Creator
        const &         &&      const &         &&
toLower      34         10            0          1
toUpper       3          1            0          0

Change-Id: I2097955f4c889ea5a21903c35ddbc0ff27bf62c5
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
Thiago Macieira 2014-07-28 20:46:18 -07:00
parent 6dd759c8e9
commit 55c1f54c55
4 changed files with 84 additions and 8 deletions

View File

@ -2777,10 +2777,10 @@ static QByteArray toCase_template(T &input, const uchar * table)
}
if (firstBad == e)
return input;
return qMove(input);
// transform the rest
QByteArray s = input;
QByteArray s = qMove(input); // will copy if T is const QByteArray
char *b = s.begin(); // will detach if necessary
char *p = b + (firstBad - orig_begin);
e = b + s.size();
@ -2790,13 +2790,19 @@ static QByteArray toCase_template(T &input, const uchar * table)
return s;
}
QByteArray QByteArray::toLower() const
QByteArray QByteArray::toLower_helper(const QByteArray &a)
{
return toCase_template(*this, latin1_lowercased);
return toCase_template(a, latin1_lowercased);
}
QByteArray QByteArray::toLower_helper(QByteArray &a)
{
return toCase_template(a, latin1_lowercased);
}
/*!
\fn QByteArray QByteArray::toUpper() const
Returns an uppercase copy of the byte array. The bytearray is
interpreted as a Latin-1 encoded string.
@ -2805,9 +2811,15 @@ QByteArray QByteArray::toLower() const
\sa toLower(), {8-bit Character Comparisons}
*/
QByteArray QByteArray::toUpper() const
QByteArray QByteArray::toUpper_helper(const QByteArray &a)
{
return toCase_template(*this, latin1_uppercased);
return toCase_template(a, latin1_uppercased);
}
QByteArray QByteArray::toUpper_helper(QByteArray &a)
{
return toCase_template(a, latin1_uppercased);
}
/*! \fn void QByteArray::clear()

View File

@ -264,8 +264,29 @@ public:
void truncate(int pos);
void chop(int n);
#if defined(Q_COMPILER_REF_QUALIFIERS) && !defined(QT_COMPILING_QSTRING_COMPAT_CPP)
# if defined(Q_CC_GNU)
// required due to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61941
# pragma push_macro("Q_REQUIRED_RESULT")
# undef Q_REQUIRED_RESULT
# define Q_REQUIRED_RESULT
# define Q_REQUIRED_RESULT_pushed
# endif
QByteArray toLower() const & Q_REQUIRED_RESULT
{ return toLower_helper(*this); }
QByteArray toLower() && Q_REQUIRED_RESULT
{ return toLower_helper(*this); }
QByteArray toUpper() const & Q_REQUIRED_RESULT
{ return toUpper_helper(*this); }
QByteArray toUpper() && Q_REQUIRED_RESULT
{ return toUpper_helper(*this); }
# ifdef Q_REQUIRED_RESULT_pushed
# pragma pop_macro("Q_REQUIRED_RESULT")
# endif
#else
QByteArray toLower() const Q_REQUIRED_RESULT;
QByteArray toUpper() const Q_REQUIRED_RESULT;
#endif
QByteArray trimmed() const Q_REQUIRED_RESULT;
QByteArray simplified() const Q_REQUIRED_RESULT;
@ -422,6 +443,10 @@ private:
void expand(int i);
QByteArray nulTerminated() const;
static QByteArray toLower_helper(const QByteArray &a);
static QByteArray toLower_helper(QByteArray &a);
static QByteArray toUpper_helper(const QByteArray &a);
static QByteArray toUpper_helper(QByteArray &a);
friend class QByteRef;
friend class QString;
friend Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes);

View File

@ -39,11 +39,12 @@
**
****************************************************************************/
#if defined(QSTRING_H)
#if defined(QSTRING_H) || defined(QBYTEARRAY_H)
# error "This file cannot be compiled with pre-compiled headers"
#endif
#define QT_COMPILING_QSTRING_COMPAT_CPP
#include "qbytearray.h"
#include "qstring.h"
QT_BEGIN_NAMESPACE
@ -64,4 +65,15 @@ QByteArray QString::toUtf8() const
return toUtf8_helper(*this);
}
// ditto, for qbytearray.h (because we're lazy)
QByteArray QByteArray::toLower() const
{
return toLower_helper(*this);
}
QByteArray QByteArray::toUpper() const
{
return toUpper_helper(*this);
}
QT_END_NAMESPACE

View File

@ -2008,6 +2008,9 @@ void tst_QByteArray::toUpperLower_data()
QTest::addColumn<QByteArray>("lower");
QTest::newRow("empty") << QByteArray() << QByteArray() << QByteArray();
QTest::newRow("literal") << QByteArrayLiteral("Hello World")
<< QByteArrayLiteral("HELLO WORLD")
<< QByteArrayLiteral("hello world");
QTest::newRow("ascii") << QByteArray("Hello World, this is a STRING")
<< QByteArray("HELLO WORLD, THIS IS A STRING")
<< QByteArray("hello world, this is a string");
@ -2026,6 +2029,30 @@ void tst_QByteArray::toUpperLower()
QCOMPARE(upper.toUpper(), upper);
QCOMPARE(input.toUpper(), upper);
QCOMPARE(input.toLower(), lower);
QByteArray copy = input;
QCOMPARE(qMove(copy).toUpper(), upper);
copy = input;
copy.detach();
QCOMPARE(qMove(copy).toUpper(), upper);
copy = input;
QCOMPARE(qMove(copy).toLower(), lower);
copy = input;
copy.detach();
QCOMPARE(qMove(copy).toLower(), lower);
copy = lower;
QCOMPARE(qMove(copy).toLower(), lower);
copy = lower;
copy.detach();
QCOMPARE(qMove(copy).toLower(), lower);
copy = upper;
QCOMPARE(qMove(copy).toUpper(), upper);
copy = upper;
copy.detach();
QCOMPARE(qMove(copy).toUpper(), upper);
}
void tst_QByteArray::macTypes()