Skip to content

Commit

Permalink
QByteArray/QString: add resizeForOverwrite
Browse files Browse the repository at this point in the history
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 <thiago.macieira@intel.com>
  • Loading branch information
dangelog committed Feb 16, 2024
1 parent 73bf1c1 commit cfaed64
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/corelib/text/qbytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/corelib/text/qbytearray.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class Q_CORE_EXPORT QByteArray
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);

Expand Down
18 changes: 18 additions & 0 deletions src/corelib/text/qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/corelib/text/qstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class Q_CORE_EXPORT QString
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);
Expand Down

0 comments on commit cfaed64

Please sign in to comment.