diff options
author | Olivier Goffart <olivier.goffart@nokia.com> | 2011-04-06 11:01:48 (GMT) |
---|---|---|
committer | Olivier Goffart <olivier.goffart@nokia.com> | 2011-04-06 12:29:37 (GMT) |
commit | 0a4f6af142c57929bd743f3e95e25ada8642e9e4 (patch) | |
tree | 496b274cc44f6fe821a557e5f26d96cbf6837668 /src/corelib | |
parent | 3c400bdd727af5feebd1f514b2509f40250acd34 (diff) | |
download | Qt-0a4f6af142c57929bd743f3e95e25ada8642e9e4.zip Qt-0a4f6af142c57929bd743f3e95e25ada8642e9e4.tar.gz Qt-0a4f6af142c57929bd743f3e95e25ada8642e9e4.tar.bz2 |
QStringBuilder: add operator += for QString and QByteArray
Optimize cases when we have.
string += someString + someOtherString
Avoid the allocation of one temporary string.
Behaviour change:
byteArray += someString + QByteArray("foo\0bar", 7)
before, the array would end at foo because of the conversion to string
Reviewed-by: hjk
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/tools/qstring.h | 3 | ||||
-rw-r--r-- | src/corelib/tools/qstringbuilder.h | 29 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index b54258f..002e516 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -485,6 +485,9 @@ public: const_iterator constEnd() const; // STL compatibility + typedef const QChar & const_reference; + typedef QChar & reference; + typedef QChar value_type; inline void push_back(QChar c) { append(c); } inline void push_back(const QString &s) { append(s); } inline void push_front(QChar c) { prepend(c); } diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h index 260b574..2d3475f 100644 --- a/src/corelib/tools/qstringbuilder.h +++ b/src/corelib/tools/qstringbuilder.h @@ -419,6 +419,35 @@ operator+(const A &a, const B &b) } #endif +template <typename A, typename B> +QByteArray &operator+=(QByteArray &a, const QStringBuilder<A, B> &b) +{ +#ifndef QT_NO_CAST_TO_ASCII + if (sizeof(typename QConcatenable< QStringBuilder<A, B> >::ConvertTo::value_type) == sizeof(QChar)) { + //it is not save to optimize as in utf8 it is not possible to compute the size + return a += QString(b); + } +#endif + int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b); + a.reserve(len); + char *it = a.data() + a.size(); + QConcatenable< QStringBuilder<A, B> >::appendTo(b, it); + a.resize(len); //we need to resize after the appendTo for the case str+=foo+str + return a; +} + +template <typename A, typename B> +QString &operator+=(QString &a, const QStringBuilder<A, B> &b) +{ + int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b); + a.reserve(len); + QChar *it = a.data() + a.size(); + QConcatenable< QStringBuilder<A, B> >::appendTo(b, it); + a.resize(it - a.constData()); //may be smaller than len if there was conversion from utf8 + return a; +} + + QT_END_NAMESPACE QT_END_HEADER |