summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.cpp
diff options
context:
space:
mode:
authorhjk <qtc-committer@nokia.com>2009-05-28 11:18:00 (GMT)
committerhjk <qtc-committer@nokia.com>2009-05-28 11:38:55 (GMT)
commit3331605529d2d81145259fd56d03bb59c480cac5 (patch)
tree4c716283b4fa714479a4f923019eaebd948de88a /src/corelib/tools/qstring.cpp
parent389ca4b5931efbf2bcac050ec80ec7971b61c857 (diff)
downloadQt-3331605529d2d81145259fd56d03bb59c480cac5.zip
Qt-3331605529d2d81145259fd56d03bb59c480cac5.tar.gz
Qt-3331605529d2d81145259fd56d03bb59c480cac5.tar.bz2
Introduce a new class QStringBuilder to speed up the creation of
QString objects from smaller chunks. The QStringBuilder class: QStringBuilder uses expression templates (using the '%' operator) to postpone any actual concatenation until it is assigned to an actual QString. At that time it knows the exact sizes of all chunks, can compute the required space, allocates once a QString of appriopriate size and then copies over the chunk data one-by-one. In addition, QLatin1Literal is a drop-in replacement for QLatin1String (which we can't change for compatibility reasons) that knows its size, therefore saving a few cycles when computing the size of the resulting string. Some further saved cycles stem from inlining and reduced reference counting logic (the QString created from a QStringBuilder has typically ref count equal to 1, while QString::append() needs an extra test) Minor changes to the existing QString class: - Introduce QString constructor to create an uninitialized QString of a given size. This particular constructor is used by QStringBuilder class. - Introduce a QT_USE_FAST_CONCATENATION macro to disable the existing overloads of operator+() and helps finding the places where they are used in code. - Introduce QT_USE_FAST_OPERATOR_PLUS. This also disables the existing overloads of operator+() and creates a new templated operator+() with identical implementation of operator%(). This allows code that is compilable QT_CAST_{TO,FROM}_ASCII to use QStringBuilder almost transparently. The only case that is not covered is creating objects like QUrl that are implicitly constructible from a QString from a QStringBuilder result. This needs to be converted explicitly to a QString first, e.g. by using QUrl url(QString(QLatin1String("http://") + hostName)); Reviewed-by: MariusSO
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r--src/corelib/tools/qstring.cpp17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 29509c5..c64e1d6 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -944,6 +944,23 @@ QString::QString(int size, QChar ch)
}
}
+/*!
+ Constructs a string of the given \a size without initializing the
+ characters. This is only used in \c QStringBuilder::toString().
+
+ \internal
+*/
+
+QString::QString(int size, Uninitialized)
+{
+ d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
+ d->ref = 1;
+ d->alloc = d->size = size;
+ d->clean = d->asciiCache = d->simpletext = d->righttoleft = d->capacity = 0;
+ d->data = d->array;
+ d->array[size] = '\0';
+}
+
/*! \fn QString::QString(const QLatin1String &str)
Constructs a copy of the Latin-1 string \a str.