diff --git a/src/corelib/tools/qstringalgorithms_p.h b/src/corelib/tools/qstringalgorithms_p.h index b4be5c7ec7..65901b0286 100644 --- a/src/corelib/tools/qstringalgorithms_p.h +++ b/src/corelib/tools/qstringalgorithms_p.h @@ -120,21 +120,23 @@ template struct QStringAlgorithms Char *dst = const_cast(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; } diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index 8cd9610542..d2f7a6ee50 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -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()