*/
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)
{
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';
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;
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';
}
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';
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';
::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);
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;
}
}
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);
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;
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++))
*/
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';
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;
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));
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);
}
*/
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) {
d->data = d->array;
size = 0;
}
- d->capacity = d->size = size;
+ d->alloc = d->size = size;
*d->array = '\0';
+ d->capacity = 0;
}
return *this;
}
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();
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';
struct Data {
QAtomicInt ref;
- int size, capacity;
+ int alloc, size, capacity;
ushort *data;
ushort array[1];
};
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)
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)