diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/serialization/qtextstream.cpp | 30 | ||||
-rw-r--r-- | src/corelib/serialization/qtextstream_p.h | 1 |
2 files changed, 18 insertions, 13 deletions
diff --git a/src/corelib/serialization/qtextstream.cpp b/src/corelib/serialization/qtextstream.cpp index 68ead1068b..3c69f3398f 100644 --- a/src/corelib/serialization/qtextstream.cpp +++ b/src/corelib/serialization/qtextstream.cpp @@ -945,6 +945,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. @@ -2186,7 +2191,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 +2200,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 +2207,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 +2220,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 +2246,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, ptr, length); + *e = '\0'; d->consumeLastToken(); return *this; } @@ -2559,7 +2564,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 +2577,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; } diff --git a/src/corelib/serialization/qtextstream_p.h b/src/corelib/serialization/qtextstream_p.h index 067a5ec94a..7d6889aa70 100644 --- a/src/corelib/serialization/qtextstream_p.h +++ b/src/corelib/serialization/qtextstream_p.h @@ -177,6 +177,7 @@ public: inline void putString(const QString &ch, bool number = false) { putString(ch.constData(), ch.length(), number); } void putString(const QChar *data, int len, bool number = false); void putString(QLatin1String data, bool number = false); + void putString(QUtf8StringView data, bool number = false); inline void putChar(QChar ch); void putNumber(qulonglong number, bool negative); |