diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2012-02-01 20:47:26 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-02-02 11:10:04 +0100 |
commit | 7edd623957b958d7d3db20772571810d7be9da85 (patch) | |
tree | 2e89fd13e14c25b34d6e7bc03752d1eedcd72911 | |
parent | 35b19be4784a65894ac9dfe64a6c036b95b06304 (diff) |
Fix QTextStream and QDebug operator<< for QLatin1String
QLatin1String now has a constructor that takes explicit length, which
makes it possible to create a QLatin1String that isn't null-terminated.
Made the QTextStream and QDebug << operators work in that case.
Change-Id: I94d051ce2ebfb2d2a403b96d25e040c80a54bf7c
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
-rw-r--r-- | src/corelib/io/qdebug.h | 2 | ||||
-rw-r--r-- | src/corelib/io/qtextstream.cpp | 15 | ||||
-rw-r--r-- | src/corelib/io/qtextstream.h | 1 | ||||
-rw-r--r-- | tests/auto/corelib/io/qdebug/tst_qdebug.cpp | 13 | ||||
-rw-r--r-- | tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp | 15 |
5 files changed, 45 insertions, 1 deletions
diff --git a/src/corelib/io/qdebug.h b/src/corelib/io/qdebug.h index c268660573..48a382eea6 100644 --- a/src/corelib/io/qdebug.h +++ b/src/corelib/io/qdebug.h @@ -113,7 +113,7 @@ public: inline QDebug &operator<<(const char* t) { stream->ts << QString::fromAscii(t); return maybeSpace(); } inline QDebug &operator<<(const QString & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); } inline QDebug &operator<<(const QStringRef & t) { return operator<<(t.toString()); } - inline QDebug &operator<<(const QLatin1String &t) { stream->ts << '\"' << t.latin1() << '\"'; return maybeSpace(); } + inline QDebug &operator<<(const QLatin1String &t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); } inline QDebug &operator<<(const QByteArray & t) { stream->ts << '\"' << t << '\"'; return maybeSpace(); } inline QDebug &operator<<(const void * t) { stream->ts << t; return maybeSpace(); } inline QDebug &operator<<(QTextStreamFunction f) { diff --git a/src/corelib/io/qtextstream.cpp b/src/corelib/io/qtextstream.cpp index 7ea00ceec2..7f37866226 100644 --- a/src/corelib/io/qtextstream.cpp +++ b/src/corelib/io/qtextstream.cpp @@ -2533,6 +2533,21 @@ QTextStream &QTextStream::operator<<(const QString &string) /*! \overload + Writes \a string to the stream, and returns a reference to the + QTextStream. The contents of \a string are converted with the + QString constructor that takes a QLatin1String as argument. +*/ +QTextStream &QTextStream::operator<<(const QLatin1String &string) +{ + Q_D(QTextStream); + CHECK_VALID_STREAM(*this); + d->putString(QString(string)); + return *this; +} + +/*! + \overload + Writes \a array to the stream. The contents of \a array are converted with QString::fromAscii(). */ diff --git a/src/corelib/io/qtextstream.h b/src/corelib/io/qtextstream.h index 3e1d0faa25..011d43ebb0 100644 --- a/src/corelib/io/qtextstream.h +++ b/src/corelib/io/qtextstream.h @@ -187,6 +187,7 @@ public: QTextStream &operator<<(float f); QTextStream &operator<<(double f); QTextStream &operator<<(const QString &s); + QTextStream &operator<<(const QLatin1String &s); QTextStream &operator<<(const QByteArray &array); QTextStream &operator<<(const char *c); QTextStream &operator<<(const void *ptr); diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 19f020f750..12f7e28a1d 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -54,6 +54,7 @@ private slots: void debugWithBool() const; void veryLongWarningMessage() const; void qDebugQStringRef() const; + void qDebugQLatin1String() const; void defaultMessagehandler() const; }; @@ -198,6 +199,18 @@ void tst_QDebug::qDebugQStringRef() const } } +void tst_QDebug::qDebugQLatin1String() const +{ + MessageHandlerSetter mhs(myMessageHandler); + { qDebug() << QLatin1String("foo") << QLatin1String("") << QLatin1String("barbaz", 3); } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; + QCOMPARE(s_msgType, QtDebugMsg); + QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"foo\" \"\" \"bar\" ")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); +} + void tst_QDebug::defaultMessagehandler() const { MessageHandlerSetter mhs(0); diff --git a/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp index 7d5db69ffd..01aecc2a21 100644 --- a/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/io/qtextstream/tst_qtextstream.cpp @@ -170,6 +170,7 @@ private slots: // text write operators void string_write_operator_ToDevice_data(); void string_write_operator_ToDevice(); + void latin1String_write_operator_ToDevice(); // other void skipWhiteSpace_data(); @@ -2384,6 +2385,20 @@ void tst_QTextStream::string_write_operator_ToDevice() } } +void tst_QTextStream::latin1String_write_operator_ToDevice() +{ + QBuffer buf; + buf.open(QBuffer::WriteOnly); + QTextStream stream(&buf); + stream.setCodec(QTextCodec::codecForName("ISO-8859-1")); + stream.setAutoDetectUnicode(true); + + stream << QLatin1String("No explicit length"); + stream << QLatin1String("Explicit length - ignore this part", 15); + stream.flush(); + QCOMPARE(buf.buffer().constData(), "No explicit lengthExplicit length"); +} + // ------------------------------------------------------------------------------ void tst_QTextStream::useCase1() { |