From 8302f412660410775887c6c524aa87fb67aebde1 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 19 Feb 2010 12:48:28 +0100 Subject: QStringBuilder: Do not resize if not required. This reduce a lot the binary size. And is also faster. It is not source compatible if customer have made their own QConcatenable. But this class is not documented, and it is easy to fix. Reviewed-by: Joao Reviewed-by: hjk --- src/corelib/tools/qstringbuilder.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 4ff1b36..9fe3fd7 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -100,14 +100,18 @@ public: operator QString() const { - QString s(QConcatenable< QStringBuilder >::size(*this), - Qt::Uninitialized); + const uint size = QConcatenable< QStringBuilder >::size(*this); + QString s(size, Qt::Uninitialized); QChar *d = s.data(); + const QChar * const start = d; QConcatenable< QStringBuilder >::appendTo(*this, d); - // this resize is necessary since we allocate a bit too much - // when dealing with variable sized 8-bit encodings - s.resize(d - s.data()); + + if (!QConcatenable< QStringBuilder >::ExactSize && size != d - start) { + // this resize is necessary since we allocate a bit too much + // when dealing with variable sized 8-bit encodings + s.resize(d - start); + } return s; } QByteArray toLatin1() const { return QString(*this).toLatin1(); } @@ -116,7 +120,6 @@ public: const B &b; }; - template <> class QStringBuilder { @@ -134,6 +137,7 @@ class QStringBuilder template <> struct QConcatenable : private QAbstractConcatenable { typedef char type; + enum { ExactSize = true }; static int size(const char) { return 1; } static inline void appendTo(const char c, QChar *&out) { @@ -144,6 +148,7 @@ template <> struct QConcatenable : private QAbstractConcatenable template <> struct QConcatenable { typedef QLatin1Char type; + enum { ExactSize = true }; static int size(const QLatin1Char) { return 1; } static inline void appendTo(const QLatin1Char c, QChar *&out) { @@ -154,6 +159,7 @@ template <> struct QConcatenable template <> struct QConcatenable { typedef QChar type; + enum { ExactSize = true }; static int size(const QChar) { return 1; } static inline void appendTo(const QChar c, QChar *&out) { @@ -164,6 +170,7 @@ template <> struct QConcatenable template <> struct QConcatenable { typedef QCharRef type; + enum { ExactSize = true }; static int size(const QCharRef &) { return 1; } static inline void appendTo(const QCharRef &c, QChar *&out) { @@ -174,6 +181,7 @@ template <> struct QConcatenable template <> struct QConcatenable { typedef QLatin1String type; + enum { ExactSize = true }; static int size(const QLatin1String &a) { return qstrlen(a.latin1()); } static inline void appendTo(const QLatin1String &a, QChar *&out) { @@ -186,6 +194,7 @@ template <> struct QConcatenable template <> struct QConcatenable { typedef QLatin1Literal type; + enum { ExactSize = true }; static int size(const QLatin1Literal &a) { return a.size(); } static inline void appendTo(const QLatin1Literal &a, QChar *&out) { @@ -197,6 +206,7 @@ template <> struct QConcatenable template <> struct QConcatenable { typedef QString type; + enum { ExactSize = true }; static int size(const QString &a) { return a.size(); } static inline void appendTo(const QString &a, QChar *&out) { @@ -209,6 +219,7 @@ template <> struct QConcatenable template <> struct QConcatenable { typedef QStringRef type; + enum { ExactSize = true }; static int size(const QStringRef &a) { return a.size(); } static inline void appendTo(QStringRef a, QChar *&out) { @@ -222,6 +233,7 @@ template <> struct QConcatenable template struct QConcatenable : private QAbstractConcatenable { typedef char type[N]; + enum { ExactSize = false }; static int size(const char[N]) { return N - 1; @@ -235,6 +247,7 @@ template struct QConcatenable : private QAbstractConcatenable template struct QConcatenable : private QAbstractConcatenable { typedef const char type[N]; + enum { ExactSize = false }; static int size(const char[N]) { return N - 1; } static inline void appendTo(const char a[N], QChar *&out) { @@ -245,6 +258,7 @@ template struct QConcatenable : private QAbstractConcaten template <> struct QConcatenable : private QAbstractConcatenable { typedef char const *type; + enum { ExactSize = false }; static int size(const char *a) { return qstrlen(a); } static inline void appendTo(const char *a, QChar *&out) { @@ -255,6 +269,7 @@ template <> struct QConcatenable : private QAbstractConcatenable template <> struct QConcatenable : private QAbstractConcatenable { typedef QByteArray type; + enum { ExactSize = false }; static int size(const QByteArray &ba) { return qstrnlen(ba.constData(), ba.size()); } static inline void appendTo(const QByteArray &ba, QChar *&out) { @@ -267,6 +282,7 @@ template struct QConcatenable< QStringBuilder > { typedef QStringBuilder type; + enum { ExactSize = QConcatenable::ExactSize && QConcatenable::ExactSize }; static int size(const type &p) { return QConcatenable::size(p.a) + QConcatenable::size(p.b); -- cgit v0.12