Fix QString::toUcs4() returns incorrectly resized QVector

...when the string contains surrogate code points.

Task-number: QTBUG-25536

Change-Id: I07251fee641c14f33175678768ddbe551dbe2bb1
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Konstantin Ritt 2013-02-26 12:41:59 +02:00 committed by The Qt Project
parent 7729d89e15
commit 996db96d5e
2 changed files with 56 additions and 7 deletions

View File

@ -966,20 +966,21 @@ const QString::Null QString::null = { };
\sa utf16(), toLatin1(), toUtf8(), toLocal8Bit() \sa utf16(), toLatin1(), toUtf8(), toLocal8Bit()
*/ */
// ### replace with QCharIterator
int QString::toUcs4_helper(const ushort *uc, int length, uint *out) int QString::toUcs4_helper(const ushort *uc, int length, uint *out)
{ {
int i = 0; int i = 0;
for (; i < length; ++i) { const ushort *const e = uc + length;
uint u = uc[i]; while (uc < e) {
if (QChar::isHighSurrogate(u) && i + 1 < length) { uint u = *uc;
ushort low = uc[i+1]; if (QChar::isHighSurrogate(u) && uc + 1 < e) {
ushort low = uc[1];
if (QChar::isLowSurrogate(low)) { if (QChar::isLowSurrogate(low)) {
++i; ++uc;
u = QChar::surrogateToUcs4(u, low); u = QChar::surrogateToUcs4(u, low);
} }
} }
*out++ = u; out[i++] = u;
++uc;
} }
return i; return i;
} }

View File

@ -195,6 +195,8 @@ private slots:
void stringRef_local8Bit(); void stringRef_local8Bit();
void fromLatin1(); void fromLatin1();
void fromAscii(); void fromAscii();
void fromUcs4();
void toUcs4();
void arg(); void arg();
void number(); void number();
void arg_fillChar_data(); void arg_fillChar_data();
@ -3931,6 +3933,52 @@ void tst_QString::fromAscii()
QVERIFY(a.size() == 5); QVERIFY(a.size() == 5);
} }
void tst_QString::fromUcs4()
{
QString s;
s = QString::fromUcs4( 0 );
QVERIFY( s.isNull() );
QCOMPARE( s.size(), 0 );
s = QString::fromUcs4( 0, 0 );
QVERIFY( s.isNull() );
QCOMPARE( s.size(), 0 );
s = QString::fromUcs4( 0, 5 );
QVERIFY( s.isNull() );
QCOMPARE( s.size(), 0 );
uint nil = '\0';
s = QString::fromUcs4( &nil );
QVERIFY( !s.isNull() );
QCOMPARE( s.size(), 0 );
s = QString::fromUcs4( &nil, 0 );
QVERIFY( !s.isNull() );
QCOMPARE( s.size(), 0 );
uint bmp = 'a';
s = QString::fromUcs4( &bmp, 1 );
QVERIFY( !s.isNull() );
QCOMPARE( s.size(), 1 );
uint smp = 0x10000;
s = QString::fromUcs4( &smp, 1 );
QVERIFY( !s.isNull() );
QCOMPARE( s.size(), 2 );
}
void tst_QString::toUcs4()
{
QString s;
QCOMPARE( s.toUcs4().size(), 0 );
QChar bmp = QLatin1Char('a');
s = QString(&bmp, 1);
QCOMPARE( s.toUcs4().size(), 1 );
QChar smp[] = { QChar::highSurrogate(0x10000), QChar::lowSurrogate(0x10000) };
s = QString(smp, 2);
QCOMPARE( s.toUcs4().size(), 1 );
}
void tst_QString::arg() void tst_QString::arg()
{ {
/* /*