diff options
author | Olivier Goffart <ogoffart@trolltech.com> | 2010-02-19 11:48:28 (GMT) |
---|---|---|
committer | Olivier Goffart <ogoffart@trolltech.com> | 2010-02-21 16:11:40 (GMT) |
commit | 8302f412660410775887c6c524aa87fb67aebde1 (patch) | |
tree | 370038b3e0c0fd44ba36fca0d2e62ad8bf0bac32 /src | |
parent | 4d3ca81b0aeef6f6496f3e89451f5369ce5efa99 (diff) | |
download | Qt-8302f412660410775887c6c524aa87fb67aebde1.zip Qt-8302f412660410775887c6c524aa87fb67aebde1.tar.gz Qt-8302f412660410775887c6c524aa87fb67aebde1.tar.bz2 |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qstringbuilder.h | 28 |
1 files 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<A, B> >::size(*this), - Qt::Uninitialized); + const uint size = QConcatenable< QStringBuilder<A, B> >::size(*this); + QString s(size, Qt::Uninitialized); QChar *d = s.data(); + const QChar * const start = d; QConcatenable< QStringBuilder<A, B> >::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<A, B> >::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 <QString, QString> { @@ -134,6 +137,7 @@ class QStringBuilder <QString, QString> template <> struct QConcatenable<char> : 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<char> : private QAbstractConcatenable template <> struct QConcatenable<QLatin1Char> { 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<QLatin1Char> template <> struct QConcatenable<QChar> { 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<QChar> template <> struct QConcatenable<QCharRef> { 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<QCharRef> template <> struct QConcatenable<QLatin1String> { 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<QLatin1String> template <> struct QConcatenable<QLatin1Literal> { 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<QLatin1Literal> template <> struct QConcatenable<QString> { 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<QString> template <> struct QConcatenable<QStringRef> { 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<QStringRef> template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable { typedef char type[N]; + enum { ExactSize = false }; static int size(const char[N]) { return N - 1; @@ -235,6 +247,7 @@ template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable template <int N> struct QConcatenable<const char[N]> : 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 <int N> struct QConcatenable<const char[N]> : private QAbstractConcaten template <> struct QConcatenable<const char *> : 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<const char *> : private QAbstractConcatenable template <> struct QConcatenable<QByteArray> : 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 <typename A, typename B> struct QConcatenable< QStringBuilder<A, B> > { typedef QStringBuilder<A, B> type; + enum { ExactSize = QConcatenable<A>::ExactSize && QConcatenable<B>::ExactSize }; static int size(const type &p) { return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b); |