From cfaed64873809b94f18f3291fa29817ee74bfedc Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 1 Feb 2024 21:22:08 +0100 Subject: [PATCH] QByteArray/QString: add resizeForOverwrite For these classes it's not really a new feature, but exposing the current resize() behavior (which does uninitialized resizes) under a proper name. Changing the existing behavior for resize() is a behavioral break that we can only likely afford in Qt 7. [ChangeLog][QtCore][QString] Added resizeForOverwrite(). [ChangeLog][QtCore][QByteArray] Added resizeForOverwrite(). Change-Id: I15b3104aee2bc29d23e91d97b0e64f87612d0099 Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 15 +++++++++++++++ src/corelib/text/qbytearray.h | 1 + src/corelib/text/qstring.cpp | 18 ++++++++++++++++++ src/corelib/text/qstring.h | 1 + 4 files changed, 35 insertions(+) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 1eb5c5b2d25..3df77f179c3 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1896,6 +1896,21 @@ void QByteArray::resize(qsizetype newSize, char c) memset(d.data() + old, c, d.size - old); } +/*! + \since 6.8 + + Resizes the byte array to \a size bytes. If the size of the + byte array grows, the new bytes are uninitialized. + + The behavior is identical to \c{resize(size)}. + + \sa resize() +*/ +void QByteArray::resizeForOverwrite(qsizetype size) +{ + resize(size); +} + /*! 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 ad4264036b5..f057fa0bb78 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -105,6 +105,7 @@ public: bool isEmpty() const noexcept { return size() == 0; } void resize(qsizetype size); void resize(qsizetype size, char c); + void resizeForOverwrite(qsizetype size); QByteArray &fill(char c, qsizetype size = -1); diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 95dfd09abd7..b95c7d22b77 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2680,6 +2680,24 @@ void QString::resize(qsizetype newSize, QChar fillChar) std::fill_n(d.data() + oldSize, difference, fillChar.unicode()); } + +/*! + \since 6.8 + + Sets the size of the string to \a size characters. If the size of + the string grows, the new characters are uninitialized. + + The behavior is identical to \c{resize(size)}. + + \sa resize() +*/ + +void QString::resizeForOverwrite(qsizetype size) +{ + resize(size); +} + + /*! \fn qsizetype QString::capacity() const Returns the maximum number of characters that can be stored in diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 218a7730056..8216cd51746 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -190,6 +190,7 @@ public: inline bool isEmpty() const noexcept { return d.size == 0; } void resize(qsizetype size); void resize(qsizetype size, QChar fillChar); + void resizeForOverwrite(qsizetype size); QString &fill(QChar c, qsizetype size = -1); void truncate(qsizetype pos);