From f72cbe39e06df893124695c05421cbe505abbd25 Mon Sep 17 00:00:00 2001 From: Kevin Funk Date: Thu, 13 Aug 2015 20:58:42 +0200 Subject: QByteArray: Add append/prepend/insert overload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use-case is fast insertion of copies of a character, avoiding any temporary heap allocations Change-Id: Ie5517d88429fbd4c58dbe5729de7c468d5d9a279 Reviewed-by: Oswald Buddenhagen Reviewed-by: Sérgio Martins Reviewed-by: Thiago Macieira --- src/corelib/tools/qbytearray.cpp | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/corelib/tools/qbytearray.cpp') diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 447e84b3d4..241c255b5d 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -1713,6 +1713,14 @@ QByteArray &QByteArray::prepend(const char *str, int len) return *this; } +/*! \fn QByteArray &QByteArray::prepend(int count, char ch) + + \overload + \since 5.7 + + Prepends \a count copies of character \a ch to this byte array. +*/ + /*! \overload @@ -1825,6 +1833,17 @@ QByteArray &QByteArray::append(const char *str, int len) return *this; } +/*! \fn QByteArray &QByteArray::append(int count, char ch) + + \overload + \since 5.7 + + Appends \a count copies of character \a ch to this byte + array and returns a reference to this byte array. + + If \a count is negative or zero nothing is appended to the byte array. +*/ + /*! \overload @@ -1941,6 +1960,33 @@ QByteArray &QByteArray::insert(int i, char ch) return qbytearray_insert(this, i, &ch, 1); } +/*! \fn QByteArray &QByteArray::insert(int i, int count, char ch) + + \overload + \since 5.7 + + Inserts \a count copies of character \a ch at index position \a i in the + byte array. + + If \a i is greater than size(), the array is first extended using resize(). +*/ + +QByteArray &QByteArray::insert(int i, int count, char ch) +{ + if (i < 0 || count <= 0) + return *this; + + int oldsize = size(); + resize(qMax(i, oldsize) + count); + char *dst = d->data(); + if (i > oldsize) + ::memset(dst + oldsize, 0x20, i - oldsize); + else if (i < oldsize) + ::memmove(dst + i + count, dst + i, oldsize - i); + ::memset(dst + i, ch, count); + return *this; +} + /*! Removes \a len bytes from the array, starting at index position \a pos, and returns a reference to the array. -- cgit v1.2.3