Track modifications of white space in QString::simplified().

The existing check fails to detect the case where white space characters
other than the space character are replaced by space characters
without the length actually changing and returns the original string.

Task-number: QTBUG-44936
Change-Id: Ice6faa975f8b41f185c76f6d0d4ff81603e25eb3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Friedemann Kleint 2015-03-05 09:11:11 +01:00
parent 718f87046a
commit 5930a2d314
2 changed files with 8 additions and 4 deletions

View File

@ -120,21 +120,23 @@ template <typename StringType> struct QStringAlgorithms
Char *dst = const_cast<Char *>(result.cbegin());
Char *ptr = dst;
bool unmodified = true;
forever {
while (src != end && isSpace(*src))
++src;
while (src != end && !isSpace(*src))
*ptr++ = *src++;
if (src != end)
*ptr++ = QChar::Space;
else
if (src == end)
break;
if (*src != QChar::Space)
unmodified = false;
*ptr++ = QChar::Space;
}
if (ptr != dst && ptr[-1] == QChar::Space)
--ptr;
int newlen = ptr - dst;
if (isConst && newlen == str.size()) {
if (isConst && newlen == str.size() && unmodified) {
// nothing happened, return the original
return str;
}

View File

@ -2033,6 +2033,8 @@ void tst_QString::simplified_data()
QTest::newRow("chars apart posttab") << "a \tb" << "a b";
QTest::newRow("chars apart pretab") << "a\t b" << "a b";
QTest::newRow("many words") << " just some random\ttext here" << "just some random text here";
QTest::newRow("newlines") << "a\nb\nc" << "a b c";
QTest::newRow("newlines-trailing") << "a\nb\nc\n" << "a b c";
}
void tst_QString::simplified()