get rid of QByteArray::shared_empty and QString::shared_empty

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2019-12-27 10:49:10 +00:00
parent 7ef1c71404
commit 259252b337
6 changed files with 23 additions and 65 deletions

View file

@ -639,10 +639,7 @@ QByteArray qFastUncompress(const char* data, int nbytes)
}
#endif // QT_NO_COMPRESS
QByteArray::Data QByteArray::shared_null = { QAtomicInt(1),
0, 0, shared_null.array, {0} };
QByteArray::Data QByteArray::shared_empty = { QAtomicInt(1),
0, 0, shared_empty.array, {0} };
QByteArray::Data QByteArray::shared_null = { QAtomicInt(1), 0, 0, shared_null.array, {0} };
/*!
\class QByteArray
@ -944,10 +941,8 @@ QByteArray &QByteArray::operator=(const QByteArray & other)
QByteArray &QByteArray::operator=(const char *str)
{
Data *x;
if (!str) {
if (!str || !*str) {
x = &shared_null;
} else if (!*str) {
x = &shared_empty;
} else {
int len = qstrlen(str);
if (d->ref != 1 || len > d->alloc || (len < d->size && len < d->alloc >> 1))
@ -1343,10 +1338,8 @@ void QByteArray::chop(int n)
QByteArray::QByteArray(const char *data)
{
int size = qstrlen(data);
if (!data) {
if (!data || size <= 0) {
d = &shared_null;
} else if (size <= 0) {
d = &shared_empty;
} else {
d = static_cast<Data *>(malloc(sizeof(Data) + size));
Q_CHECK_PTR(d);
@ -1372,10 +1365,8 @@ QByteArray::QByteArray(const char *data)
QByteArray::QByteArray(const char *data, int size)
{
if (!data) {
if (!data || size <= 0) {
d = &shared_null;
} else if (size <= 0) {
d = &shared_empty;
} else {
d = static_cast<Data *>(malloc(sizeof(Data) + size));
Q_CHECK_PTR(d);
@ -1420,7 +1411,7 @@ QByteArray::QByteArray(int size, char ch)
QByteArray::QByteArray(int size, Qt::Initialization)
{
if (size <= 0) {
d = &shared_empty;
d = &shared_null;
} else {
d = static_cast<Data *>(malloc(sizeof(Data)+size));
Q_CHECK_PTR(d);
@ -1448,28 +1439,11 @@ QByteArray::QByteArray(int size, Qt::Initialization)
void QByteArray::resize(int size)
{
if (size <= 0) {
Data *x = &shared_empty;
Data *x = &shared_null;
x->ref.ref();
if (!d->ref.deref())
freeData(d);
d = x;
} else if (d == &shared_null) {
//
// Optimize the idiom:
// QByteArray a;
// a.resize(sz);
// ...
// which is used in place of the Qt 3 idiom:
// QByteArray a(sz);
//
Data *x = static_cast<Data *>(malloc(sizeof(Data)+size));
Q_CHECK_PTR(x);
x->ref = 1;
x->alloc = x->size = size;
x->data = x->array;
x->array[size] = '\0';
(void) d->ref.deref(); // cannot be 0, x points to shared_null
d = x;
} else {
if (d->ref != 1 || size != d->size)
reallocData(size);
@ -1575,7 +1549,7 @@ QByteArray QByteArray::nulTerminated() const
QByteArray &QByteArray::prepend(const QByteArray &ba)
{
if ((d == &shared_null || d == &shared_empty) && !IS_RAW_DATA(ba.d)) {
if (d == &shared_null && !IS_RAW_DATA(ba.d)) {
*this = ba;
} else if (ba.d != &shared_null) {
QByteArray tmp = *this;
@ -1659,7 +1633,7 @@ QByteArray &QByteArray::prepend(char ch)
QByteArray &QByteArray::append(const QByteArray &ba)
{
if ((d == &shared_null || d == &shared_empty) && !IS_RAW_DATA(ba.d)) {
if (d == &shared_null && !IS_RAW_DATA(ba.d)) {
*this = ba;
} else if (ba.d != &shared_null) {
if (d->ref != 1 || d->size + ba.d->size > d->alloc)
@ -2667,7 +2641,7 @@ QByteArray QByteArray::right(int len) const
QByteArray QByteArray::mid(int pos, int len) const
{
if (d == &shared_null || d == &shared_empty || pos >= d->size)
if (d == &shared_null || pos >= d->size)
return QByteArray();
if (len < 0)
len = d->size - pos;
@ -3169,8 +3143,7 @@ QByteArray QByteArray::trimmed() const
}
int l = end - start + 1;
if (l <= 0) {
shared_empty.ref.ref();
return QByteArray(&shared_empty, 0, 0);
return QByteArray();
}
return QByteArray(s+start, l);
}

View file

@ -299,7 +299,6 @@ public:
private:
operator bool() const;
static Data shared_null;
static Data shared_empty;
Data *d;
QByteArray(Data *dd, int /*dummy*/, int /*dummy*/) : d(dd) {}
static void freeData(Data *);

