From 59a081dc7e08073d2b18a78bb526d324d79be2d2 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 13 Feb 2025 08:22:26 -0800 Subject: [PATCH] QCborStreamWriter: add append(QUtf8StringView) And test appendTextString() through it, which was apparently not directly tested until now (only through appending Latin1 and UTF-16 strings). [ChangeLog][QtCore][QCborStreamWriter] Added a QUtf8StringView overload of append(). Change-Id: I4d306ab1561a4fd6e9a8fffd65f352bdb78b96b9 Reviewed-by: Ahmad Samir Reviewed-by: Thiago Macieira --- src/corelib/serialization/qcborstreamwriter.cpp | 14 +++++++++++++- src/corelib/serialization/qcborstreamwriter.h | 1 + .../qcborstreamwriter/tst_qcborstreamwriter.cpp | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) 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);