Fix compilation on MSVC
authorLars Knoll <lars.knoll@nokia.com>
Wed, 13 Jul 2011 08:21:23 +0000 (10:21 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 13 Jul 2011 08:38:59 +0000 (10:38 +0200)
MSVC apparently doesn't like the way the QString::Data
pointers got initialized.

Also fixed a few warnings about signed/unsigned conversions.

Change-Id: I1267979af7601129e5483f8785d4982a1f2f8182
Reviewed-on: http://codereview.qt.nokia.com/1558
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@nokia.com>
src/corelib/tools/qstring.cpp
src/corelib/tools/qstring.h

index dab281c..b5efc85 100644 (file)
@@ -1051,7 +1051,11 @@ QString::QString(const QChar *unicode, int size)
     } else {
         d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
         Q_CHECK_PTR(d);
-        *d = (Data){ Q_REFCOUNT_INITIALIZER(1), size, size, false, { 0 } };
+        d->ref = 1;
+        d->size = size;
+        d->alloc = (uint) size;
+        d->capacityReserved = false;
+        d->offset = 0;
         memcpy(d->data(), unicode, size * sizeof(QChar));
         d->data()[size] = '\0';
     }
@@ -1079,7 +1083,11 @@ QString::QString(const QChar *unicode)
          } else {
              d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
              Q_CHECK_PTR(d);
-             *d = (Data){ Q_REFCOUNT_INITIALIZER(1), size, size, false, { 0 } };
+             d->ref = 1;
+             d->size = size;
+             d->alloc = (uint) size;
+             d->capacityReserved = false;
+             d->offset = 0;
              memcpy(d->data(), unicode, size * sizeof(QChar));
              d->data()[size] = '\0';
          }
@@ -1100,7 +1108,11 @@ QString::QString(int size, QChar ch)
     } else {
         d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
         Q_CHECK_PTR(d);
-        *d = (Data){ Q_REFCOUNT_INITIALIZER(1), size, size, false, { 0 } };
+        d->ref = 1;
+        d->size = size;
+        d->alloc = (uint) size;
+        d->capacityReserved = false;
+        d->offset = 0;
         d->data()[size] = '\0';
         ushort *i = d->data() + size;
         ushort *b = d->data();
@@ -1120,7 +1132,11 @@ QString::QString(int size, Qt::Initialization)
 {
     d = (Data*) qMalloc(sizeof(Data)+(size+1)*sizeof(QChar));
     Q_CHECK_PTR(d);
-    *d = (Data){ Q_REFCOUNT_INITIALIZER(1), size, size, false, { 0 } };
+    d->ref = 1;
+    d->size = size;
+    d->alloc = (uint) size;
+    d->capacityReserved = false;
+    d->offset = 0;
     d->data()[size] = '\0';
 }
 
@@ -1138,7 +1154,11 @@ QString::QString(QChar ch)
 {
     d = (Data *) qMalloc(sizeof(Data) + 2*sizeof(QChar));
     Q_CHECK_PTR(d);
-    *d = (Data) { Q_REFCOUNT_INITIALIZER(1), 1, 1, false, { 0 } };
+    d->ref = 1;
+    d->size = 1;
+    d->alloc = 1;
+    d->capacityReserved = false;
+    d->offset = 0;
     d->data()[0] = ch.unicode();
     d->data()[1] = '\0';
 }
@@ -1313,7 +1333,11 @@ void QString::realloc(int alloc)
     if (d->ref != 1 || d->offset) {
         Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + (alloc+1) * sizeof(QChar)));
         Q_CHECK_PTR(x);
-        *x = (Data){ Q_REFCOUNT_INITIALIZER(1), qMin(alloc, d->size), alloc, d->capacityReserved, { 0 } };
+        x->ref = 1;
+        x->size = qMin(alloc, d->size);
+        x->alloc = (uint) alloc;
+        x->capacityReserved = d->capacityReserved;
+        x->offset =0;
         ::memcpy(x->data(), d->data(), x->size * sizeof(QChar));
         x->data()[x->size] = 0;
         if (!d->ref.deref())
