QDebug: add op<<(QUtf8StringView)

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

Task-number: QTBUG-88029
Change-Id: Ifcb001cfebddd79ce1cdbfef5bcc19f7c56679ec
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Karsten Heimrich 2020-11-03 15:03:57 +01:00
parent 7cad91a7df
commit e9b92c926d
3 changed files with 59 additions and 0 deletions

View File

@ -662,6 +662,23 @@ QDebug &QDebug::resetFormat()
See the QString overload for examples.
*/
/*!
\since 6.0
\fn QDebug &QDebug::operator<<(QUtf8StringView s)
Writes the string view, \a s, 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.
*/
/*!
\fn QDebug &QDebug::operator<<(QLatin1String t)

View File

@ -144,6 +144,7 @@ public:
inline QDebug &operator<<(const QString & t) { putString(t.constData(), uint(t.length())); return maybeSpace(); }
#endif
inline QDebug &operator<<(QStringView s) { putString(s.data(), size_t(s.size())); return maybeSpace(); }
inline QDebug &operator<<(QUtf8StringView s) { putByteArray(s.data(), s.size(), ContainsBinary); 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(); }

View File

@ -73,6 +73,7 @@ private slots:
void qDebugQChar() const;
void qDebugQString() const;
void qDebugQStringView() const;
void qDebugQUtf8StringView() const;
void qDebugQLatin1String() const;
void qDebugQByteArray() const;
void qDebugQByteArrayView() const;
@ -556,6 +557,46 @@ void tst_QDebug::qDebugQStringView() const
}
}
void tst_QDebug::qDebugQUtf8StringView() const
{
/* Use a utf8 string. */
{
QLatin1String file, function;
int line = 0;
const QUtf8StringView inView = u8"\U0001F609 is ;-)";
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << inView; }
#ifndef QT_NO_MESSAGELOGCONTEXT
file = QLatin1String(__FILE__); line = __LINE__ - 2; function = QLatin1String(Q_FUNC_INFO);
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QString::fromUtf8("\"\\xF0\\x9F\\x98\\x89 is ;-)\""));
QCOMPARE(QLatin1String(s_file), file);
QCOMPARE(s_line, line);
QCOMPARE(QLatin1String(s_function), function);
}
/* Use a null QUtf8StringView. */
{
QString file, function;
int line = 0;
const QUtf8StringView inView;
MessageHandlerSetter mhs(myMessageHandler);
{ qDebug() << inView; }
#ifndef QT_NO_MESSAGELOGCONTEXT
file = __FILE__; line = __LINE__ - 2; function = Q_FUNC_INFO;
#endif
QCOMPARE(s_msgType, QtDebugMsg);
QCOMPARE(s_msg, QLatin1String("\"\""));
QCOMPARE(QLatin1String(s_file), file);
QCOMPARE(s_line, line);
QCOMPARE(QLatin1String(s_function), function);
}
}
void tst_QDebug::qDebugQLatin1String() const
{
QString file, function;