From 1b1b484184d818fb43c4ff3837b88b0383ccb936 Mon Sep 17 00:00:00 2001 From: hjk <qtc-committer@nokia.com> Date: Wed, 27 May 2009 16:49:45 +0200 Subject: [PATCH] qstringbuilder: final version to merge into Qt proper --- src/libs/utils/qstringbuilder.h | 40 ++++++++++-------------- tests/benchmarks/qstringbuilder/main.cpp | 4 +-- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/libs/utils/qstringbuilder.h b/src/libs/utils/qstringbuilder.h index 7a12ba6c386..3368742e1ce 100644 --- a/src/libs/utils/qstringbuilder.h +++ b/src/libs/utils/qstringbuilder.h @@ -53,10 +53,6 @@ QT_BEGIN_NAMESPACE QT_MODULE(Core) -// Using this relies on changing the QString::QString(QChar *, int) -// constructor to allocated an unitialized string of the given size. -//#define USE_CHANGED_QSTRING 1 - class QLatin1Literal { public: @@ -74,35 +70,33 @@ private: namespace { +template <typename T> class QConcatenable {}; + template <typename A, typename B> class QStringBuilder { public: QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {} - operator QString() const; -public: + operator QString() const + { + #if 0 + QString s(QConcatenable< QStringBuilder<A, B> >::size(*this), + QString::Uninitialized()); + #else + QString s; + s.resize(QConcatenable< QStringBuilder<A, B> >::size(*this)); + #endif + + QChar *d = s.data(); + QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d); + return s; + } + const A &a; const B &b; }; -// make sure the operator% defined below acts only on types we want to handle. -template <typename T> struct QConcatenable {}; - - -template <typename A, typename B> -QStringBuilder<A, B>::operator QString() const -{ -#ifdef USE_CHANGED_QSTRING - QString s(this->size(), QChar(-1)); -#else - QString s; - s.resize(QConcatenable< QStringBuilder<A, B> >::size(*this)); -#endif - QChar *d = s.data(); - QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d); - return s; -} template <> struct QConcatenable<char> { diff --git a/tests/benchmarks/qstringbuilder/main.cpp b/tests/benchmarks/qstringbuilder/main.cpp index 6cecfba6b48..cb769257cc5 100644 --- a/tests/benchmarks/qstringbuilder/main.cpp +++ b/tests/benchmarks/qstringbuilder/main.cpp @@ -275,8 +275,8 @@ private: int main(int argc, char *argv[]) { - 42 % 3; // Sanity test, should always work. - + //qDebug() << (QString("xx") * QLatin1String("y")).toString(); + //42 % 3; // Sanity test, should always work. //QString("x") % 2; // Sanity test, should only compile when the // operator%(QString, int) is visible. -- GitLab