Change how QDebug escapes QStrings in the output

[ChangeLog][Important Behavior Changes] QDebug output for QStrings
changed compared to Qt 5.5.0 to more closely match the output of
previous Qt versions. Like Qt 5.5.0, QDebug will escape non-printable
characters, the backslash and quote characters, but will no longer
escape the printable characters.

Task-number: QTBUG-47316
Change-Id: I52dd43c12685407bb9a6ffff13f62ef68cbc80c5
Reviewed-by: Kai Koehne <kai.koehne@theqtcompany.com>
This commit is contained in:
Thiago Macieira 2015-07-31 17:26:07 -07:00
parent 007ad9e859
commit 644ac04af0
5 changed files with 69 additions and 22 deletions

View File

@ -174,6 +174,16 @@ void QDebug::putUcs4(uint ucs4)
maybeQuote('\''); maybeQuote('\'');
} }
// These two functions return true if the character should be printed by QDebug.
// For QByteArray, this is technically identical to US-ASCII isprint();
// for QString, we use QChar::isPrint, which requires a full UCS-4 decode.
static inline bool isPrintable(uint ucs4)
{ return QChar::isPrint(ucs4); }
static inline bool isPrintable(ushort uc)
{ return QChar::isPrint(uc); }
static inline bool isPrintable(uchar c)
{ return c >= ' ' && c < 0x7f; }
template <typename Char> template <typename Char>
static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, int length, bool isUnicode = true) static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, int length, bool isUnicode = true)
{ {
@ -194,22 +204,23 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, in
} }
if (sizeof(Char) == sizeof(QChar)) { if (sizeof(Char) == sizeof(QChar)) {
// Surrogate characters are category Cs (Other_Surrogate), so isPrintable = false for them
int runLength = 0; int runLength = 0;
while (p + runLength != end && while (p + runLength != end &&
p[runLength] < 0x7f && p[runLength] >= 0x20 && p[runLength] != '\\' && p[runLength] != '"') isPrintable(p[runLength]) && p[runLength] != '\\' && p[runLength] != '"')
++runLength; ++runLength;
if (runLength) { if (runLength) {
d->write(reinterpret_cast<const QChar *>(p), runLength); d->write(reinterpret_cast<const QChar *>(p), runLength);
p += runLength - 1; p += runLength - 1;
continue; continue;
} }
} else if (*p < 0x7f && *p >= 0x20 && *p != '\\' && *p != '"') { } else if (isPrintable(*p) && *p != '\\' && *p != '"') {
QChar c = QLatin1Char(*p); QChar c = QLatin1Char(*p);
d->write(&c, 1); d->write(&c, 1);
continue; continue;
} }
// print as an escape sequence // print as an escape sequence (maybe, see below for surrogate pairs)
int buflen = 2; int buflen = 2;
ushort buf[sizeof "\\U12345678" - 1]; ushort buf[sizeof "\\U12345678" - 1];
buf[0] = '\\'; buf[0] = '\\';
@ -248,17 +259,23 @@ static inline void putEscapedString(QTextStreamPrivate *d, const Char *begin, in
if ((p + 1) != end && QChar::isLowSurrogate(p[1])) { if ((p + 1) != end && QChar::isLowSurrogate(p[1])) {
// properly-paired surrogates // properly-paired surrogates
uint ucs4 = QChar::surrogateToUcs4(*p, p[1]); uint ucs4 = QChar::surrogateToUcs4(*p, p[1]);
if (isPrintable(ucs4)) {
buf[0] = *p;
buf[1] = p[1];
buflen = 2;
} else {
buf[1] = 'U';
buf[2] = '0'; // toHexUpper(ucs4 >> 32);
buf[3] = '0'; // toHexUpper(ucs4 >> 28);
buf[4] = toHexUpper(ucs4 >> 20);
buf[5] = toHexUpper(ucs4 >> 16);
buf[6] = toHexUpper(ucs4 >> 12);
buf[7] = toHexUpper(ucs4 >> 8);
buf[8] = toHexUpper(ucs4 >> 4);
buf[9] = toHexUpper(ucs4);
buflen = 10;
}
++p; ++p;
buf[1] = 'U';
buf[2] = '0'; // toHexUpper(ucs4 >> 32);
buf[3] = '0'; // toHexUpper(ucs4 >> 28);
buf[4] = toHexUpper(ucs4 >> 20);
buf[5] = toHexUpper(ucs4 >> 16);
buf[6] = toHexUpper(ucs4 >> 12);
buf[7] = toHexUpper(ucs4 >> 8);
buf[8] = toHexUpper(ucs4 >> 4);
buf[9] = toHexUpper(ucs4);
buflen = 10;
break; break;
} }
// improperly-paired surrogates, fall through // improperly-paired surrogates, fall through

View File

@ -379,18 +379,48 @@ void tst_QDebug::qDebugQString() const
qDebug().noquote().nospace() << qSetFieldWidth(8) << string; qDebug().noquote().nospace() << qSetFieldWidth(8) << string;
QCOMPARE(s_msg, " " + string); QCOMPARE(s_msg, " " + string);
string = QLatin1String("\nSm\xF8rg\xE5sbord\\"); string = "Sm\xc3\xb8rg\xc3\xa5sbord " // Latin script
"\xce\x91\xce\xb8\xce\xae\xce\xbd\xce\xb1 " // Greek script
"\xd0\x9c\xd0\xbe\xd1\x81\xd0\xba\xd0\xb2\xd0\xb0"; // Cyrillic script
qDebug().noquote().nospace() << string; qDebug().noquote().nospace() << string;
QCOMPARE(s_msg, string); QCOMPARE(s_msg, string);
// This string only contains printable characters
qDebug() << string; qDebug() << string;
QCOMPARE(s_msg, QString("\"\\nSm\\u00F8rg\\u00E5sbord\\\\\"")); QCOMPARE(s_msg, '"' + string + '"');
// surrogate pairs (including broken pairings) string = "\n\t\\\"";
ushort utf16[] = { 0xDC00, 0xD800, 0xDC00, 'x', 0xD800, 0xDC00, 0xD800, 0 }; qDebug().noquote().nospace() << string;
QCOMPARE(s_msg, string);
// This string only contains characters that must be escaped
qDebug() << string;
QCOMPARE(s_msg, QString("\"\\n\\t\\\\\\\"\""));
// Unicode escapes, BMP
string = "\1" // U+0001: START OF HEADING (category Cc)
"\x7f" // U+007F: DELETE (category Cc)
"\xc2\xad" // U+00AD: SOFT HYPHEN (category Cf)
"\xef\xbb\xbf"; // U+FEFF: ZERO WIDTH NO-BREAK SPACE / BOM (category Cf)
qDebug() << string;
QCOMPARE(s_msg, QString("\"\\u0001\\u007F\\u00AD\\uFEFF\""));
// Unicode printable non-BMP
string = "\xf0\x90\x80\x80"; // U+10000: LINEAR B SYLLABLE B008 A (category Lo)
qDebug() << string;
QCOMPARE(s_msg, '"' + string + '"');
// non-BMP and non-printable
string = "\xf3\xa0\x80\x81 " // U+E0001: LANGUAGE TAG (category Cf)
"\xf4\x80\x80\x80"; // U+100000: Plane 16 Private Use (category Co)
qDebug() << string;
QCOMPARE(s_msg, QString("\"\\U000E0001 \\U00100000\""));
// broken surrogate pairs
ushort utf16[] = { 0xDC00, 0xD800, 'x', 0xD800, 0 };
string = QString::fromUtf16(utf16); string = QString::fromUtf16(utf16);
qDebug() << string; qDebug() << string;
QCOMPARE(s_msg, QString("\"\\uDC00\\U00010000x\\U00010000\\uD800\"")); QCOMPARE(s_msg, QString("\"\\uDC00\\uD800x\\uD800\""));
} }
void tst_QDebug::qDebugQStringRef() const void tst_QDebug::qDebugQStringRef() const

