Commit 7c8735b7 authored by hjk's avatar hjk
Browse files

Make QStringBuilder work with plain 'char' "chunks".

parent feb34c0e
......@@ -114,6 +114,22 @@ private:
const QString *a;
};
template <>
class QStringBuilder<char>
{
public:
QStringBuilder(char c_) : c(c_) {}
inline int size() const { return 1; }
inline void append(QChar *&out) const { *out++ = QLatin1Char(c); }
private:
const char c;
};
template <class A, class B>
class QStringBuilderPair
{
......@@ -134,6 +150,9 @@ operator%(const A &a, const B &b)
return QStringBuilderPair<A, B> (a, b);
}
// QString related specializations
template <class A>
inline QStringBuilder< QStringBuilderPair<A, QStringBuilder<QString> > >
operator%(const A &a, const QString &b)
......@@ -148,10 +167,8 @@ operator%(const QString &a, const B &b)
return QStringBuilderPair<QStringBuilder<QString>, B> (a, b);
}
inline QStringBuilder<
QStringBuilderPair<QStringBuilder<QString>,
QStringBuilder<QString> >
QStringBuilderPair<QStringBuilder<QString>, QStringBuilder<QString> >
>
operator%(const QString &a, const QString &b)
{
......@@ -159,4 +176,31 @@ operator%(const QString &a, const QString &b)
QStringBuilder<QString> > (a, b);
}
// char related specializations
template <class A>
inline QStringBuilder< QStringBuilderPair<A, QStringBuilder<char> > >
operator%(const A &a, char b)
{
return QStringBuilderPair<A, QStringBuilder<char> > (a, b);
}
template <class B>
inline QStringBuilder< QStringBuilderPair<QStringBuilder<char>, B> >
operator%(char a, const B &b)
{
return QStringBuilderPair<QStringBuilder<QString>, B> (a, b);
}
inline QStringBuilder<
QStringBuilderPair<QStringBuilder<QString>, QStringBuilder<char> >
>
operator%(const QString &a, char b)
{
return QStringBuilderPair<
QStringBuilder<QString>, QStringBuilder<char>
> (a, b);
}
#endif // QSTRINGBUILDER_H
......@@ -16,69 +16,91 @@ public:
private slots:
// QStringBuilder based
void builderbased_l1literal_l1literal();
void builderbased_l1literal_l1literal_l1literal();
void builderbased_l1literal_l1literal_l1literal_l1literal();
void b_separator() { qDebug() << "\n------- QStringBuilder based ----------"; }
void b_2_l1literal();
void b_3_l1literal();
void b_4_l1literal();
void b_string_4_char();
// QString based for comparison
void stringbased_l1string_l1string();
void stringbased_l1string_l1string_l1string();
void stringbased_l1string_l1string_l1string_l1string();
void s_separator() { qDebug() << "\n-------- QString based ---------"; }
void s_2_l1string();
void s_3_l1string();
void s_4_l1string();
void s_string_4_char();
private:
const QLatin1Literal l1literal;
const QLatin1String l1string;
const QString string;
const char achar;
};
tst_qstringbuilder::tst_qstringbuilder()
: l1literal("some literal"), l1string("some literal")
: l1literal("some literal"),
l1string("some literal"),
string(l1string),
achar('c')
{}
void tst_qstringbuilder::builderbased_l1literal_l1literal()
void tst_qstringbuilder::b_2_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal; }
QCOMPARE(result, l1string + l1string);
}
void tst_qstringbuilder::builderbased_l1literal_l1literal_l1literal()
void tst_qstringbuilder::b_3_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal % l1literal; }
QCOMPARE(result, l1string + l1string + l1string);
}
void tst_qstringbuilder::builderbased_l1literal_l1literal_l1literal_l1literal()
void tst_qstringbuilder::b_4_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal % l1literal % l1literal; }
QCOMPARE(result, l1string + l1string + l1string + l1string);
}
void tst_qstringbuilder::b_string_4_char()
{
QString result;
QBENCHMARK { result = string + achar + achar + achar; }
QCOMPARE(result, QString(string % achar % achar % achar));
}
void tst_qstringbuilder::stringbased_l1string_l1string()
void tst_qstringbuilder::s_2_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string; }
QCOMPARE(result, QString(l1literal % l1literal));
}
void tst_qstringbuilder::stringbased_l1string_l1string_l1string()
void tst_qstringbuilder::s_3_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string + l1string; }
QCOMPARE(result, QString(l1literal % l1literal % l1literal));
}
void tst_qstringbuilder::stringbased_l1string_l1string_l1string_l1string()
void tst_qstringbuilder::s_4_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string + l1string + l1string; }
QCOMPARE(result, QString(l1literal % l1literal % l1literal % l1literal));
}
void tst_qstringbuilder::s_string_4_char()
{
QString result;
QBENCHMARK { result = string + achar + achar + achar; }
QCOMPARE(result, QString(string % achar % achar % achar));
}
QTEST_MAIN(tst_qstringbuilder)
#include "main.moc"
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