QDebug: add op<<(QByteArrayView)

[ChangeLog][QtCore][QDebug] Added streaming of QByteArrayViews.

Task-number: QTBUG-88029
Change-Id: I66f5cc45a0438dbaacb1754cb3c669b3717f528b
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Karsten Heimrich 2020-11-02 23:29:08 +01:00
parent b8069f0a47
commit 7cad91a7df
3 changed files with 63 additions and 0 deletions

View File

@ -697,6 +697,25 @@ QDebug &QDebug::resetFormat()
interpreted as part of the previous hexadecimal escape sequence.
*/
/*!
\since 6.0
\fn QDebug &QDebug::operator<<(QByteArrayView t)
Writes the data of the observed byte array, \a t, to the stream and returns
a reference to the stream.
Normally, QDebug prints the data inside quotes and transforms control or
non-US-ASCII characters to their C escape sequences (\\xAB). This way, the
output is always 7-bit clean and the string can be copied from the output
and pasted back into C++ sources, if necessary.
To print non-printable characters without transformation, enable the
noquote() functionality. Note that some QDebug backends might not be 8-bit
clean.
See the QByteArray overload for examples.
*/
/*!
\fn QDebug &QDebug::operator<<(const void *t)

View File

@ -146,6 +146,7 @@ public:
inline QDebug &operator<<(QStringView s) { putString(s.data(), size_t(s.size())); return maybeSpace(); }
inline QDebug &operator<<(QLatin1String t) { putByteArray(t.latin1(), t.size(), ContainsLatin1); return maybeSpace(); }
inline QDebug &operator<<(const QByteArray & t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
inline QDebug &operator<<(QByteArrayView t) { putByteArray(t.constData(), t.size(), ContainsBinary); return maybeSpace(); }
inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(std::nullptr_t) { stream->ts << "(nullptr)"; return maybeSpace(); }
inline QDebug &operator<<(QTextStreamFunction f) {

View File

@ -75,6 +75,7 @@ private slots:
void qDebugQStringView() const;
void qDebugQLatin1String() const;
void qDebugQByteArray() const;
void qDebugQByteArrayView() const;
void qDebugQFlags() const;
void textStreamModifiers() const;
void resetFormat() const;
@ -635,6 +636,48 @@ void tst_QDebug::qDebugQByteArray() const
QCOMPARE(s_msg, QString("\"\\xFF\"\"FFFF\""));
}
void tst_QDebug::qDebugQByteArrayView() const
{
QString file, function;
int line = 0;
MessageHandlerSetter mhs(myMessageHandler);
{
QDebug d = qDebug();
d << QByteArrayView("foo") << QByteArrayView("") << QByteArrayView("barbaz", 3);
d.nospace().noquote() << QByteArrayView("baz");
}
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 5; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromLatin1("\"foo\" \"\" \"bar\" baz"));
QCOMPARE(QString::fromLatin1(s_file), file);
QCOMPARE(s_line, line);
QCOMPARE(QString::fromLatin1(s_function), function);
/* simpler tests from now on */
QByteArrayView ba = "\"Hello\"";
qDebug() << ba;
QCOMPARE(s_msg, QString("\"\\\"Hello\\\"\""));
qDebug().noquote().nospace() << ba;
QCOMPARE(s_msg, QString::fromLatin1(ba));
qDebug().noquote().nospace() << qSetFieldWidth(8) << ba;
QCOMPARE(s_msg, " " + QString::fromLatin1(ba));
ba = "\nSm\xC3\xB8rg\xC3\xA5sbord\\";
qDebug().noquote().nospace() << ba;
QCOMPARE(s_msg, QString::fromUtf8(ba));
qDebug() << ba;
QCOMPARE(s_msg, QString("\"\\nSm\\xC3\\xB8rg\\xC3\\xA5sbord\\\\\""));
// ensure that it closes hex escape sequences correctly
qDebug() << QByteArrayView("\377FFFF");
QCOMPARE(s_msg, QString("\"\\xFF\"\"FFFF\""));
}
enum TestEnum {
Flag1 = 0x1,
Flag2 = 0x10