Fix QByteArray::to{Upper,Lower} when the array contains embedded nulls

[ChangeLog][QtCore][QByteArray] Fixed a bug that would cause QByteArray
to stop converting toUpper or toLower at the first embedded null
character.

Change-Id: Ia369037206617813d86a8f1489589243c82aa51b
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Thiago Macieira 2014-07-28 20:01:23 -07:00
parent 5aacc43e64
commit 5d11688d02
2 changed files with 31 additions and 2 deletions

View File

@ -2697,8 +2697,9 @@ QByteArray QByteArray::toLower() const
{
QByteArray s(*this);
uchar *p = reinterpret_cast<uchar *>(s.data());
uchar *e = reinterpret_cast<uchar *>(s.end());
if (p) {
while (*p) {
while (p != e) {
*p = QChar::toLower((ushort)*p);
p++;
}
@ -2720,8 +2721,9 @@ QByteArray QByteArray::toUpper() const
{
QByteArray s(*this);
uchar *p = reinterpret_cast<uchar *>(s.data());
uchar *e = reinterpret_cast<uchar *>(s.end());
if (p) {
while (*p) {
while (p != e) {
*p = QChar::toUpper((ushort)*p);
p++;
}

View File

@ -151,6 +151,8 @@ private slots:
#if defined(Q_COMPILER_LAMBDA)
void literals();
#endif
void toUpperLower_data();
void toUpperLower();
void macTypes();
@ -1999,6 +2001,31 @@ void tst_QByteArray::literals()
}
#endif
void tst_QByteArray::toUpperLower_data()
{
QTest::addColumn<QByteArray>("input");
QTest::addColumn<QByteArray>("upper");
QTest::addColumn<QByteArray>("lower");
QTest::newRow("empty") << QByteArray() << QByteArray() << QByteArray();
QTest::newRow("ascii") << QByteArray("Hello World, this is a STRING")
<< QByteArray("HELLO WORLD, THIS IS A STRING")
<< QByteArray("hello world, this is a string");
QTest::newRow("latin1") << QByteArray("R\311sum\351")
<< QByteArray("R\311SUM\311")
<< QByteArray("r\351sum\351");
QTest::newRow("nul") << QByteArray("a\0B", 3) << QByteArray("A\0B", 3) << QByteArray("a\0b", 3);
}
void tst_QByteArray::toUpperLower()
{
QFETCH(QByteArray, input);
QFETCH(QByteArray, upper);
QFETCH(QByteArray, lower);
QCOMPARE(input.toUpper(), upper);
QCOMPARE(input.toLower(), lower);
}
void tst_QByteArray::macTypes()
{
#ifndef Q_OS_MAC