View file

@ -587,8 +587,6 @@ 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)
{
@ -834,12 +832,9 @@ int QString::toWCharArray(wchar_t *array) const
*/
QString::QString(const QChar *unicode, int size)
{
if (!unicode) {
if (!unicode || size <= 0) {
d = &shared_null;
d->ref.ref();
} else if (size <= 0) {
d = &shared_empty;
d->ref.ref();
} else {
d = (Data*) malloc(sizeof(Data)+size*sizeof(QChar));
Q_CHECK_PTR(d);
@ -861,7 +856,7 @@ QString::QString(const QChar *unicode, int size)
QString::QString(const int size, const QChar ch)
{
if (size <= 0) {
d = &shared_empty;
d = &shared_null;
d->ref.ref();
} else {
d = (Data*) malloc(sizeof(Data)+size*sizeof(QChar));
@ -973,7 +968,7 @@ QString::QString(const QChar ch)
void QString::freeData(Data *d)
{
if(d != &shared_null && d != &shared_empty)
if(d != &shared_null)
free(d);
}
@ -1013,7 +1008,7 @@ void QString::resize(int size)
size = 0;
if (size == 0 && !d->capacity) {
Data *x = &shared_empty;
Data *x = &shared_null;
x->ref.ref();
if (!d->ref.deref())
QString::freeData(d);
@ -3432,12 +3427,9 @@ QVector<uint> QString::toUcs4() const
QString::Data *QString::fromLatin1_helper(const char *str, int size)
{
Data *d;
if (!str) {
if (!str || size == 0 || (!*str && size < 0)) {
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);
@ -3460,12 +3452,9 @@ QString::Data *QString::fromAscii_helper(const char *str, int size)
#ifndef QT_NO_TEXTCODEC
if (codecForCStrings) {
Data *d;
if (!str) {
if (!str || size == 0 || (!*str && size < 0)) {
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);
@ -3702,8 +3691,7 @@ QString QString::simplified() const
break;
if (++from == fromEnd) {
// All-whitespace string
shared_empty.ref.ref();
return QString(&shared_empty, 0);
return QString();
}
}
// This loop needs no underflow check, as we already determined that
@ -3796,8 +3784,7 @@ QString QString::trimmed() const
}
int l = end - start + 1;
if (l <= 0) {
shared_empty.ref.ref();
return QString(&shared_empty, 0);
return QString();
}
return QString(s + start, l);
}

View file

@ -491,7 +491,6 @@ 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

View file

@ -729,12 +729,12 @@ QKeySequence::QKeySequence(StandardKey key)
/*!
Constructs an empty key sequence.
Constructs an null key sequence.
*/
QKeySequence::QKeySequence()
{
static QKeySequencePrivate shared_empty;
d = &shared_empty;
static QKeySequencePrivate shared_null;
d = &shared_null;
d->ref.ref();
}

View file

@ -184,7 +184,7 @@ struct QWExtra {
QPointer<QStyle> style;
QPointer<QWidget> focus_proxy;
// Implicit pointers (shared_empty/shared_null).
// Implicit pointers (shared_null).
QRegion mask; // widget mask
QString styleSheet;
@ -549,7 +549,7 @@ public:
static QWidgetMapper *mapper;
static QWidgetSet *allWidgets;
// Implicit pointers (shared_null/shared_empty).
// Implicit pointers (shared_null).
QRegion opaqueChildren;
QRegion dirty;
#ifndef QT_NO_TOOLTIP