QDebug: add op<<(QStringView)

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

Change-Id: Id81fae223b60188d541b255b67bc316f9f1b6bef
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2017-01-30 10:31:10 +01:00
parent 00a8be85d1
commit 8092a01f84
3 changed files with 59 additions and 0 deletions

View File

@ -647,6 +647,21 @@ QDebug &QDebug::resetFormat()
See the QString overload for examples.
*/
/*!
\since 5.10
\fn QDebug &QDebug::operator<<(QStringView s)
Writes the string view, \a s, to the stream and returns a reference to the
stream. Normally, QDebug prints the string inside quotes and transforms
non-printable characters to their Unicode values (\\u1234).
To print non-printable characters without transformation, enable the
noquote() functionality. Note that some QDebug backends might not be 8-bit
clean.
See the QString overload for examples.
*/
/*!
\fn QDebug &QDebug::operator<<(QLatin1String s)

View File

@ -151,8 +151,11 @@ public:
inline QDebug &operator<<(float t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(double t) { stream->ts << t; return maybeSpace(); }
inline QDebug &operator<<(const char* t) { stream->ts << QString::fromUtf8(t); return maybeSpace(); }
#if QT_STRINGVIEW_LEVEL < 2
inline QDebug &operator<<(const QString & t) { putString(t.constData(), uint(t.length())); return maybeSpace(); }
inline QDebug &operator<<(const QStringRef & 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<<(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<<(const void * t) { stream->ts << t; return maybeSpace(); }

View File

@ -51,6 +51,7 @@ private slots:
void qDebugQChar() const;
void qDebugQString() const;
void qDebugQStringRef() const;
void qDebugQStringView() const;
void qDebugQLatin1String() const;
void qDebugQByteArray() const;
void qDebugQFlags() const;
@ -492,6 +493,46 @@ void tst_QDebug::qDebugQStringRef() const
}
}
void tst_QDebug::qDebugQStringView() const
{
/* Use a basic string. */
{
QLatin1String file, function;
int line = 0;
const QStringView inView = QStringViewLiteral("input");
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, QLatin1String("\"input\""));
QCOMPARE(QLatin1String(s_file), file);
QCOMPARE(s_line, line);
QCOMPARE(QLatin1String(s_function), function);
}
/* Use a null QStringView. */
{
QString file, function;
int line = 0;
const QStringView 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;