summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.h
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2012-04-04 15:36:09 +0200
committerQt by Nokia <qt-info@nokia.com>2012-04-16 15:50:53 +0200
commit2ac4c463afdb8a62b0abb8ba444f34ca50e8979c (patch)
tree56be42941a0221e1aa9d325fb7d7d5ac0557873e /src/corelib/tools/qstring.h
parent77fd8fd9977b987c99acba6417ac369a9975d989 (diff)
Make QByteArray and QString keep track of terminating null
In conceptual terms, this change increments the Data::alloc member by one for all strings allocated and maintained by these classes. Instances initialized with fromRawData retain 0 as the member value, so they are treated as immutable. This brings QByteArray and QString closer to QVector, making it possible for them to reference and share the same data in memory, in the future. It also brings them closer to QArrayData. In practical terms all comparisons to the alloc member were changed to take into account that it also tracks the terminating null character. Aside from the increment in the alloc member, there should be no user visible changes. Change-Id: I618f49022a9b1845754500c8f8706c72a68b9c7d Reviewed-by: João Abecasis <joao.abecasis@nokia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qstring.h')
-rw-r--r--src/corelib/tools/qstring.h8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index a9f2484de6..edb140b682 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -369,7 +369,7 @@ public:
inline QString &prepend(const QLatin1String &s) { return insert(0, s); }
inline QString &operator+=(QChar c) {
- if (d->ref.isShared() || d->size + 1 > int(d->alloc))
+ if (d->ref.isShared() || uint(d->size) + 2u > d->alloc)
reallocData(uint(d->size) + 2u, true);
d->data()[d->size++] = c.unicode();
d->data()[d->size] = '\0';
@@ -754,7 +754,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->alloc; }
+{ return d->alloc ? d->alloc - 1 : 0; }
inline QString &QString::setNum(short n, int base)
{ return setNum(qlonglong(n), base); }
inline QString &QString::setNum(ushort n, int base)
@@ -906,7 +906,7 @@ inline QString::~QString() { if (!d->ref.deref()) free(d); }
inline void QString::reserve(int asize)
{
- if (d->ref.isShared() || asize > int(d->alloc))
+ if (d->ref.isShared() || uint(asize) + 1u > d->alloc)
reallocData(uint(asize) + 1u);
if (!d->capacityReserved) {
@@ -917,7 +917,7 @@ inline void QString::reserve(int asize)
inline void QString::squeeze()
{
- if (d->ref.isShared() || d->size < int(d->alloc))
+ if (d->ref.isShared() || uint(d->size) + 1u < d->alloc)
reallocData(uint(d->size) + 1u);
if (d->capacityReserved) {