From 7905b624fda19960f85cfada6b84e416d738a22a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 2 May 2022 14:54:20 +0200 Subject: [PATCH] QTextStream: fix streaming of char16_t's Clazy complains about all uses of QLatin1Char these days, but if one actually applies the fixit to turn out << QLatin1Char(' '); into out << u' '; the space is now streamed as an int (20), not as a space. Fix by providing an explicit char16_t overload. [ChangeLog][QtCore][QTextStream] Added op<<(char16_t). [ChangeLog][Important Behavior Changes] QTextStream streams char16_t's as QChars now instead of outputting the numeric value. If you want to preserve the old behavior, cast the char16_t to a numeric type, such as ushort or int, and stream that. This is backwards-compatible. Pick-to: 6.3 Change-Id: I42d422cdebb27d38ac1714b22ef186642ec407e7 Reviewed-by: Thiago Macieira Reviewed-by: Qt CI Bot --- src/corelib/serialization/qtextstream.cpp | 9 +++++++ src/corelib/serialization/qtextstream.h | 1 + .../qtextstream/tst_qtextstream.cpp | 27 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp index aaed1a905e..a29be8969a 100644 --- a/src/corelib/serialization/qtextstream.cpp +++ b/src/corelib/serialization/qtextstream.cpp @@ -2287,6 +2287,15 @@ QTextStream &QTextStream::operator<<(char c) return *this; } +/*! + \fn QTextStream &QTextStream::operator<<(char16_t c) + \overload + \since 6.3.1 + + Writes the Unicode character \a c to the stream, then returns a + reference to the QTextStream. +*/ + /*! Writes the integer number \a i to the stream, then returns a reference to the QTextStream. By default, the number is stored in diff --git a/src/corelib/serialization/qtextstream.h b/src/corelib/serialization/qtextstream.h index 52e1360b66..05631c0883 100644 --- a/src/corelib/serialization/qtextstream.h +++ b/src/corelib/serialization/qtextstream.h @@ -169,6 +169,7 @@ public: QTextStream &operator<<(QChar ch); QTextStream &operator<<(char ch); + QTextStream &operator<<(char16_t ch) { return *this << QChar(ch); } QTextStream &operator<<(signed short i); QTextStream &operator<<(unsigned short i); QTextStream &operator<<(signed int i); diff --git a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp index 9e8bcfaea2..5a7be18823 100644 --- a/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp +++ b/tests/auto/corelib/serialization/qtextstream/tst_qtextstream.cpp @@ -94,6 +94,8 @@ private slots: // char operators void QChar_operators_FromDevice_data(); void QChar_operators_FromDevice(); + void char16_t_operators_FromDevice_data(); + void char16_t_operators_FromDevice(); void char_operators_FromDevice_data(); void char_operators_FromDevice(); @@ -1918,6 +1920,31 @@ void tst_QTextStream::QChar_operators_FromDevice() QCOMPARE(writeBuf.buffer().constData(), write_output.constData()); } +// ------------------------------------------------------------------------------ +void tst_QTextStream::char16_t_operators_FromDevice_data() +{ + generateOperatorCharData(false); +} + +// ------------------------------------------------------------------------------ +void tst_QTextStream::char16_t_operators_FromDevice() +{ + QFETCH(const QChar, qchar_output); + QFETCH(const QByteArray, write_output); + const char16_t char16_t_output = qchar_output.unicode(); + + QBuffer writeBuf; + writeBuf.open(QBuffer::WriteOnly); + + QTextStream writeStream(&writeBuf); + writeStream.setEncoding(QStringConverter::Latin1); + writeStream << char16_t_output; + writeStream.flush(); + + QCOMPARE(writeBuf.buffer().size(), write_output.size()); + QCOMPARE(writeBuf.buffer().constData(), write_output.constData()); +} + // ------------------------------------------------------------------------------ void tst_QTextStream::char_operators_FromDevice_data() {