OSDN Git Service

effectively revert QString changes
authorIvailo Monev <xakepa10@laimg.moc>
Sat, 27 Jul 2019 21:53:56 +0000 (21:53 +0000)
committerIvailo Monev <xakepa10@laimg.moc>
Sat, 27 Jul 2019 21:53:56 +0000 (21:53 +0000)
that includes:
https://github.com/fluxer/katie/commit/53b54fc8a63c4301f07ca5efbe9d8df8cd4739d5
https://github.com/fluxer/katie/commit/3e4c0955a908bc8f75c30494041feb3355cadf30

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
src/core/tools/qstring.cpp
src/core/tools/qstring.h

index 3d34843..540fff7 100644 (file)
@@ -633,9 +633,9 @@ static int findChar(const QChar *str, int len, QChar ch, int from,
 */
 
 QString::Data QString::shared_null = { QAtomicInt(1),
-                                       0, 0, shared_null.array, {0} };
+                                       0, 0, 0, shared_null.array, {0} };
 QString::Data QString::shared_empty = { QAtomicInt(1),
-                                        0, 0, shared_empty.array, {0} };
+                                        0, 0, 0, shared_empty.array, {0} };
 
 int QString::grow(int size)
 {
@@ -891,7 +891,8 @@ QString::QString(const QChar *unicode, int size)
         d = (Data*) malloc(sizeof(Data)+size*sizeof(QChar));
         Q_CHECK_PTR(d);
         d->ref = 1;
-        d->capacity = d->size = size;
+        d->alloc = d->size = size;
+        d->capacity = 0;
         d->data = d->array;
         memcpy(d->array, unicode, size * sizeof(QChar));
         d->array[size] = '\0';
@@ -913,7 +914,8 @@ QString::QString(const int size, const QChar ch)
         d = (Data*) malloc(sizeof(Data)+size*sizeof(QChar));
         Q_CHECK_PTR(d);
         d->ref = 1;
-        d->capacity = d->size = size;
+        d->alloc = d->size = size;
+        d->capacity = 0;
         d->data = d->array;
         d->array[size] = '\0';
         ushort *i = d->array + size;
@@ -935,7 +937,8 @@ QString::QString(int size, Qt::Initialization)
     d = (Data*) ::malloc(sizeof(Data)+size*sizeof(QChar));
     Q_CHECK_PTR(d);
     d->ref = 1;
-    d->capacity = d->size = size;
+    d->alloc = d->size = size;
+    d->capacity = 0;
     d->data = d->array;
     d->array[size] = '\0';
 }
@@ -956,7 +959,8 @@ QString::QString(const QChar ch)
     Q_CHECK_PTR(buf);
     d = reinterpret_cast<Data *>(buf);
     d->ref = 1;
-    d->capacity = d->size = 1;
+    d->alloc = d->size = 1;
+    d->capacity = 0;
     d->data = d->array;
     d->array[0] = ch.unicode();
     d->array[1] = '\0';
@@ -1055,16 +1059,17 @@ void QString::resize(int size)
     if (size < 0)
         size = 0;
 
