diff --git a/src/corelib/serialization/qcborstreamwriter.cpp b/src/corelib/serialization/qcborstreamwriter.cpp index 950c08270d4..07e62ed4cf4 100644 --- a/src/corelib/serialization/qcborstreamwriter.cpp +++ b/src/corelib/serialization/qcborstreamwriter.cpp @@ -419,6 +419,18 @@ void QCborStreamWriter::append(QCborNegativeInteger n) QCborStreamReader::readByteArray() */ +/*! + \fn void QCborStreamWriter::append(QUtf8StringView str) + \since 6.10 + \overload + + Appends the UTF-8 string viewed by \a str to the stream, creating a CBOR + Text String value. QCborStreamWriter will attempt to write the entire string + in one chunk. + + \sa appendTextString(), QCborStreamReader::isString(), QCborStreamReader::readString() +*/ + /*! \overload @@ -435,7 +447,7 @@ void QCborStreamWriter::append(QCborNegativeInteger n) determine whether the contents are US-ASCII or not. If the string is found to contain characters outside of US-ASCII, it will allocate memory and convert to UTF-8. If this check is unnecessary, use appendTextString() - instead. + instead or the overload taking a \l QUtf8StringView. \sa QCborStreamReader::isString(), QCborStreamReader::readString() */ diff --git a/src/corelib/serialization/qcborstreamwriter.h b/src/corelib/serialization/qcborstreamwriter.h index dbc340e0b35..be072564e28 100644 --- a/src/corelib/serialization/qcborstreamwriter.h +++ b/src/corelib/serialization/qcborstreamwriter.h @@ -49,6 +49,7 @@ public: void append(QByteArrayView ba) { appendByteString(ba.data(), ba.size()); } void append(QLatin1StringView str); void append(QStringView str); + void append(QUtf8StringView str) { appendTextString(str.data(), str.size()); } void append(QCborTag tag); void append(QCborKnownTags tag) { append(QCborTag(tag)); } void append(QCborSimpleType st); diff --git a/tests/auto/corelib/serialization/qcborstreamwriter/tst_qcborstreamwriter.cpp b/tests/auto/corelib/serialization/qcborstreamwriter/tst_qcborstreamwriter.cpp index a0ce4a93a6a..d05868b3890 100644 --- a/tests/auto/corelib/serialization/qcborstreamwriter/tst_qcborstreamwriter.cpp +++ b/tests/auto/corelib/serialization/qcborstreamwriter/tst_qcborstreamwriter.cpp @@ -189,6 +189,7 @@ void tst_QCborStreamWriter::nonAsciiStrings() // will be wrong if !isLatin1 QByteArray latin1 = input.toLatin1(); + QByteArray utf8 = input.toUtf8(); if (useDevice) { { @@ -199,6 +200,14 @@ void tst_QCborStreamWriter::nonAsciiStrings() QCOMPARE(buffer.data(), output); } + { + QBuffer buffer; + buffer.open(QIODevice::WriteOnly); + QCborStreamWriter writer(&buffer); + writer.append(QUtf8StringView(utf8)); + QCOMPARE(buffer.data(), output); + } + if (isLatin1) { QBuffer buffer; buffer.open(QIODevice::WriteOnly); @@ -214,6 +223,13 @@ void tst_QCborStreamWriter::nonAsciiStrings() QCOMPARE(buffer, output); } + { + QByteArray buffer; + QCborStreamWriter writer(&buffer); + writer.append(QUtf8StringView(utf8)); + QCOMPARE(buffer, output); + } + if (isLatin1) { QByteArray buffer; QCborStreamWriter writer(&buffer);