summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.cpp
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2012-04-04 13:50:21 +0200
committerQt by Nokia <qt-info@nokia.com>2012-04-05 01:44:34 +0200
commite5d10b2a3be77244d8db3a4dac86168ee9b91e6f (patch)
treee9f2a63e957d95fd2f10a62c772aaa993bdd9f79 /src/corelib/tools/qstring.cpp
parent7488b79652cb18de3e2b295835c08a46f3e286f4 (diff)
Move growth computation to re-allocation function
Callers of QByteArray/QString::realloc() are still responsible for the heuristics and decide whether to provide the "grow" hint, but computation is centralized there. With this change we also ensure growth takes into account the terminating null. Previously, calls to qAllocMore took into account header and string size, for left out the null, meaning we ended up allocating ("nice-size" + Null). Change-Id: Iad1536e7706cd2d446daee96859db9b01c5f9680 Reviewed-by: Marius Storm-Olsen <marius.storm-olsen@nokia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r--src/corelib/tools/qstring.cpp18
1 files changed, 8 insertions, 10 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index c5c43673bc..123d332bad 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -799,11 +799,6 @@ const QString::Null QString::null = { };
const QStaticStringData<1> QString::shared_null = { Q_STATIC_STRING_DATA_HEADER_INITIALIZER(0), { 0 } };
const QStaticStringData<1> QString::shared_empty = { Q_STATIC_STRING_DATA_HEADER_INITIALIZER(0), { 0 } };
-int QString::grow(int size)
-{
- return qAllocMore(size * sizeof(QChar), sizeof(Data)) / sizeof(QChar);
-}
-
/*! \typedef QString::ConstIterator
Qt-style synonym for QString::const_iterator.
@@ -1247,7 +1242,7 @@ void QString::resize(int size)
} else {
if (d->ref.isShared() || size > int(d->alloc) ||
(!d->capacityReserved && size < d->size && size < int(d->alloc) >> 1))
- realloc(grow(size));
+ realloc(size, true);
if (int(d->alloc) >= size) {
d->size = size;
d->data()[size] = '\0';
@@ -1306,8 +1301,11 @@ void QString::resize(int size)
*/
// ### Qt 5: rename reallocData() to avoid confusion. 197625
-void QString::realloc(int alloc)
+void QString::realloc(int alloc, bool grow)
{
+ if (grow)
+ alloc = qAllocMore((alloc+1) * sizeof(QChar), sizeof(Data)) / sizeof(QChar) - 1;
+
if (d->ref.isShared() || IS_RAW_DATA(d)) {
Data *x = static_cast<Data *>(::malloc(sizeof(Data) + (alloc+1) * sizeof(QChar)));
Q_CHECK_PTR(x);
@@ -1534,7 +1532,7 @@ QString &QString::append(const QString &str)
operator=(str);
} else {
if (d->ref.isShared() || d->size + str.d->size > int(d->alloc))
- realloc(grow(d->size + str.d->size));
+ realloc(d->size + str.d->size, true);
memcpy(d->data() + d->size, str.d->data(), str.d->size * sizeof(QChar));
d->size += str.d->size;
d->data()[d->size] = '\0';
@@ -1554,7 +1552,7 @@ QString &QString::append(const QLatin1String &str)
if (s) {
int len = str.size();
if (d->ref.isShared() || d->size + len > int(d->alloc))
- realloc(grow(d->size + len));
+ realloc(d->size + len, true);
ushort *i = d->data() + d->size;
while ((*i++ = *s++))
;
@@ -1597,7 +1595,7 @@ QString &QString::append(const QLatin1String &str)
QString &QString::append(QChar ch)
{
if (d->ref.isShared() || d->size + 1 > int(d->alloc))
- realloc(grow(d->size + 1));
+ realloc(d->size + 1, true);
d->data()[d->size++] = ch.unicode();
d->data()[d->size] = '\0';
return *this;