From bbff940f4971962f18dda8c4d46a70e58776e52d Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Thu, 25 Mar 2021 23:42:51 +0200 Subject: [PATCH] Revert "get rid of QString::shared_empty member" This reverts commit 93d76d14e764cc191cbb7786dcdeda3fb75f4aa4. --- src/core/tools/qstring.cpp | 31 +++++++++++++++++++++---------- src/core/tools/qstring.h | 1 + 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/core/tools/qstring.cpp b/src/core/tools/qstring.cpp index 6057db23c..361cc8020 100644 --- a/src/core/tools/qstring.cpp +++ b/src/core/tools/qstring.cpp @@ -576,6 +576,8 @@ static int findChar(const QChar *str, int len, QChar ch, int from, QString::Data QString::shared_null = { QAtomicInt(1), 0, 0, 0, shared_null.array, {0} }; +QString::Data QString::shared_empty = { QAtomicInt(1), + 0, 0, 0, shared_empty.array, {0} }; int QString::grow(int size) { @@ -821,9 +823,12 @@ int QString::toWCharArray(wchar_t *array) const */ QString::QString(const QChar *unicode, int size) { - if (!unicode || size <= 0) { + if (!unicode) { d = &shared_null; d->ref.ref(); + } else if (size <= 0) { + d = &shared_empty; + d->ref.ref(); } else { d = static_cast(::malloc(sizeof(Data)+size*sizeof(QChar))); Q_CHECK_PTR(d); @@ -845,7 +850,7 @@ QString::QString(const QChar *unicode, int size) QString::QString(const int size, const QChar ch) { if (size <= 0) { - d = &shared_null; + d = &shared_empty; d->ref.ref(); } else { d = static_cast(::malloc(sizeof(Data)+size*sizeof(QChar))); @@ -956,8 +961,8 @@ QString::QString(const QChar ch) void QString::freeData(Data *d) { - if(d != &shared_null) - ::free(d); + if(d != &shared_null && d != &shared_empty) + free(d); } /*! @@ -996,7 +1001,7 @@ void QString::resize(int size) size = 0; if (size == 0 && !d->capacity) { - Data *x = &shared_null; + Data *x = &shared_empty; x->ref.ref(); if (!d->ref.deref()) QString::freeData(d); @@ -1287,7 +1292,7 @@ QString& QString::insert(int i, QChar ch) */ QString &QString::append(const QString &str) { - if (str.d != &shared_null) { + if (str.d != &shared_null && str.d != &shared_empty) { 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)); @@ -3126,7 +3131,7 @@ QString QString::right(int n) const QString QString::mid(int position, int n) const { - if (d == &shared_null || position >= d->size) + if (d == &shared_null || d == &shared_empty || position >= d->size) return QString(); if (n < 0) n = d->size - position; @@ -3403,9 +3408,12 @@ QVector QString::toUcs4() const QString::Data *QString::fromLatin1_helper(const char *str, int size) { Data *d; - if (!str || size == 0 || (!*str && size < 0)) { + if (!str) { d = &shared_null; d->ref.ref(); + } else if (size == 0 || (!*str && size < 0)) { + d = &shared_empty; + d->ref.ref(); } else { if (size < 0) size = qstrlen(str); @@ -3428,9 +3436,12 @@ QString::Data *QString::fromAscii_helper(const char *str, int size) #ifndef QT_NO_TEXTCODEC if (codecForCStrings) { Data *d; - if (!str || size == 0 || (!*str && size < 0)) { + if (!str) { d = &shared_null; d->ref.ref(); + } else if (size == 0 || (!*str && size < 0)) { + d = &shared_empty; + d->ref.ref(); } else { if (size < 0) size = qstrlen(str); @@ -7692,7 +7703,7 @@ QStringRef QString::rightRef(int n) const QStringRef QString::midRef(int position, int n) const { - if (d == &shared_null || position >= d->size) + if (d == &shared_null || d == &shared_empty || position >= d->size) return QStringRef(); if (n < 0) n = d->size - position; diff --git a/src/core/tools/qstring.h b/src/core/tools/qstring.h index b208f9ce2..fff674a70 100644 --- a/src/core/tools/qstring.h +++ b/src/core/tools/qstring.h @@ -477,6 +477,7 @@ private: ushort array[1]; }; static Data shared_null; + static Data shared_empty; Data *d; QString(Data *dd, int /*dummy*/) : d(dd) {} #ifndef QT_NO_TEXTCODEC