Commit 2ec33349 authored by hjk's avatar hjk
Browse files

qstringbuilder: make it work for the char % string case

parent 0b19fb63
......@@ -53,7 +53,7 @@
smaller chunks.
Contrary to \c QLatin1String, a \c QLatin1Literal can retrieve its size
int \i{O(1)}.
without iterating over the literal.
\ingroup tools
\ingroup shared
......@@ -127,5 +127,7 @@
Creates a helper object containing both parameters.
This is the main function to build up
This is the main function to build up complex QStringBuilder objects from
smaller QStringBuilder objects.
*/
......@@ -46,14 +46,18 @@
#include <string.h>
// Using this relies on chaning the QString::QString(QChar *, int)
// constructor to allocated an unitialized string of the given size.
//#define USE_CHANGED_QSTRING 1
class QLatin1Literal
{
public:
template <int N>
QLatin1Literal(const char (&str)[N]) : m_size(N - 1), m_data(str) {}
int size() const { return m_size; }
const char *data() const { return m_data; }
inline int size() const { return m_size; }
inline const char *data() const { return m_data; }
void append(QChar *&out) const
{
......@@ -64,13 +68,18 @@ public:
operator QString() const
{
#ifdef USE_CHANGED_QSTRING
QString s(m_size, QChar(-1));
#else
QString s;
s.resize(m_size);
#endif
QChar *d = s.data();
append(d);
return s;
}
private:
const int m_size;
const char *m_data;
......@@ -84,8 +93,12 @@ public:
operator QString() const
{
#ifdef USE_CHANGED_QSTRING
QString s(this->size(), QChar(-1));
#else
QString s;
s.resize(this->size());
#endif
QChar *d = s.data();
this->append(d);
return s;
......@@ -115,35 +128,42 @@ private:
};
template <>
class QStringBuilder<char>
{
public:
QStringBuilder(char c_) : c(c_) {}
template <typename A>
int qStringBuilderSize(const A a) { return a.size(); }
inline int size() const { return 1; }
inline void append(QChar *&out) const { *out++ = QLatin1Char(c); }
inline int qStringBuilderSize(const char) { return 1; }
private:
const char c;
};
template <typename A>
inline void qStringBuilderAppend(const A a, QChar *&out) { a.append(out); }
inline void qStringBuilderAppend(char c, QChar *&out) { *out++ = QLatin1Char(c); }
template <class A, class B>
template <typename A, typename B>
class QStringBuilderPair
{
public:
QStringBuilderPair(A a_, B b_) : a(a_), b(b_) {}
inline int size() const { return a.size() + b.size(); }
inline void append(QChar *&out) const { a.append(out); b.append(out); }
inline int size() const
{
return qStringBuilderSize(a) + qStringBuilderSize(b);
}
inline void append(QChar *&out) const
{
qStringBuilderAppend(a, out);
qStringBuilderAppend(b, out);
}
private:
A a;
B b;
};
template <class A, class B>
template <typename A, typename B>
QStringBuilder< QStringBuilderPair<A, B> >
operator%(const A &a, const B &b)
{
......@@ -176,31 +196,5 @@ 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
#include "qstringbuilder.h"
#include <QDebug>
#include <QString>
#include "../../../src/libs/utils/qstringbuilder.h"
#include <qtest.h>
#define COMPARE(a, b) QCOMPARE(a, b)
//#define COMPARE(a, b)
class tst_qstringbuilder : public QObject
{
Q_OBJECT
......@@ -20,14 +23,18 @@ private slots:
void b_2_l1literal();
void b_3_l1literal();
void b_4_l1literal();
void b_5_l1literal();
void b_string_4_char();
void b_char_string_char();
// QString based for comparison
void s_separator() { qDebug() << "\n-------- QString based ---------"; }
void s_2_l1string();
void s_3_l1string();
void s_4_l1string();
void s_5_l1string();
void s_string_4_char();
void s_char_string_char();
private:
const QLatin1Literal l1literal;
......@@ -48,28 +55,42 @@ void tst_qstringbuilder::b_2_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal; }
QCOMPARE(result, l1string + l1string);
COMPARE(result, l1string + l1string);
}
void tst_qstringbuilder::b_3_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal % l1literal; }
QCOMPARE(result, l1string + l1string + l1string);
COMPARE(result, l1string + l1string + l1string);
}
void tst_qstringbuilder::b_4_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal % l1literal % l1literal; }
QCOMPARE(result, l1string + l1string + l1string + l1string);
COMPARE(result, l1string + l1string + l1string + l1string);
}
void tst_qstringbuilder::b_5_l1literal()
{
QString result;
QBENCHMARK { result = l1literal % l1literal % l1literal % l1literal %l1literal; }
COMPARE(result, l1string + 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));
QBENCHMARK { result = string + achar + achar + achar + achar; }
COMPARE(result, QString(string % achar % achar % achar % achar));
}
void tst_qstringbuilder::b_char_string_char()
{
QString result;
QBENCHMARK { result = achar + string + achar; }
COMPARE(result, QString(achar % string % achar));
}
......@@ -77,28 +98,42 @@ void tst_qstringbuilder::s_2_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string; }
QCOMPARE(result, QString(l1literal % l1literal));
COMPARE(result, QString(l1literal % l1literal));
}
void tst_qstringbuilder::s_3_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string + l1string; }
QCOMPARE(result, QString(l1literal % l1literal % l1literal));
COMPARE(result, QString(l1literal % l1literal % l1literal));
}
void tst_qstringbuilder::s_4_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string + l1string + l1string; }
QCOMPARE(result, QString(l1literal % l1literal % l1literal % l1literal));
COMPARE(result, QString(l1literal % l1literal % l1literal % l1literal));
}
void tst_qstringbuilder::s_5_l1string()
{
QString result;
QBENCHMARK { result = l1string + l1string + l1string + l1string + l1string; }
COMPARE(result, QString(l1literal % 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));
QBENCHMARK { result = string + achar + achar + achar + achar; }
COMPARE(result, QString(string % achar % achar % achar % achar));
}
void tst_qstringbuilder::s_char_string_char()
{
QString result;
QBENCHMARK { result = achar + string + achar; }
COMPARE(result, QString(achar % string % achar));
}
QTEST_MAIN(tst_qstringbuilder)
......
load(qttest_p4)
TEMPLATE = app
TARGET = tst_qstringbuilder
DEPENDPATH += .
INCLUDEPATH += .
STRINGBUILDERDIR = ../../../src/libs/utils
INCLUDEPATH += $$STRINGBUILDERDIR
QT -= gui
CONFIG += release
# Input
SOURCES += main.cpp
SOURCES += main.cpp $$STRINGBUILDERDIR/qstringbuilder.cpp
Supports Markdown
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