View File

@ -113,7 +113,7 @@
</TestFunction> </TestFunction>
<TestFunction name="encoding"> <TestFunction name="encoding">
<Message type="qdebug" file="" line="0"> <Message type="qdebug" file="" line="0">
<Description><![CDATA["\u00DClrich \u00DCml\u00E4ut"]]></Description> <Description><![CDATA["Ülrich Ümläut"]]></Description>
</Message> </Message>
<Incident type="pass" file="" line="0" /> <Incident type="pass" file="" line="0" />
<Duration msecs="0"/> <Duration msecs="0"/>

View File

@ -115,7 +115,7 @@
</TestFunction> </TestFunction>
<TestFunction name="encoding"> <TestFunction name="encoding">
<Message type="qdebug" file="" line="0"> <Message type="qdebug" file="" line="0">
<Description><![CDATA["\u00DClrich \u00DCml\u00E4ut"]]></Description> <Description><![CDATA["Ülrich Ümläut"]]></Description>
</Message> </Message>
<Incident type="pass" file="" line="0" /> <Incident type="pass" file="" line="0" />
<Duration msecs="0"/> <Duration msecs="0"/>

View File

@ -30,7 +30,7 @@
<failure message="failure message" result="fail"/> <failure message="failure message" result="fail"/>
</testcase> </testcase>
<testcase result="pass" name="encoding"> <testcase result="pass" name="encoding">
<!-- message="&quot;\u00DClrich \u00DCml\u00E4ut&quot;" type="qdebug" --> <!-- message="&quot;Ülrich Ümläut&quot;" type="qdebug" -->
</testcase> </testcase>
<testcase result="pass" name="cleanupTestCase"/> <testcase result="pass" name="cleanupTestCase"/>
<system-err> <system-err>
@ -46,6 +46,6 @@
<![CDATA[quotes " text" more text]]> <![CDATA[quotes " text" more text]]>
<![CDATA[xml close > open < tags < text]]> <![CDATA[xml close > open < tags < text]]>
<![CDATA[all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]> <![CDATA[all > " mixed ]]]><![CDATA[]> up > " in < the ]]]><![CDATA[]> hopes < of triggering "< ]]]><![CDATA[]> bugs]]>
<![CDATA["\u00DClrich \u00DCml\u00E4ut"]]> <![CDATA["Ülrich Ümläut"]]>
</system-err> </system-err>
</testsuite> </testsuite>