summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qtextstream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/serialization/qtextstream.cpp')
-rw-r--r--src/corelib/serialization/qtextstream.cpp295
1 files changed, 106 insertions, 189 deletions
diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp
index c834f22834..e9d650b3e2 100644
--- a/src/corelib/serialization/qtextstream.cpp
+++ b/src/corelib/serialization/qtextstream.cpp
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
//#define QTEXTSTREAM_DEBUG
static const int QTEXTSTREAM_BUFFERSIZE = 16384;
@@ -50,6 +14,7 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
\ingroup io
\ingroup string-processing
+ \ingroup qtserialization
\reentrant
QTextStream can operate on a QIODevice, a QByteArray or a
@@ -230,6 +195,9 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
#include "qfile.h"
#include "qnumeric.h"
#include "qvarlengtharray.h"
+#include <private/qdebug_p.h>
+#include <private/qnumeric_p.h>
+#include <private/qtools_p.h>
#include <locale.h>
#include "private/qlocale_p.h"
@@ -239,48 +207,6 @@ static const int QTEXTSTREAM_BUFFERSIZE = 16384;
#include <limits.h>
#include <new>
-#if defined QTEXTSTREAM_DEBUG
-#include <ctype.h>
-#include "private/qtools_p.h"
-
-QT_BEGIN_NAMESPACE
-
-// Returns a human readable representation of the first \a len
-// characters in \a data.
-static QByteArray qt_prettyDebug(const char *data, int len, int maxSize)
-{
- if (!data) return "(null)";
- QByteArray out;
- for (int i = 0; i < len; ++i) {
- char c = data[i];
- if (isprint(int(uchar(c)))) {
- out += c;
- } else switch (c) {
- case '\n': out += "\\n"; break;
- case '\r': out += "\\r"; break;
- case '\t': out += "\\t"; break;
- default: {
- const char buf[] = {
- '\\',
- 'x',
- QtMiscUtils::toHexLower(uchar(c) / 16),
- QtMiscUtils::toHexLower(uchar(c) % 16),
- 0
- };
- out += buf;
- }
- }
- }
-
- if (len < maxSize)
- out += "...";
-
- return out;
-}
-QT_END_NAMESPACE
-
-#endif
-
// A precondition macro
#define Q_VOID
#define CHECK_VALID_STREAM(x) do { \
@@ -320,6 +246,9 @@ QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
+using namespace Qt::StringLiterals;
+using namespace QtMiscUtils;
+
//-------------------------------------------------------------------
/*!
@@ -351,7 +280,7 @@ void QTextStreamPrivate::Params::reset()
realNumberPrecision = 6;
integerBase = 0;
fieldWidth = 0;
- padChar = QLatin1Char(' ');
+ padChar = u' ';
fieldAlignment = QTextStream::AlignRight;
realNumberNotation = QTextStream::SmartNotation;
numberFlags = { };
@@ -368,7 +297,7 @@ void QTextStreamPrivate::reset()
deleteDevice = false;
string = nullptr;
stringOffset = 0;
- stringOpenMode = QIODevice::NotOpen;
+ stringOpenMode = QTextStream::NotOpen;
readBufferOffset = 0;
readBufferStartDevicePos = 0;
@@ -431,10 +360,11 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
if (bytesRead <= 0)
return false;
+#ifndef QT_BOOTSTRAPPED
if (autoDetectUnicode) {
autoDetectUnicode = false;
- auto e = QStringConverter::encodingForData(buf, bytesRead);
+ auto e = QStringConverter::encodingForData(QByteArrayView(buf, bytesRead));
// QStringConverter::Locale implies unknown, so keep the current encoding
if (e) {
encoding = *e;
@@ -445,18 +375,19 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::fillReadBuffer(), using %s encoding", QStringConverter::nameForEncoding(encoding));
#endif
+#endif
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::fillReadBuffer(), device->read(\"%s\", %d) == %d",
- qt_prettyDebug(buf, qMin(32,int(bytesRead)) , int(bytesRead)).constData(), int(sizeof(buf)), int(bytesRead));
+ QtDebugUtils::toPrintable(buf, bytesRead, 32).constData(), int(sizeof(buf)), int(bytesRead));
#endif
int oldReadBufferSize = readBuffer.size();
- readBuffer += toUtf16(buf, bytesRead);
+ readBuffer += toUtf16(QByteArrayView(buf, bytesRead));
// remove all '\r\n' in the string.
if (readBuffer.size() > oldReadBufferSize && textModeEnabled) {
- QChar CR = QLatin1Char('\r');
+ QChar CR = u'\r';
QChar *writePtr = readBuffer.data() + oldReadBufferSize;
QChar *readPtr = readBuffer.data() + oldReadBufferSize;
QChar *endPtr = readBuffer.data() + readBuffer.size();
@@ -486,7 +417,7 @@ bool QTextStreamPrivate::fillReadBuffer(qint64 maxBytes)
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::fillReadBuffer() read %d bytes from device. readBuffer = [%s]", int(bytesRead),
- qt_prettyDebug(readBuffer.toLatin1(), readBuffer.size(), readBuffer.size()).data());
+ QtDebugUtils::toPrintable(readBuffer.toLatin1(), readBuffer.size(), readBuffer.size()).constData());
#endif
return true;
}
@@ -524,7 +455,7 @@ void QTextStreamPrivate::flushWriteBuffer()
bool textModeEnabled = device->isTextModeEnabled();
if (textModeEnabled) {
device->setTextModeEnabled(false);
- writeBuffer.replace(QLatin1Char('\n'), QLatin1String("\r\n"));
+ writeBuffer.replace(u'\n', "\r\n"_L1);
}
#endif
@@ -536,7 +467,7 @@ void QTextStreamPrivate::flushWriteBuffer()
qint64 bytesWritten = device->write(data);
#if defined (QTEXTSTREAM_DEBUG)
qDebug("QTextStreamPrivate::flushWriteBuffer(), device->write(\"%s\") == %d",
- qt_prettyDebug(data.constData(), qMin(data.size(),32), data.size()).constData(), int(bytesWritten));
+ QtDebugUtils::toPrintable(data.constData(), data.size(), 32).constData(), int(bytesWritten));
#endif
#if defined (Q_OS_WIN)
@@ -601,7 +532,6 @@ bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenD
int startOffset = device ? readBufferOffset : stringOffset;
QChar lastChar;
- bool canStillReadFromDevice = true;
do {
int endOffset;
const QChar *chPtr;
@@ -632,9 +562,9 @@ bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenD
}
break;
case EndOfLine:
- if (ch == QLatin1Char('\n')) {
+ if (ch == u'\n') {
foundToken = true;
- delimSize = (lastChar == QLatin1Char('\r')) ? 2 : 1;
+ delimSize = (lastChar == u'\r') ? 2 : 1;
consumeDelimiter = true;
}
lastChar = ch;
@@ -643,7 +573,7 @@ bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenD
}
} while (!foundToken
&& (!maxlen || totalSize < maxlen)
- && (device && (canStillReadFromDevice = fillReadBuffer())));
+ && device && fillReadBuffer());
if (totalSize == 0) {
#if defined (QTEXTSTREAM_DEBUG)
@@ -656,7 +586,7 @@ bool QTextStreamPrivate::scan(const QChar **ptr, int *length, int maxlen, TokenD
// don't make it part of the line.
if (delimiter == EndOfLine && totalSize > 0 && !foundToken) {
if (((string && stringOffset + totalSize == string->size()) || (device && device->atEnd()))
- && lastChar == QLatin1Char('\r')) {
+ && lastChar == u'\r') {
consumeDelimiter = true;
++delimSize;
}
@@ -754,7 +684,7 @@ inline void QTextStreamPrivate::restoreToSavedConverterState()
/*!
\internal
*/
-void QTextStreamPrivate::write(const QChar *data, int len)
+void QTextStreamPrivate::write(const QChar *data, qsizetype len)
{
if (string) {
// ### What about seek()??
@@ -784,7 +714,7 @@ inline void QTextStreamPrivate::write(QChar ch)
/*!
\internal
*/
-void QTextStreamPrivate::write(QLatin1String data)
+void QTextStreamPrivate::write(QLatin1StringView data)
{
if (string) {
// ### What about seek()??
@@ -799,7 +729,7 @@ void QTextStreamPrivate::write(QLatin1String data)
/*!
\internal
*/
-void QTextStreamPrivate::writePadding(int len)
+void QTextStreamPrivate::writePadding(qsizetype len)
{
if (string) {
// ### What about seek()??
@@ -864,7 +794,7 @@ inline void QTextStreamPrivate::putChar(QChar ch)
/*!
\internal
*/
-QTextStreamPrivate::PaddingResult QTextStreamPrivate::padding(int len) const
+QTextStreamPrivate::PaddingResult QTextStreamPrivate::padding(qsizetype len) const
{
Q_ASSERT(params.fieldWidth > len); // calling padding() when no padding is needed is an error
@@ -891,7 +821,7 @@ QTextStreamPrivate::PaddingResult QTextStreamPrivate::padding(int len) const
/*!
\internal
*/
-void QTextStreamPrivate::putString(const QChar *data, int len, bool number)
+void QTextStreamPrivate::putString(const QChar *data, qsizetype len, bool number)
{
if (Q_UNLIKELY(params.fieldWidth > len)) {
@@ -920,7 +850,7 @@ void QTextStreamPrivate::putString(const QChar *data, int len, bool number)
/*!
\internal
*/
-void QTextStreamPrivate::putString(QLatin1String data, bool number)
+void QTextStreamPrivate::putString(QLatin1StringView data, bool number)
{
if (Q_UNLIKELY(params.fieldWidth > data.size())) {
@@ -933,7 +863,7 @@ void QTextStreamPrivate::putString(QLatin1String data, bool number)
if (sign == locale.negativeSign() || sign == locale.positiveSign()) {
// write the sign before the padding, then skip it later
write(&sign, 1);
- data = QLatin1String(data.data() + 1, data.size() - 1);
+ data = QLatin1StringView(data.data() + 1, data.size() - 1);
}
}
@@ -945,6 +875,11 @@ void QTextStreamPrivate::putString(QLatin1String data, bool number)
}
}
+void QTextStreamPrivate::putString(QUtf8StringView data, bool number)
+{
+ putString(data.toString(), number);
+}
+
/*!
Constructs a QTextStream. Before you can use it for reading or
writing, you must assign a device or a string.
@@ -983,7 +918,7 @@ QTextStream::QTextStream(QIODevice *device)
Constructs a QTextStream that operates on \a string, using \a
openMode to define the open mode.
*/
-QTextStream::QTextStream(QString *string, QIODevice::OpenMode openMode)
+QTextStream::QTextStream(QString *string, OpenMode openMode)
: d_ptr(new QTextStreamPrivate(this))
{
#if defined (QTEXTSTREAM_DEBUG)
@@ -1001,7 +936,7 @@ QTextStream::QTextStream(QString *string, QIODevice::OpenMode openMode)
openMode to define the open mode. Internally, the array is wrapped
by a QBuffer.
*/
-QTextStream::QTextStream(QByteArray *array, QIODevice::OpenMode openMode)
+QTextStream::QTextStream(QByteArray *array, OpenMode openMode)
: d_ptr(new QTextStreamPrivate(this))
{
#if defined (QTEXTSTREAM_DEBUG)
@@ -1028,7 +963,7 @@ QTextStream::QTextStream(QByteArray *array, QIODevice::OpenMode openMode)
\snippet code/src_corelib_io_qtextstream.cpp 3
*/
-QTextStream::QTextStream(const QByteArray &array, QIODevice::OpenMode openMode)
+QTextStream::QTextStream(const QByteArray &array, OpenMode openMode)
: d_ptr(new QTextStreamPrivate(this))
{
#if defined (QTEXTSTREAM_DEBUG)
@@ -1059,7 +994,7 @@ QTextStream::QTextStream(const QByteArray &array, QIODevice::OpenMode openMode)
\snippet code/src_corelib_io_qtextstream.cpp 4
*/
-QTextStream::QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode)
+QTextStream::QTextStream(FILE *fileHandle, OpenMode openMode)
: d_ptr(new QTextStreamPrivate(this))
{
#if defined (QTEXTSTREAM_DEBUG)
@@ -1067,7 +1002,10 @@ QTextStream::QTextStream(FILE *fileHandle, QIODevice::OpenMode openMode)
fileHandle, int(openMode));
#endif
QFile *file = new QFile;
- file->open(fileHandle, openMode);
+ // Discarding the return value of open; even if it failed
+ // (and the file is not open), QTextStream still reports `Ok`
+ // for closed QIODevices, so there's nothing really to do here.
+ (void)file->open(fileHandle, openMode);
Q_D(QTextStream);
d->device = file;
@@ -1272,7 +1210,7 @@ QIODevice *QTextStream::device() const
\sa string(), setDevice()
*/
-void QTextStream::setString(QString *string, QIODevice::OpenMode openMode)
+void QTextStream::setString(QString *string, OpenMode openMode)
{
Q_D(QTextStream);
flush();
@@ -1470,7 +1408,8 @@ QTextStream::RealNumberNotation QTextStream::realNumberNotation() const
/*!
Sets the precision of real numbers to \a precision. This value
describes the number of fraction digits QTextStream should
- write when generating real numbers.
+ write when generating real numbers (FixedNotation, ScientificNotation), or
+ the maximum number of significant digits (SmartNotation).
The precision cannot be a negative value. The default value is 6.
@@ -1489,7 +1428,9 @@ void QTextStream::setRealNumberPrecision(int precision)
/*!
Returns the current real number precision, or the number of fraction
- digits QTextStream will write when generating real numbers.
+ digits QTextStream will write when generating real numbers
+ (FixedNotation, ScientificNotation), or the maximum number of significant
+ digits (SmartNotation).
\sa setRealNumberNotation(), realNumberNotation(), numberFlags(), integerBase()
*/
@@ -1683,7 +1624,7 @@ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong
QChar ch;
if (!getChar(&ch))
return npsInvalidPrefix;
- if (ch == QLatin1Char('0')) {
+ if (ch == u'0') {
QChar ch2;
if (!getChar(&ch2)) {
// Result is the number 0
@@ -1692,9 +1633,9 @@ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong
}
ch2 = ch2.toLower();
- if (ch2 == QLatin1Char('x')) {
+ if (ch2 == u'x') {
base = 16;
- } else if (ch2 == QLatin1Char('b')) {
+ } else if (ch2 == u'b') {
base = 2;
} else if (ch2.isDigit() && ch2.digitValue() >= 0 && ch2.digitValue() <= 7) {
base = 8;
@@ -1719,9 +1660,9 @@ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong
case 2: {
QChar pf1, pf2, dig;
// Parse prefix '0b'
- if (!getChar(&pf1) || pf1 != QLatin1Char('0'))
+ if (!getChar(&pf1) || pf1 != u'0')
return npsInvalidPrefix;
- if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('b'))
+ if (!getChar(&pf2) || pf2.toLower() != u'b')
return npsInvalidPrefix;
// Parse digits
int ndigits = 0;
@@ -1747,13 +1688,13 @@ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong
case 8: {
QChar pf, dig;
// Parse prefix '0'
- if (!getChar(&pf) || pf != QLatin1Char('0'))
+ if (!getChar(&pf) || pf != u'0')
return npsInvalidPrefix;
// Parse digits
int ndigits = 0;
while (getChar(&dig)) {
int n = dig.toLower().unicode();
- if (n >= '0' && n <= '7') {
+ if (isOctalDigit(n)) {
val *= 8;
val += n - '0';
} else {
@@ -1810,20 +1751,17 @@ QTextStreamPrivate::NumberParsingStatus QTextStreamPrivate::getNumber(qulonglong
case 16: {
QChar pf1, pf2, dig;
// Parse prefix ' 0x'
- if (!getChar(&pf1) || pf1 != QLatin1Char('0'))
+ if (!getChar(&pf1) || pf1 != u'0')
return npsInvalidPrefix;
- if (!getChar(&pf2) || pf2.toLower() != QLatin1Char('x'))
+ if (!getChar(&pf2) || pf2.toLower() != u'x')
return npsInvalidPrefix;
// Parse digits
int ndigits = 0;
while (getChar(&dig)) {
- int n = dig.toLower().unicode();
- if (n >= '0' && n <= '9') {
+ const int h = fromHex(dig.unicode());
+ if (h != -1) {
val <<= 4;
- val += n - '0';
- } else if (n >= 'a' && n <= 'f') {
- val <<= 4;
- val += 10 + (n - 'a');
+ val += h;
} else {
ungetChar(dig);
break;
@@ -1979,13 +1917,13 @@ bool QTextStreamPrivate::getReal(double *f)
// nan/+inf/-inf, so here we also check for uppercase and mixed
// case versions.
if (!qstricmp(buf, "nan") || !qstricmp(buf, "+nan") || !qstricmp(buf, "-nan")) {
- *f = qQNaN();
+ *f = qt_qnan();
return true;
} else if (!qstricmp(buf, "+inf") || !qstricmp(buf, "inf")) {
- *f = qInf();
+ *f = qt_inf();
return true;
} else if (!qstricmp(buf, "-inf")) {
- *f = -qInf();
+ *f = -qt_inf();
return true;
}
bool ok;
@@ -2017,7 +1955,7 @@ QTextStream &QTextStream::operator>>(QChar &c)
\overload
Reads a character from the stream and stores it in \a c. The
- character from the stream is converted to ISO-5589-1 before it is
+ character from the stream is converted to ISO-8859-1 before it is
stored.
\sa QChar::toLatin1()
@@ -2031,6 +1969,14 @@ QTextStream &QTextStream::operator>>(char &c)
}
/*!
+ \fn QTextStream &QTextStream::operator>>(char16_t &c)
+ \overload
+ \since 6.4
+
+ Reads a character from the stream and stores it in \a c.
+*/
+
+/*!
Reads an integer from the stream and stores it in \a i, then
returns a reference to the QTextStream. The number is cast to
the correct type before it is stored. If no number was detected on
@@ -2186,7 +2132,7 @@ QTextStream &QTextStream::operator>>(QString &str)
/*!
\overload
- Converts the word to ISO-8859-1, then stores it in \a array.
+ Converts the word to UTF-8, then stores it in \a array.
\sa QString::toLatin1()
*/
@@ -2195,7 +2141,6 @@ QTextStream &QTextStream::operator>>(QByteArray &array)
Q_D(QTextStream);
CHECK_VALID_STREAM(*this);
- array.clear();
d->scan(nullptr, nullptr, 0, QTextStreamPrivate::NotSpace);
d->consumeLastToken();
@@ -2203,11 +2148,11 @@ QTextStream &QTextStream::operator>>(QByteArray &array)
int length;
if (!d->scan(&ptr, &length, 0, QTextStreamPrivate::Space)) {
setStatus(ReadPastEnd);
+ array.clear();
return *this;
}
- for (int i = 0; i < length; ++i)
- array += ptr[i].toLatin1();
+ array = QStringView(ptr, length).toUtf8();
d->consumeLastToken();
return *this;
@@ -2216,13 +2161,14 @@ QTextStream &QTextStream::operator>>(QByteArray &array)
/*!
\overload
- Stores the word in \a c, terminated by a '\\0' character. If no word is
- available, only the '\\0' character is stored.
+ Converts the word to UTF-8 and stores it in \a c, terminated by a '\\0'
+ character. If no word is available, only the '\\0' character is stored.
Warning: Although convenient, this operator is dangerous and must
be used with care. QTextStream assumes that \a c points to a
buffer with enough space to hold the word. If the buffer is too
- small, your application may crash.
+ small, your application may crash. For a word consisting of \c{n} QChars,
+ the buffer needs to be at least \c{3*n+1} characters long.
If possible, use the QByteArray operator instead.
*/
@@ -2241,9 +2187,9 @@ QTextStream &QTextStream::operator>>(char *c)
return *this;
}
- for (int i = 0; i < length; ++i)
- *c++ = ptr[i].toLatin1();
- *c = '\0';
+ QStringEncoder encoder(QStringConverter::Utf8);
+ char *e = encoder.appendToBuffer(c, QStringView(ptr, length));
+ *e = '\0';
d->consumeLastToken();
return *this;
}
@@ -2288,8 +2234,8 @@ void QTextStreamPrivate::putNumber(qulonglong number, bool negative)
// workaround for backward compatibility - in octal form with
// ShowBase flag set zero should be written as '00'
if (number == 0 && base == 8 && params.numberFlags & QTextStream::ShowBase
- && result == QLatin1String("0")) {
- result.prepend(QLatin1Char('0'));
+ && result == "0"_L1) {
+ result.prepend(u'0');
}
}
putString(result, true);
@@ -2323,6 +2269,15 @@ QTextStream &QTextStream::operator<<(char c)
}
/*!
+ \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
decimal form, but you can also set the base by calling
@@ -2533,7 +2488,7 @@ QTextStream &QTextStream::operator<<(QStringView string)
Writes \a string to the stream, and returns a reference to the
QTextStream.
*/
-QTextStream &QTextStream::operator<<(QLatin1String string)
+QTextStream &QTextStream::operator<<(QLatin1StringView string)
{
Q_D(QTextStream);
CHECK_VALID_STREAM(*this);
@@ -2551,7 +2506,7 @@ QTextStream &QTextStream::operator<<(const QByteArray &array)
{
Q_D(QTextStream);
CHECK_VALID_STREAM(*this);
- d->putString(QString::fromUtf8(array.constData(), array.length()));
+ d->putString(QString::fromUtf8(array.constData(), array.size()));
return *this;
}
@@ -2559,7 +2514,7 @@ QTextStream &QTextStream::operator<<(const QByteArray &array)
\overload
Writes the constant string pointed to by \a string to the stream. \a
- string is assumed to be in ISO-8859-1 encoding. This operator
+ string is assumed to be in UTF-8 encoding. This operator
is convenient when working with constant string data. Example:
\snippet code/src_corelib_io_qtextstream.cpp 8
@@ -2572,8 +2527,7 @@ QTextStream &QTextStream::operator<<(const char *string)
{
Q_D(QTextStream);
CHECK_VALID_STREAM(*this);
- // ### Qt6: consider changing to UTF-8
- d->putString(QLatin1String(string));
+ d->putString(QUtf8StringView(string));
return *this;
}
@@ -2830,7 +2784,7 @@ QTextStream &scientific(QTextStream &stream)
\since 5.14
- \sa {QTextStream::}{right()}, {QTextStream::}{center()}, {QTextStream manipulators}
+ \sa right(), center(), {QTextStream manipulators}
*/
QTextStream &left(QTextStream &stream)
{
@@ -2844,7 +2798,7 @@ QTextStream &left(QTextStream &stream)
\since 5.14
- \sa {QTextStream::}{left()}, {QTextStream::}{center()}, {QTextStream manipulators}
+ \sa left(), center(), {QTextStream manipulators}
*/
QTextStream &right(QTextStream &stream)
{
@@ -2858,7 +2812,7 @@ QTextStream &right(QTextStream &stream)
\since 5.14
- \sa {QTextStream::}{left()}, {QTextStream::}{right()}, {QTextStream manipulators}
+ \sa left(), right(), {QTextStream manipulators}
*/
QTextStream &center(QTextStream &stream)
{
@@ -2882,7 +2836,7 @@ QTextStream &center(QTextStream &stream)
*/
QTextStream &endl(QTextStream &stream)
{
- return stream << QLatin1Char('\n') << Qt::flush;
+ return stream << '\n'_L1 << Qt::flush;
}
/*!
@@ -2996,7 +2950,7 @@ void QTextStream::setEncoding(QStringConverter::Encoding encoding)
d->encoding = encoding;
d->toUtf16 = QStringDecoder(d->encoding);
- bool generateBOM = d->hasWrittenData && d->generateBOM;
+ bool generateBOM = !d->hasWrittenData && d->generateBOM;
d->fromUtf16 = QStringEncoder(d->encoding,
generateBOM ? QStringEncoder::Flag::WriteBom : QStringEncoder::Flag::Default);
@@ -3050,7 +3004,7 @@ bool QTextStream::autoDetectUnicode() const
device. If \a generate is false, no BOM will be inserted. This function
must be called before any data is written. Otherwise, it does nothing.
- \sa generateByteOrderMark(), bom()
+ \sa generateByteOrderMark(), {Qt::}{bom()}
*/
void QTextStream::setGenerateByteOrderMark(bool generate)
{
@@ -3105,43 +3059,6 @@ QLocale QTextStream::locale() const
return d->locale;
}
-#if QT_DEPRECATED_SINCE(5, 15) && !defined(Q_QDOC)
-// Deprecated source compatible migration versions:
-namespace QTextStreamFunctions {
-QTextStream &bin(QTextStream &s) { return Qt::bin(s); }
-QTextStream &oct(QTextStream &s) { return Qt::oct(s); }
-QTextStream &dec(QTextStream &s) { return Qt::dec(s); }
-QTextStream &hex(QTextStream &s) { return Qt::hex(s); }
-
-QTextStream &showbase(QTextStream &s) { return Qt::showbase(s); }
-QTextStream &forcesign(QTextStream &s) { return Qt::forcesign(s); }
-QTextStream &forcepoint(QTextStream &s) { return Qt::forcepoint(s); }
-QTextStream &noshowbase(QTextStream &s) { return Qt::noshowbase(s); }
-QTextStream &noforcesign(QTextStream &s) { return Qt::noforcesign(s); }
-QTextStream &noforcepoint(QTextStream &s) { return Qt::noforcepoint(s); }
-
-QTextStream &uppercasebase(QTextStream &s) { return Qt::uppercasebase(s); }
-QTextStream &uppercasedigits(QTextStream &s) { return Qt::uppercasedigits(s); }
-QTextStream &lowercasebase(QTextStream &s) { return Qt::lowercasebase(s); }
-QTextStream &lowercasedigits(QTextStream &s) { return Qt::lowercasedigits(s); }
-
-QTextStream &fixed(QTextStream &s) { return Qt::fixed(s); }
-QTextStream &scientific(QTextStream &s) { return Qt::scientific(s); }
-
-QTextStream &left(QTextStream &s) { return Qt::left(s); }
-QTextStream &right(QTextStream &s) { return Qt::right(s); }
-QTextStream &center(QTextStream &s) { return Qt::center(s); }
-
-QTextStream &endl(QTextStream &s) { return Qt::endl(s); }
-QTextStream &flush(QTextStream &s) { return Qt::flush(s); }
-QTextStream &reset(QTextStream &s) { return Qt::reset(s); }
-
-QTextStream &ws(QTextStream &s) { return Qt::ws(s); }
-
-QTextStream &bom(QTextStream &s) { return Qt::bom(s); }
-} // namespace QTextStreamFunctions
-#endif
-
QT_END_NAMESPACE
#ifndef QT_NO_QOBJECT