diff options
author | Dennis Oberst <dennis.oberst@qt.io> | 2023-05-17 10:31:38 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2023-06-02 06:43:31 +0200 |
commit | 18a2c62c073371ab24685d0f98b4b0cc04d11a7e (patch) | |
tree | 0efa8576c1f637aa74a6c67b3d0d839a1e9225f4 /src | |
parent | 1ee7aa741a25bbb9bfe571b0692efaf848c9876e (diff) |
QByteArray: add STL-style assign()
Implemented assign() methods for QByteArray to align with the
criteria of std::basic_string, addressing the previously missing
functionality. This is a subset of the overloads provided by the
standard.
Reference:
https://en.cppreference.com/w/cpp/string/basic_string/assign
[ChangeLog][QtCore][QByteArray] Added assign().
Fixes: QTBUG-106199
Change-Id: I899b14d74e8f774face8690303efb8610ead95b5
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qbytearray.cpp | 67 | ||||
-rw-r--r-- | src/corelib/text/qbytearray.h | 21 |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 4e6cce49c0..267d8a3371 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2105,6 +2105,73 @@ QByteArray& QByteArray::append(char ch) } /*! + \fn QByteArray &QByteArray::assign(QByteArrayView v) + \since 6.6 + + Replaces the contents of this byte array with a copy of \a v and returns a + reference to this byte array. + + The size of this byte array will be equal to the size of \a v. + + This function only allocates memory if the size of \a v exceeds the capacity + of this byte array or this byte array is shared. +*/ + +/*! + \fn QByteArray &QByteArray::assign(qsizetype n, char c) + \since 6.6 + + Replaces the contents of this byte array with \a n copies of \a c and + returns a reference to this byte array. + + The size of this byte array will be equal to \a n, which has to be non-negative. + + This function will only allocate memory if \a n exceeds the capacity of this + byte array or this byte array is shared. + + \sa fill() +*/ + +/*! + \fn template <typename InputIterator, if_input_iterator<InputIterator>> QByteArray &QByteArray::assign(InputIterator first, InputIterator last) + \since 6.6 + + Replaces the contents of this byte array with a copy of the elements in the + iterator range [\a first, \a last) and returns a reference to this + byte array. + + The size of this byte array will be equal to the number of elements in the + range [\a first, \a last). + + This function will only allocate memory if the number of elements in the + range exceeds the capacity of this byte array or this byte array is shared. + + \note This function overload only participates in overload resolution if + \c InputIterator meets the requirements of a + \l {https://en.cppreference.com/w/cpp/named_req/InputIterator} {LegacyInputIterator}. + + \note The behavior is undefined if either argument is an iterator into *this or + [\a first, \a last) is not a valid range. +*/ + +QByteArray &QByteArray::assign(QByteArrayView v) +{ + const auto len = v.size(); + + if (len <= capacity() && isDetached()) { + const auto offset = d.freeSpaceAtBegin(); + if (offset) + d.setBegin(d.begin() - offset); + std::memcpy(d.begin(), v.data(), len); + d.size = len; + d.data()[d.size] = '\0'; + } else { + *this = v.toByteArray(); + } + return *this; +} + +/*! Inserts \a data at index position \a i and returns a reference to this byte array. diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 2c15545ecd..972e2174d3 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -40,6 +40,8 @@ namespace emscripten { } #endif +class tst_QByteArray; + QT_BEGIN_NAMESPACE class QString; @@ -60,6 +62,11 @@ private: DataPointer d; static const char _empty; + + friend class ::tst_QByteArray; + + template <typename InputIterator> + using if_input_iterator = QtPrivate::IfIsInputIterator<InputIterator>; public: enum Base64Option { @@ -227,6 +234,20 @@ public: QByteArray &append(QByteArrayView a) { return insert(size(), a); } + QByteArray &assign(QByteArrayView v); + QByteArray &assign(qsizetype n, char c) + { + Q_ASSERT(n >= 0); + return fill(c, n); + } + template <typename InputIterator, if_input_iterator<InputIterator> = true> + QByteArray &assign(InputIterator first, InputIterator last) + { + d.assign(first, last); + d.data()[d.size] = '\0'; + return *this; + } + QByteArray &insert(qsizetype i, QByteArrayView data); inline QByteArray &insert(qsizetype i, const char *s) { return insert(i, QByteArrayView(s)); } |