@@ -3747,7 +3771,11 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size)
             size = qstrlen(str);
         d = static_cast<Data *>(qMalloc(sizeof(Data) + (size+1) * sizeof(QChar)));
         Q_CHECK_PTR(d);
-        *d = (Data){ Q_REFCOUNT_INITIALIZER(1), size, size, false, { 0 } };
+        d->ref = 1;
+        d->size = size;
+        d->alloc = (uint) size;
+        d->capacityReserved = false;
+        d->offset = 0;
         d->data()[size] = '\0';
         ushort *dst = d->data();
         /* SIMD:
@@ -7071,13 +7099,19 @@ bool QString::isRightToLeft() const
 */
 QString QString::fromRawData(const QChar *unicode, int size)
 {
-    Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
-    *x = (Data){ Q_REFCOUNT_INITIALIZER(1), size, 0, false, { 0 } };
-    Q_CHECK_PTR(x);
-    if (unicode) {
-        x->offset = (const ushort *)unicode - (x->d + sizeof(qptrdiff)/sizeof(ushort));
+    Data *x;
+    if (!unicode) {
+        x = const_cast<Data *>(&shared_null.str);
+    } else if (!size) {
+        x = const_cast<Data *>(&shared_empty.str);
     } else {
-        size = 0;
+        x = static_cast<Data *>(qMalloc(sizeof(Data) + sizeof(ushort)));
+        Q_CHECK_PTR(x);
+        x->ref = 1;
+        x->size = size;
+        x->alloc = 0;
+        x->capacityReserved = false;
+        x->offset = (const ushort *)unicode - (x->d + sizeof(qptrdiff)/sizeof(ushort));
     }
     return QString(x, 0);
 }
index 27948e0..45ff002 100644 (file)
@@ -185,7 +185,7 @@ public:
 
     int capacity() const;
     inline void reserve(int size);
-    inline void squeeze() { if (d->size < d->alloc || d->ref != 1) realloc(); d->capacityReserved = false;}
+    inline void squeeze() { if (d->size < (int)d->alloc || d->ref != 1) realloc(); d->capacityReserved = false;}
 
     inline const QChar *unicode() const;
     inline QChar *data();
@@ -337,7 +337,7 @@ public:
     inline QString &prepend(const QLatin1String &s) { return insert(0, s); }
 
     inline QString &operator+=(QChar c) {
-        if (d->ref != 1 || d->size + 1 > d->alloc)
+        if (d->ref != 1 || d->size + 1 > (int)d->alloc)
             realloc(grow(d->size + 1));
         d->data()[d->size++] = c.unicode();
         d->data()[d->size] = '\0';
@@ -633,7 +633,7 @@ public:
 class Q_CORE_EXPORT QLatin1String
 {
 public:
-    inline explicit QLatin1String(const char *s) : m_size(s ? strlen(s) : 0), m_data(s) {}
+    inline explicit QLatin1String(const char *s) : m_size(s ? (int)strlen(s) : 0), m_data(s) {}
 
     inline const char *latin1() const { return m_data; }
     inline int size() const { return m_size; }
@@ -842,7 +842,7 @@ inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
 
 inline QString::QString() : d(const_cast<Data *>(&shared_null.str)) {}
 inline QString::~QString() { if (!d->ref.deref()) free(d); }
-inline void QString::reserve(int asize) { if (d->ref != 1 || asize > d->alloc) realloc(asize); d->capacityReserved = true;}
+inline void QString::reserve(int asize) { if (d->ref != 1 || asize > (int)d->alloc) realloc(asize); d->capacityReserved = true;}
 inline QString &QString::setUtf16(const ushort *autf16, int asize)
 { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
 inline QCharRef QString::operator[](int i)