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 <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2022-05-02 14:54:20 +02:00
parent 175cc7f7c0
commit 7905b624fd
3 changed files with 37 additions and 0 deletions

View File

@ -2287,6 +2287,15 @@ QTextStream &QTextStream::operator<<(char c)
return *this; 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 Writes the integer number \a i to the stream, then returns a
reference to the QTextStream. By default, the number is stored in reference to the QTextStream. By default, the number is stored in

View File

@ -169,6 +169,7 @@ public:
QTextStream &operator<<(QChar ch); QTextStream &operator<<(QChar ch);
QTextStream &operator<<(char ch); QTextStream &operator<<(char ch);
QTextStream &operator<<(char16_t ch) { return *this << QChar(ch); }
QTextStream &operator<<(signed short i); QTextStream &operator<<(signed short i);
QTextStream &operator<<(unsigned short i); QTextStream &operator<<(unsigned short i);
QTextStream &operator<<(signed int i); QTextStream &operator<<(signed int i);

View File

@ -94,6 +94,8 @@ private slots:
// char operators // char operators
void QChar_operators_FromDevice_data(); void QChar_operators_FromDevice_data();
void QChar_operators_FromDevice(); void QChar_operators_FromDevice();
void char16_t_operators_FromDevice_data();
void char16_t_operators_FromDevice();
void char_operators_FromDevice_data(); void char_operators_FromDevice_data();
void char_operators_FromDevice(); void char_operators_FromDevice();
@ -1918,6 +1920,31 @@ void tst_QTextStream::QChar_operators_FromDevice()
QCOMPARE(writeBuf.buffer().constData(), write_output.constData()); 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() void tst_QTextStream::char_operators_FromDevice_data()
{ {