diff options
author | Markus Goetz <Markus.Goetz@nokia.com> | 2009-09-03 11:48:45 (GMT) |
---|---|---|
committer | Markus Goetz <Markus.Goetz@nokia.com> | 2009-09-03 11:54:55 (GMT) |
commit | b36b2605961afef44c78c47b06feb64bedbd3563 (patch) | |
tree | 0ad2297571fc873a6288aebc3ab8504e88334aad | |
parent | 98aee81ccc17c0fe5d7e013fc40ca171dc2ebb43 (diff) | |
download | Qt-b36b2605961afef44c78c47b06feb64bedbd3563.zip Qt-b36b2605961afef44c78c47b06feb64bedbd3563.tar.gz Qt-b36b2605961afef44c78c47b06feb64bedbd3563.tar.bz2 |
QByteArray: Two new functions
We had append(str,len) before and now we also have insert(index,str,len)
and prepend(str,len).
Task-number: 247881
Reviewed-by: Thiago
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 29 | ||||
-rw-r--r-- | src/corelib/tools/qbytearray.h | 2 | ||||
-rw-r--r-- | tests/auto/qbytearray/tst_qbytearray.cpp | 8 |
3 files changed, 38 insertions, 1 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 611fc58..145b631 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -1467,8 +1467,19 @@ QByteArray &QByteArray::prepend(const QByteArray &ba) QByteArray &QByteArray::prepend(const char *str) { + return prepend(str, qstrlen(str)); +} + +/*! + \overload + \since 4.6 + + Prepends \a len bytes of the string \a str to this byte array. +*/ + +QByteArray &QByteArray::prepend(const char *str, int len) +{ if (str) { - int len = qstrlen(str); if (d->ref != 1 || d->size + len > d->alloc) realloc(qAllocMore(d->size + len, sizeof(Data))); memmove(d->data+len, d->data, d->size); @@ -1679,6 +1690,22 @@ QByteArray &QByteArray::insert(int i, const char *str) /*! \overload + \since 4.6 + + Inserts \a len bytes of the string \a str at 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, const char *str, int len) +{ + return qbytearray_insert(this, i, str, len); +} + +/*! + \overload Inserts 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 diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index f7e790d..34dd44f 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -224,6 +224,7 @@ public: QByteArray &prepend(char c); QByteArray &prepend(const char *s); + QByteArray &prepend(const char *s, int len); QByteArray &prepend(const QByteArray &a); QByteArray &append(char c); QByteArray &append(const char *s); @@ -231,6 +232,7 @@ public: QByteArray &append(const QByteArray &a); QByteArray &insert(int i, char c); QByteArray &insert(int i, const char *s); + QByteArray &insert(int i, const char *s, int len); QByteArray &insert(int i, const QByteArray &a); QByteArray &remove(int index, int len); QByteArray &replace(int index, int len, const char *s); diff --git a/tests/auto/qbytearray/tst_qbytearray.cpp b/tests/auto/qbytearray/tst_qbytearray.cpp index dfb2fe1..cce4d7d 100644 --- a/tests/auto/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/qbytearray/tst_qbytearray.cpp @@ -710,6 +710,7 @@ void tst_QByteArray::prepend() QCOMPARE(ba.prepend("1"), QByteArray("1foo")); QCOMPARE(ba.prepend(QByteArray("2")), QByteArray("21foo")); QCOMPARE(ba.prepend('3'), QByteArray("321foo")); + QCOMPARE(ba.prepend("\0 ", 2), QByteArray::fromRawData("\0 321foo", 8)); } void tst_QByteArray::append() @@ -720,6 +721,9 @@ void tst_QByteArray::append() QCOMPARE(ba.append("1"), QByteArray("foo1")); QCOMPARE(ba.append(QByteArray("2")), QByteArray("foo12")); QCOMPARE(ba.append('3'), QByteArray("foo123")); + QCOMPARE(ba.append("\0"), QByteArray("foo123")); + QCOMPARE(ba.append("\0", 1), QByteArray::fromRawData("foo123\0", 7)); + QCOMPARE(ba.size(), 7); } void tst_QByteArray::insert() @@ -738,6 +742,10 @@ void tst_QByteArray::insert() ba = "ikl"; QCOMPARE(ba.insert(1, "j"), QByteArray("ijkl")); QCOMPARE(ba.size(), 4); + + ba = "ab"; + QCOMPARE(ba.insert(1, "\0X\0", 3), QByteArray::fromRawData("a\0X\0b", 5)); + QCOMPARE(ba.size(), 5); } void tst_QByteArray::remove_data() |