-    if (size == 0) {
+    if (size == 0 && !d->capacity) {
         Data *x = &shared_empty;
         x->ref.ref();
         if (!d->ref.deref())
             QString::freeData(d);
         d = x;
     } else {
-        if (d->ref != 1 || size != d->capacity)
-            reallocData(size);
-        if (d->capacity >= size) {
+        if (d->ref != 1 || size > d->alloc ||
+            (!d->capacity && size < d->size && size < d->alloc >> 1))
+            reallocData(grow(size));
+        if (d->alloc >= size) {
             d->size = size;
             if (d->data == d->array) {
                 d->array[size] = '\0';
@@ -1132,7 +1137,8 @@ void QString::reallocData(int alloc)
         ::memcpy(x->array, d->data, x->size * sizeof(QChar));
         x->array[x->size] = 0;
         x->ref = 1;
-        x->capacity = alloc;
+        x->alloc = alloc;
+        x->capacity = d->capacity;
         x->data = x->array;
         if (!d->ref.deref())
             QString::freeData(d);
@@ -1141,7 +1147,7 @@ void QString::reallocData(int alloc)
         Data *p = static_cast<Data *>(realloc(d, sizeof(Data) + alloc * sizeof(QChar)));
         Q_CHECK_PTR(p);
         d = p;
-        d->capacity = alloc;
+        d->alloc = alloc;
         d->data = d->array;
     }
 }
@@ -1289,7 +1295,7 @@ QString& QString::insert(int i, const QChar *unicode, int size)
         return *this;
 
     const ushort *s = (const ushort *)unicode;
-    if (s >= d->data && s < d->data + d->capacity) {
+    if (s >= d->data && s < d->data + d->alloc) {
         // Part of me - take a copy
         ushort *tmp = static_cast<ushort *>(malloc(size * sizeof(QChar)));
         Q_CHECK_PTR(tmp);
@@ -1349,7 +1355,7 @@ QString &QString::append(const QString &str)
         if (d == &shared_null) {
             operator=(str);
         } else {
-            if (d->ref != 1 || d->size + str.d->size > d->capacity)
+            if (d->ref != 1 || d->size + str.d->size > d->alloc)
                 reallocData(grow(d->size + str.d->size));
             memcpy(d->data + d->size, str.d->data, str.d->size * sizeof(QChar));
             d->size += str.d->size;
@@ -1369,7 +1375,7 @@ QString &QString::append(const QLatin1String &str)
     const uchar *s = (const uchar *)str.latin1();
     if (s) {
         int len = qstrlen((char *)s);
-        if (d->ref != 1 || d->size + len > d->capacity)
+        if (d->ref != 1 || d->size + len > d->alloc)
             reallocData(grow(d->size + len));
         ushort *i = d->data + d->size;
         while ((*i++ = *s++))
@@ -1412,7 +1418,7 @@ QString &QString::append(const QLatin1String &str)
 */
 QString &QString::append(QChar ch)
 {
-    if (d->ref != 1 || d->size + 1 > d->capacity)
+    if (d->ref != 1 || d->size + 1 > d->alloc)
         reallocData(grow(d->size + 1));
     d->data[d->size++] = ch.unicode();
     d->data[d->size] = '\0';
@@ -3485,7 +3491,8 @@ QString::Data *QString::fromLatin1_helper(const char *str, int size)
         d = static_cast<Data *>(malloc(sizeof(Data) + size * sizeof(QChar)));
         Q_CHECK_PTR(d);
         d->ref = 1;
-        d->capacity = d->size = size;
+        d->alloc = d->size = size;
+        d->capacity = 0;
         d->data = d->array;
         d->array[size] = '\0';
         ushort *dst = d->data;
@@ -5710,7 +5717,7 @@ QString QString::repeated(int times) const
     const int resultSize = times * d->size;
 
     QString result(resultSize, Qt::Uninitialized);
-    if (result.d->capacity != resultSize)
+    if (result.d->alloc != resultSize)
         return QString(); // not enough memory
 
     memcpy(result.d->data, d->data, d->size * sizeof(ushort));
@@ -6601,8 +6608,9 @@ QString QString::fromRawData(const QChar *unicode, int size)
         size = 0;
     }
     x->ref = 1;
-    x->capacity = x->size = size;
+    x->alloc = x->size = size;
     *x->array = '\0';
+    x->capacity = 0;
     return QString(x, 0);
 }
 
@@ -6622,7 +6630,7 @@ QString QString::fromRawData(const QChar *unicode, int size)
 */
 QString &QString::setRawData(const QChar *unicode, int size)
 {
-    if (d->ref != 1 || (d->data == d->array && d->capacity)) {
+    if (d->ref != 1 || (d->data == d->array && d->alloc)) {
         *this = fromRawData(unicode, size);
     } else {
         if (unicode) {
@@ -6631,8 +6639,9 @@ QString &QString::setRawData(const QChar *unicode, int size)
             d->data = d->array;
             size = 0;
         }
-        d->capacity = d->size = size;
+        d->alloc = d->size = size;
         *d->array = '\0';
+        d->capacity = 0;
     }
     return *this;
 }
index a30bef4..26f44b4 100644 (file)
@@ -91,7 +91,7 @@ public:
 
     int capacity() const;
     inline void reserve(int size);
-    inline void squeeze() { if (d->size < d->capacity || d->ref != 1) reallocData(d->size);}
+    inline void squeeze() { if (d->size < d->alloc || d->ref != 1) reallocData(d->size); d->capacity = 0;}
 
     inline const QChar *unicode() const;
     inline QChar *data();
@@ -246,7 +246,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->capacity)
+        if (d->ref != 1 || d->size + 1 > d->alloc)
             reallocData(grow(d->size + 1));
         d->data[d->size++] = c.unicode();
         d->data[d->size] = '\0';
@@ -485,7 +485,7 @@ private:
 
     struct Data {
         QAtomicInt ref;
-        int size, capacity;
+        int alloc, size, capacity;
         ushort *data;
         ushort array[1];
     };
@@ -599,7 +599,7 @@ inline void QString::clear()
 inline QString::QString(const QString &other) : d(other.d)
 { Q_ASSERT(&other != this); d->ref.ref(); }
 inline int QString::capacity() const
-{ return d->capacity; }
+{ return d->alloc; }
 inline QString &QString::setNum(short n, int base)
 { return setNum(qlonglong(n), base); }
 inline QString &QString::setNum(ushort n, int base)
@@ -731,7 +731,7 @@ inline void QCharRef::setCell(uchar acell) { QChar(*this).setCell(acell); }
 
 inline QString::QString() : d(&shared_null) { d->ref.ref(); }
 inline QString::~QString() { if (!d->ref.deref()) freeData(d); }
-inline void QString::reserve(int asize) { if (d->ref != 1 || asize > d->capacity) reallocData(asize);}
+inline void QString::reserve(int asize) { if (d->ref != 1 || asize > d->alloc) reallocData(asize); d->capacity = 1;}
 inline QString &QString::setUtf16(const ushort *autf16, int asize)
 { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); }
 inline QCharRef QString::operator[](int i)