diff options
| author | Ahmad Samir <a.samirh78@gmail.com> | 2023-06-15 02:39:19 +0300 |
|---|---|---|
| committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-08-03 02:12:22 +0300 |
| commit | 8ed2bc919432aaa2f7304b1a6207c056365cc22c (patch) | |
| tree | c9ac5d58080050dff184e0fd3344dbed82043563 | |
| parent | d1da83002d18040f8ac21ebf6542ff71cb462509 (diff) | |
QByteArray: change append(QByteArray) to match QStringBuilder behavior
I.e. concatenating a null byte array and an empty-but-not-null byte
array should result in an empty-but-not-null byte array.
This matches the behavior of QString::append(QString) too.
Fixes: QTBUG-114238
Pick-to: 6.6
Change-Id: Id36d10ee09c08041b7dabda102df48ca6d413d8b
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
| -rw-r--r-- | src/corelib/text/qbytearray.cpp | 11 | ||||
| -rw-r--r-- | tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp | 18 |
2 files changed, 26 insertions, 3 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 7f8349131d8..42921e99d76 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2044,9 +2044,14 @@ QByteArray &QByteArray::prepend(const QByteArray &ba) QByteArray &QByteArray::append(const QByteArray &ba) { - if (size() == 0 && ba.size() > d->freeSpaceAtEnd() && ba.d.isMutable()) - return (*this = ba); - return append(QByteArrayView(ba)); + if (!ba.isNull()) { + if (isNull()) { + operator=(ba); + } else if (ba.size()) { + append(QByteArrayView(ba)); + } + } + return *this; } /*! diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index bc84e256514..f5e149836ab 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -53,6 +53,7 @@ private slots: void append(); void appendExtended_data(); void appendExtended(); + void appendEmptyNull(); void assign(); void assignShared(); void assignUsesPrependBuffer(); @@ -937,6 +938,23 @@ void tst_QByteArray::appendExtended() QCOMPARE(array.size(), 11); } +void tst_QByteArray::appendEmptyNull() +{ + QByteArray a; + QVERIFY(a.isEmpty()); + QVERIFY(a.isNull()); + + QByteArray b(""); + QVERIFY(b.isEmpty()); + QVERIFY(!b.isNull()); + + // Concatenating a null and an empty-but-not-null byte arrays results in + // an empty but not null byte array + QByteArray r = a + b; + QVERIFY(r.isEmpty()); + QVERIFY(!r.isNull()); +} + void tst_QByteArray::assign() { // QByteArray &assign(QByteArrayView) |
