diff options
author | Marc Mutz <marc.mutz@qt.io> | 2022-04-04 15:20:51 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-04-06 10:44:40 +0000 |
commit | a00a1d8806cfbf17e04b88d1b4ff4a9cf5b6294a (patch) | |
tree | 393f227915629c65ca4d63230b0b6f45f2acf341 /src/corelib/text | |
parent | 2fb7c94f63ce783c7f36149791fe72e053933ece (diff) |
QByteArray/QVarLengthArray: add missing resize(n, v) overloads
QList and QString had them, so add them to QByteArray and
QVarLengthArray, too.
In the QVLA case, we need to jump though a hoop or two to avoid having
to duplicate all the reallocation logic. Nothing a few template tricks
cannot solve.
[ChangeLog][QtCore][QByteArray] Added resize(n, ch) overload.
[ChangeLog][QtCore][QVarLengthArray] Added resize(n, v) overload.
Fixes: QTBUG-102270
Change-Id: I0d281ae5b574f440f682e4a62427b434dcf5b687
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 25 | ||||
-rw-r--r-- | src/corelib/text/qbytearray.h | 1 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index d9518c9e8f..7675d7f4a2 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1760,6 +1760,31 @@ void QByteArray::resize(qsizetype size) } /*! + \since 6.4 + + Sets the size of the byte array to \a newSize bytes. + + If \a newSize is greater than the current size, the byte array is + extended to make it \a newSize bytes with the extra bytes added to + the end. The new bytes are initialized to \a c. + + If \a newSize is less than the current size, bytes beyond position + \a newSize are excluded from the byte array. + + \note While resize() will grow the capacity if needed, it never shrinks + capacity. To shed excess capacity, use squeeze(). + + \sa size(), truncate(), squeeze() +*/ +void QByteArray::resize(qsizetype newSize, char c) +{ + const auto old = d.size; + resize(newSize); + if (old < d.size) + memset(d.data() + old, c, d.size - old); +} + +/*! Sets every byte in the byte array to \a ch. If \a size is different from -1 (the default), the byte array is resized to size \a size beforehand. diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 2f0b099db9..75fb75fd35 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -128,6 +128,7 @@ public: bool isEmpty() const noexcept { return size() == 0; } void resize(qsizetype size); + void resize(qsizetype size, char c); QByteArray &fill(char c, qsizetype size = -1); |