summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/text/qbytearray.cpp8
-rw-r--r--src/corelib/text/qbytearray.h3
-rw-r--r--tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp12
3 files changed, 20 insertions, 3 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index 459e00887d..1ff61ed11a 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -1784,6 +1784,12 @@ QByteArray QByteArray::nulTerminated() const
Prepends \a ba to this byte array.
*/
+QByteArray &QByteArray::prepend(const QByteArray &ba)
+{
+ if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
+ return (*this = ba);
+ return prepend(QByteArrayView(ba));
+}
/*!
\fn QByteArray &QByteArray::prepend(const char *str)
@@ -1842,7 +1848,7 @@ QByteArray QByteArray::nulTerminated() const
QByteArray &QByteArray::append(const QByteArray &ba)
{
- if (size() == 0 && ba.d.isMutable())
+ if (size() == 0 && ba.size() > d.constAllocatedCapacity() && ba.d.isMutable())
return (*this = ba);
return append(QByteArrayView(ba));
}
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 60f1bd01f5..c2d65e2c55 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -283,8 +283,7 @@ public:
{ return insert(0, QByteArrayView(s, qsizetype(qstrlen(s)))); }
QByteArray &prepend(const char *s, qsizetype len)
{ return insert(0, QByteArrayView(s, len)); }
- QByteArray &prepend(const QByteArray &a)
- { return insert(0, a); }
+ QByteArray &prepend(const QByteArray &a);
QByteArray &prepend(QByteArrayView a)
{ return insert(0, a); }
diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
index 2f0f5654ca..cf70a7bbaa 100644
--- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
@@ -838,6 +838,12 @@ void tst_QByteArray::prepend()
QCOMPARE(ba.prepend(-1, 'x'), QByteArray("321foo"));
QCOMPARE(ba.prepend(3, 'x'), QByteArray("xxx321foo"));
QCOMPARE(ba.prepend("\0 ", 2), QByteArray::fromRawData("\0 xxx321foo", 11));
+
+ QByteArray tenChars;
+ tenChars.reserve(10);
+ QByteArray twoChars("ab");
+ tenChars.prepend(twoChars);
+ QCOMPARE(tenChars.capacity(), 10);
}
void tst_QByteArray::prependExtended_data()
@@ -882,6 +888,12 @@ void tst_QByteArray::append()
QCOMPARE(ba.append("\0"), QByteArray("foo123xxx"));
QCOMPARE(ba.append("\0", 1), QByteArray::fromRawData("foo123xxx\0", 10));
QCOMPARE(ba.size(), 10);
+
+ QByteArray tenChars;
+ tenChars.reserve(10);
+ QByteArray twoChars("ab");
+ tenChars.append(twoChars);
+ QCOMPARE(tenChars.capacity(), 10);
}
void tst_QByteArray::appendExtended_data()