summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstringbuilder.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qstringbuilder.h')
-rw-r--r--src/corelib/tools/qstringbuilder.h101
1 files changed, 67 insertions, 34 deletions
diff --git a/src/corelib/tools/qstringbuilder.h b/src/corelib/tools/qstringbuilder.h
index a93a6389b..798d09702 100644
--- a/src/corelib/tools/qstringbuilder.h
+++ b/src/corelib/tools/qstringbuilder.h
@@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
@@ -9,8 +10,8 @@
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
-** contained in the either Technology Preview License Agreement or the
-** Beta Release License Agreement.
+** contained in the Technology Preview License Agreement accompanying
+** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
@@ -20,21 +21,20 @@
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
-** In addition, as a special exception, Nokia gives you certain
-** additional rights. These rights are described in the Nokia Qt LGPL
-** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
-** package.
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3.0 as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU General Public License version 3.0 requirements will be
-** met: http://www.gnu.org/copyleft/gpl.html.
**
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/
@@ -44,6 +44,12 @@
#include <QtCore/qstring.h>
+#if defined(Q_CC_GNU) && !defined(Q_CC_INTEL)
+# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
+# include <QtCore/qmap.h>
+# endif
+#endif
+
#include <string.h>
QT_BEGIN_HEADER
@@ -60,7 +66,7 @@ public:
const char *data() const { return m_data; }
template <int N>
- QLatin1Literal(const char (&str)[N])
+ QLatin1Literal(const char (&str)[N])
: m_size(N - 1), m_data(str) {}
private:
@@ -68,6 +74,21 @@ private:
const char *m_data;
};
+struct Q_CORE_EXPORT QAbstractConcatenable
+{
+protected:
+ static void convertFromAscii(const char *a, int len, QChar *&out);
+
+ static inline void convertFromAscii(char a, QChar *&out)
+ {
+#ifndef QT_NO_TEXTCODEC
+ if (QString::codecForCStrings)
+ *out++ = QChar::fromAscii(a);
+ else
+#endif
+ *out++ = QLatin1Char(a);
+ }
+};
template <typename T> struct QConcatenable {};
@@ -81,9 +102,12 @@ public:
{
QString s(QConcatenable< QStringBuilder<A, B> >::size(*this),
Qt::Uninitialized);
-
+
QChar *d = s.data();
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());
return s;
}
QByteArray toLatin1() const { return QString(*this).toLatin1(); }
@@ -93,13 +117,13 @@ public:
};
-template <> struct QConcatenable<char>
+template <> struct QConcatenable<char> : private QAbstractConcatenable
{
typedef char type;
static int size(const char) { return 1; }
static inline void appendTo(const char c, QChar *&out)
{
- *out++ = QLatin1Char(c);
+ QAbstractConcatenable::convertFromAscii(c, out);
}
};
@@ -164,7 +188,7 @@ template <> struct QConcatenable<QString>
{
const int n = a.size();
memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
- out += n;
+ out += n;
}
};
@@ -176,42 +200,51 @@ template <> struct QConcatenable<QStringRef>
{
const int n = a.size();
memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
- out += n;
+ out += n;
}
};
#ifndef QT_NO_CAST_FROM_ASCII
-template <int N> struct QConcatenable<char[N]>
+template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable
{
typedef char type[N];
+ static int size(const char[N])
+ {
+ return N - 1;
+ }
+ static inline void appendTo(const char a[N], QChar *&out)
+ {
+ QAbstractConcatenable::convertFromAscii(a, N, out);
+ }
+};
+
+template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable
+{
+ typedef const char type[N];
static int size(const char[N]) { return N - 1; }
static inline void appendTo(const char a[N], QChar *&out)
{
- for (int i = 0; i < N - 1; ++i)
- *out++ = QLatin1Char(a[i]);
+ QAbstractConcatenable::convertFromAscii(a, N, out);
}
};
-template <> struct QConcatenable<const char *>
+template <> struct QConcatenable<const char *> : private QAbstractConcatenable
{
typedef char const *type;
static int size(const char *a) { return qstrlen(a); }
static inline void appendTo(const char *a, QChar *&out)
{
- while (*a)
- *out++ = QLatin1Char(*a++);
+ QAbstractConcatenable::convertFromAscii(a, -1, out);
}
};
-template <> struct QConcatenable<QByteArray>
+template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
{
typedef QByteArray type;
static int size(const QByteArray &ba) { return qstrnlen(ba.constData(), ba.size()); }
static inline void appendTo(const QByteArray &ba, QChar *&out)
{
- const char *data = ba.constData();
- while (*data)
- *out++ = QLatin1Char(*data++);
+ QAbstractConcatenable::convertFromAscii(ba.constData(), -1, out);
}
};
#endif
@@ -220,7 +253,7 @@ template <typename A, typename B>
struct QConcatenable< QStringBuilder<A, B> >
{
typedef QStringBuilder<A, B> type;
- static int size(const type &p)
+ static int size(const type &p)
{
return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b);
}
@@ -235,7 +268,7 @@ template <typename A, typename B>
QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
operator%(const A &a, const B &b)
{
- return QStringBuilder<A, B>(a, b);
+ return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
}
#ifdef QT_USE_FAST_OPERATOR_PLUS
@@ -243,7 +276,7 @@ template <typename A, typename B>
QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
operator+(const A &a, const B &b)
{
- return QStringBuilder<A, B>(a, b);
+ return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
}
#endif