From dde8d5e3a006c87b7a3f18733ba255d6354fcd37 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 15 Oct 2015 14:47:18 +0200 Subject: QTextStream: add missing op<<(QStringRef) It simply is missing. We could wait for QStringView to come around, but I need this function in uic _now_, so let's add it. [ChangeLog][QtCore][QTextStream] Can now stream QStringRef without converting to a QString first. Change-Id: Idd178e0ba8a89c025f4533d46de912cbdb3883d5 Reviewed-by: Lars Knoll --- src/corelib/io/qtextstream.cpp | 15 +++++++++++++++ src/corelib/io/qtextstream.h | 1 + tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp | 17 +++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index 5d82453270..64222c2212 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -2519,6 +2519,21 @@ QTextStream &QTextStream::operator<<(QLatin1String string) return *this; } +/*! + \since 5.6 + \overload + + Writes \a string to the stream, and returns a reference to the + QTextStream. +*/ +QTextStream &QTextStream::operator<<(const QStringRef &string) +{ + Q_D(QTextStream); + CHECK_VALID_STREAM(*this); + d->putString(string.data(), string.size()); + return *this; +} + /*! \overload diff --git a/src/corelib/io/qtextstream.h b/src/corelib/io/qtextstream.h index eb33db63d7..d7566e6f7b 100644 --- a/src/corelib/io/qtextstream.h +++ b/src/corelib/io/qtextstream.h @@ -179,6 +179,7 @@ public: QTextStream &operator<<(double f); QTextStream &operator<<(const QString &s); QTextStream &operator<<(QLatin1String s); + QTextStream &operator<<(const QStringRef &s); QTextStream &operator<<(const QByteArray &array); QTextStream &operator<<(const char *c); QTextStream &operator<<(const void *ptr); diff --git a/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp index ecec97f009..6e58642eb6 100644 --- a/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp @@ -163,6 +163,7 @@ private slots: void string_write_operator_ToDevice_data(); void string_write_operator_ToDevice(); void latin1String_write_operator_ToDevice(); + void stringref_write_operator_ToDevice(); // other void skipWhiteSpace_data(); @@ -2554,6 +2555,22 @@ void tst_QTextStream::latin1String_write_operator_ToDevice() QCOMPARE(buf.buffer().constData(), "No explicit lengthExplicit length"); } +void tst_QTextStream::stringref_write_operator_ToDevice() +{ + QBuffer buf; + buf.open(QBuffer::WriteOnly); + QTextStream stream(&buf); + stream.setCodec(QTextCodec::codecForName("ISO-8859-1")); + stream.setAutoDetectUnicode(true); + + const QString expected = "No explicit lengthExplicit length"; + + stream << expected.leftRef(18); + stream << expected.midRef(18); + stream.flush(); + QCOMPARE(buf.buffer().constData(), "No explicit lengthExplicit length"); +} + // ------------------------------------------------------------------------------ void tst_QTextStream::useCase1() { -- cgit v1.2.3