Remove explicit checks for shared_null/empty
authorJoão Abecasis <joao.abecasis@nokia.com>
Fri, 6 Apr 2012 07:24:37 +0000 (09:24 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 16 Apr 2012 13:50:59 +0000 (15:50 +0200)
This eases porting to QArrayData and retains semantics. In append() and
prepend() a conditional was generalized so that no work is done if there
is nothing to add.

Done-with: Jędrzej Nowacki
Change-Id: Ib9e7bb572801b2434fa040cde2bf66dacf595f22
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@nokia.com>
src/corelib/tools/qbytearray.cpp

index 1e1fb79..13b5230 100644 (file)
@@ -1412,7 +1412,7 @@ void QByteArray::resize(int size)
         if (!d->ref.deref())
             free(d);
         d = x;
-    } else if (d == &shared_null.ba || d == &shared_empty.ba) {
+    } else if (d->size == 0 && d->ref.isStatic()) {
         //
         // Optimize the idiom:
         //    QByteArray a;
@@ -1536,9 +1536,9 @@ QByteArray QByteArray::nulTerminated() const
 
 QByteArray &QByteArray::prepend(const QByteArray &ba)
 {
-    if ((d == &shared_null.ba || d == &shared_empty.ba) && !IS_RAW_DATA(ba.d)) {
+    if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
         *this = ba;
-    } else if (ba.d != &shared_null.ba) {
+    } else if (ba.d->size != 0) {
         QByteArray tmp = *this;
         *this = ba;
         append(tmp);
@@ -1620,9 +1620,9 @@ QByteArray &QByteArray::prepend(char ch)
 
 QByteArray &QByteArray::append(const QByteArray &ba)
 {
-    if ((d == &shared_null.ba || d == &shared_empty.ba) && !IS_RAW_DATA(ba.d)) {
+    if (d->size == 0 && d->ref.isStatic() && !IS_RAW_DATA(ba.d)) {
         *this = ba;
-    } else if (ba.d != &shared_null.ba) {
+    } else if (ba.d->size != 0) {
         if (d->ref.isShared() || uint(d->size + ba.d->size) + 1u > d->alloc)
             reallocData(uint(d->size + ba.d->size) + 1u, true);
         memcpy(d->data() + d->size, ba.d->data(), ba.d->size);
@@ -2663,7 +2663,7 @@ QByteArray QByteArray::right(int len) const
 
 QByteArray QByteArray::mid(int pos, int len) const
 {
-    if (d == &shared_null.ba || d == &shared_empty.ba || pos > d->size)
+    if ((d->size == 0 && d->ref.isStatic()) || pos > d->size)
         return QByteArray();
     if (len < 0)
         len = d->size - pos;