Commit 273011f9 authored by hjk's avatar hjk
Browse files

Revert "qstringbuilder: a version without QLatin1Literal"

This reverts commit 28303e86.
parent 28303e86
......@@ -57,9 +57,22 @@ QT_MODULE(Core)
// constructor to allocated an unitialized string of the given size.
//#define USE_CHANGED_QSTRING 1
namespace {
class QLatin1Literal
{
public:
template <int N>
QLatin1Literal(const char (&str)[N]) : m_size(N - 1), m_data(str) {}
template <typename T> struct QConcatenable {};
inline int size() const { return m_size; }
inline const char *data() const { return m_data; }
private:
const int m_size;
const char *m_data;
};
namespace {
template <typename A, typename B>
class QStringBuilder
......@@ -73,6 +86,10 @@ public:
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
{
......@@ -90,7 +107,7 @@ QStringBuilder<A, B>::operator QString() const
template <> struct QConcatenable<char>
{
typedef char type;
static inline int size(const char) { return 1; }
static int size(const char) { return 1; }
static inline void appendTo(const char c, QChar *&out)
{
*out++ = QLatin1Char(c);
......@@ -100,7 +117,7 @@ template <> struct QConcatenable<char>
template <> struct QConcatenable<QLatin1Char>
{
typedef QLatin1Char type;
static inline int size(const QLatin1Char) { return 1; }
static int size(const QLatin1Char) { return 1; }
static inline void appendTo(const QLatin1Char c, QChar *&out)
{
*out++ = c;
......@@ -110,7 +127,7 @@ template <> struct QConcatenable<QLatin1Char>
template <> struct QConcatenable<QLatin1String>
{
typedef QLatin1String type;
static inline int size(const QLatin1String &a) { return qstrlen(a.latin1()); }
static int size(const QLatin1String &a) { return qstrlen(a.latin1()); }
static inline void appendTo(const QLatin1String &a, QChar *&out)
{
for (const char *s = a.latin1(); *s; )
......@@ -119,10 +136,21 @@ template <> struct QConcatenable<QLatin1String>
};
template <> struct QConcatenable<QLatin1Literal>
{
typedef QLatin1Literal type;
static int size(const QLatin1Literal &a) { return a.size(); }
static inline void appendTo(const QLatin1Literal &a, QChar *&out)
{
for (const char *s = a.data(); *s; )
*out++ = QLatin1Char(*s++);
}
};
template <> struct QConcatenable<QString>
{
typedef QString type;
static inline int size(const QString &a) { return a.size(); }
static int size(const QString &a) { return a.size(); }
static inline void appendTo(const QString &a, QChar *&out)
{
const int n = a.size();
......@@ -134,7 +162,7 @@ template <> struct QConcatenable<QString>
template <> struct QConcatenable<QStringRef>
{
typedef QStringRef type;
static inline int size(const QStringRef &a) { return a.size(); }
static int size(const QStringRef &a) { return a.size(); }
static inline void appendTo(QStringRef a, QChar *&out)
{
const int n = a.size();
......@@ -143,36 +171,12 @@ template <> struct QConcatenable<QStringRef>
}
};
template <int N>
struct QConcatenable<const char[N]>
{
typedef const char type[N];
static inline int size(const char *) { return N - 1; }
static inline void appendTo(const char *a, QChar *&out)
{
for (int i = 0; i < N - 1; ++i)
*out++ = QLatin1Char(*a++);
}
};
template <int N>
struct QConcatenable<char[N]>
{
typedef char type[N];
static inline int size(const char *) { return N - 1; }
static inline void appendTo(const char *a, QChar *&out)
{
for (int i = 0; i < N - 1; ++i)
*out++ = QLatin1Char(*a++);
}
};
template <typename A, typename B>
struct QConcatenable< QStringBuilder<A, B> >
{
typedef QStringBuilder<A, B> type;
static inline int size(const type &p)
static int size(const type &p)
{
return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b);
}
......@@ -194,6 +198,7 @@ QStringBuilder<A, B> operator%(const A &a, const B &b)
return QStringBuilder<A1, B1>(a, b);
}
QT_END_NAMESPACE
QT_END_HEADER
......
......@@ -13,17 +13,15 @@
#define SEP(s) qDebug() << "\n\n-------- " s " ---------";
#define L(s) QLatin1String(s)
const char l1literal[] = "some string literal";
class tst_qstringbuilder : public QObject
{
Q_OBJECT
public:
tst_qstringbuilder()
:
l1string(l1literal),
ba(l1literal),
: l1literal("some string literal"),
l1string("some string literal"),
ba("some string literal"),
string(l1string),
stringref(&string, 2, 10),
achar('c')
......@@ -73,12 +71,12 @@ private slots:
void separator_1() { SEP("literal + literal (builder first)"); }
void b_2_l1literal() {
QBENCHMARK { r = l1string % l1literal; }
QBENCHMARK { r = l1literal % l1literal; }
COMPARE(r, l1string + l1string);
}
void s_2_l1string() {
QBENCHMARK { r = l1string + l1string; }
COMPARE(r, QString(l1string % l1literal));
COMPARE(r, QString(l1literal % l1literal));
}
......@@ -141,37 +139,37 @@ private slots:
void separator_3() { SEP("3 literals"); }
void b_3_l1literal() {
QBENCHMARK { r = l1string % l1literal % l1literal; }
QBENCHMARK { r = l1literal % l1literal % l1literal; }
COMPARE(r, l1string + l1string + l1string);
}
void s_3_l1string() {
QBENCHMARK { r = l1string + l1string + l1string; }
COMPARE(r, QString(l1string % l1literal % l1literal));
COMPARE(r, QString(l1literal % l1literal % l1literal));
}
void separator_4() { SEP("4 literals"); }
void b_4_l1literal() {
QBENCHMARK { r = l1string % l1literal % l1literal % l1literal; }
QBENCHMARK { r = l1literal % l1literal % l1literal % l1literal; }
COMPARE(r, l1string + l1string + l1string + l1string);
}
void s_4_l1string() {
QBENCHMARK { r = l1string + l1string + l1string + l1string; }
COMPARE(r, QString(l1string % l1literal % l1literal % l1literal));
COMPARE(r, QString(l1literal % l1literal % l1literal % l1literal));
}
void separator_5() { SEP("5 literals"); }
void b_5_l1literal() {
QBENCHMARK { r = l1string % l1literal % l1literal % l1literal %l1literal; }
QBENCHMARK { r = l1literal % l1literal % l1literal % l1literal %l1literal; }
COMPARE(r, l1string + l1string + l1string + l1string + l1string);
}
void s_5_l1string() {
QBENCHMARK { r = l1string + l1string + l1string + l1string + l1string; }
COMPARE(r, QString(l1string % l1literal % l1literal % l1literal % l1literal));
COMPARE(r, QString(l1literal % l1literal % l1literal % l1literal % l1literal));
}
......@@ -262,6 +260,7 @@ private slots:
}
private:
const QLatin1Literal l1literal;
const QLatin1String l1string;
const QByteArray ba;
const QString string;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment