add custom string type

ProString is almost a QStringRef, except that it keeps a copy of instead
of a pointer to the QString - to make it refcountable. additionally, it
holds a hash so it can be efficiently used for repetetive hash lookups.
This commit is contained in:
Oswald Buddenhagen
2010-04-27 18:39:10 +02:00
parent 2fd83af82f
commit 5db91cf5bc
5 changed files with 663 additions and 330 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -51,8 +51,8 @@ class ProFileEvaluator
public:
struct FunctionDefs {
QHash<QString, ProFunctionDef *> testFunctions;
QHash<QString, ProFunctionDef *> replaceFunctions;
QHash<ProString, ProFunctionDef *> testFunctions;
QHash<ProString, ProFunctionDef *> replaceFunctions;
};
enum TemplateType {
@@ -178,7 +178,7 @@ struct ProFileOption
private:
friend class ProFileEvaluator;
friend class ProFileEvaluator::Private;
QHash<QString, QStringList> base_valuemap; // Cached results of qmake.conf, .qmake.cache & default_pre.prf
QHash<ProString, ProStringList> base_valuemap; // Cached results of qmake.conf, .qmake.cache & default_pre.prf
ProFileEvaluator::FunctionDefs base_functions;
QStringList feature_roots;
QString qmakespec_name;

View File

@@ -30,9 +30,207 @@
#include "proitems.h"
#include <QtCore/QFileInfo>
#include <QtCore/QSet>
QT_BEGIN_NAMESPACE
using namespace ProStringConstants;
// from qhash.cpp
static uint hash(const QChar *p, int n)
{
uint h = 0;
while (n--) {
h = (h << 4) + (*p++).unicode();
h ^= (h & 0xf0000000) >> 23;
h &= 0x0fffffff;
}
return h;
}
ProString::ProString() :
m_offset(0), m_length(0), m_hash(0x80000000)
{
}
ProString::ProString(const ProString &other) :
m_string(other.m_string), m_offset(other.m_offset), m_length(other.m_length), m_hash(other.m_hash)
{
}
ProString::ProString(const ProString &other, OmitPreHashing) :
m_string(other.m_string), m_offset(other.m_offset), m_length(other.m_length), m_hash(0x80000000)
{
}
ProString::ProString(const QString &str) :
m_string(str), m_offset(0), m_length(str.length())
{
updatedHash();
}
ProString::ProString(const QString &str, OmitPreHashing) :
m_string(str), m_offset(0), m_length(str.length()), m_hash(0x80000000)
{
}
ProString::ProString(const char *str) :
m_string(QString::fromLatin1(str)), m_offset(0), m_length(qstrlen(str))
{
updatedHash();
}
ProString::ProString(const char *str, OmitPreHashing) :
m_string(QString::fromLatin1(str)), m_offset(0), m_length(qstrlen(str)), m_hash(0x80000000)
{
}
uint ProString::updatedHash() const
{
return (m_hash = hash(m_string.constData() + m_offset, m_length));
}
uint qHash(const ProString &str)
{
if (!(str.m_hash & 0x80000000))
return str.m_hash;
return str.updatedHash();
}
QString ProString::toQString() const
{
return m_string.mid(m_offset, m_length);
}
QString &ProString::toQString(QString &tmp) const
{
return tmp.setRawData(m_string.constData() + m_offset, m_length);
}
bool ProString::operator==(const ProString &other) const
{
if (m_length != other.m_length)
return false;
return !memcmp(m_string.constData() + m_offset,
other.m_string.constData() + other.m_offset, m_length * 2);
}
bool ProString::operator==(const QString &other) const
{
if (m_length != other.length())
return false;
return !memcmp(m_string.constData() + m_offset, other.constData(), m_length * 2);
}
bool ProString::operator==(const QLatin1String &other) const
{
const ushort *uc = (ushort *)m_string.constData() + m_offset;
const ushort *e = uc + m_length;
const uchar *c = (uchar *)other.latin1();
if (!c)
return isEmpty();
while (*c) {
if (uc == e || *uc != *c)
return false;
++uc;
++c;
}
return (uc == e);
}
QString operator+(const ProString &one, const ProString &two)
{
if (two.m_length) {
if (!one.m_length) {
return two.toQString();
} else {
QString neu(one.m_length + two.m_length, Qt::Uninitialized);
ushort *ptr = (ushort *)neu.constData();
memcpy(ptr, one.m_string.constData() + one.m_offset, one.m_length * 2);
memcpy(ptr + one.m_length, two.m_string.constData() + two.m_offset, two.m_length * 2);
return neu;
}
}
return one.toQString();
}
ProString ProString::mid(int off, int len) const
{
ProString ret(*this, NoHash);
if (off > m_length)
off = m_length;
ret.m_offset += off;
ret.m_length -= off;
if (ret.m_length > len)
ret.m_length = len;
return ret;
}
ProString ProString::trimmed() const
{
ProString ret(*this, NoHash);
int cur = m_offset;
int end = cur + m_length;
const QChar *data = m_string.constData();
for (; cur < end; cur++)
if (!data[cur].isSpace()) {
// No underrun check - we know there is at least one non-whitespace
while (data[end - 1].isSpace())
end--;
break;
}
ret.m_offset = cur;
ret.m_length = end - cur;
return ret;
}
QString ProStringList::join(const QString &sep) const
{
int totalLength = 0;
const int sz = size();
for (int i = 0; i < sz; ++i)
totalLength += at(i).size();
if (sz)
totalLength += sep.size() * (sz - 1);
QString res(totalLength, Qt::Uninitialized);
QChar *ptr = (QChar *)res.constData();
for (int i = 0; i < sz; ++i) {
if (i) {
memcpy(ptr, sep.constData(), sep.size() * 2);
ptr += sep.size();
}
memcpy(ptr, at(i).constData(), at(i).size() * 2);
ptr += at(i).size();
}
return res;
}
void ProStringList::removeDuplicates()
{
int n = size();
int j = 0;
QSet<ProString> seen;
seen.reserve(n);
for (int i = 0; i < n; ++i) {
const ProString &s = at(i);
if (seen.contains(s))
continue;
seen.insert(s);
if (j != i)
(*this)[j] = s;
++j;
}
if (n != j)
erase(begin() + j, end());
}
void ProItem::disposeItems(ProItem *nitm)
{
for (ProItem *itm; (itm = nitm); ) {

View File

@@ -31,7 +31,7 @@
#define PROITEMS_H
#include <QtCore/QString>
#include <QtCore/QList>
#include <QtCore/QVector>
QT_BEGIN_NAMESPACE
@@ -49,6 +49,61 @@ private:
};
#endif
namespace ProStringConstants {
enum OmitPreHashing { NoHash };
}
class ProString {
public:
ProString();
ProString(const ProString &other);
ProString(const ProString &other, ProStringConstants::OmitPreHashing);
explicit ProString(const QString &str);
ProString(const QString &str, ProStringConstants::OmitPreHashing);
explicit ProString(const char *str);
ProString(const char *str, ProStringConstants::OmitPreHashing);
QString toQString() const;
QString &toQString(QString &tmp) const;
bool operator==(const ProString &other) const;
bool operator==(const QString &other) const;
bool operator==(const QLatin1String &other) const;
bool operator!=(const ProString &other) const { return !(*this == other); }
bool operator!=(const QString &other) const { return !(*this == other); }
bool operator!=(const QLatin1String &other) const { return !(*this == other); }
bool isEmpty() const { return !m_length; }
int size() const { return m_length; }
const QChar *constData() const { return m_string.constData() + m_offset; }
ProString mid(int off, int len = -1) const;
ProString left(int len) const { return mid(0, len); }
ProString right(int len) const { return mid(qMax(0, size() - len)); }
ProString trimmed() const;
void clear() { m_string.clear(); m_length = 0; }
private:
QString m_string;
int m_offset, m_length;
mutable uint m_hash;
uint updatedHash() const;
friend uint qHash(const ProString &str);
friend QString operator+(const ProString &one, const ProString &two);
};
Q_DECLARE_TYPEINFO(ProString, Q_MOVABLE_TYPE);
uint qHash(const ProString &str);
QString operator+(const ProString &one, const ProString &two);
inline QString operator+(const ProString &one, const QString &two)
{ return one + ProString(two, ProStringConstants::NoHash); }
inline QString operator+(const QString &one, const ProString &two)
{ return ProString(one, ProStringConstants::NoHash) + two; }
class ProStringList : public QVector<ProString> {
public:
ProStringList() {}
ProStringList(const ProString &str) { *this << str; }
QString join(const QString &sep) const;
void removeDuplicates();
};
class ProItem
{
public:
@@ -100,17 +155,18 @@ public:
UniqueAddOperator = 4
};
ProVariable(const QString &name) : ProItem(VariableKind), m_variableKind(SetOperator), m_variable(name) {}
ProVariable(const ProString &name) : ProItem(VariableKind),
m_variableKind(SetOperator), m_variable(name) {}
void setVariableOperator(VariableOperator variableKind) { m_variableKind = variableKind; }
VariableOperator variableOperator() const { return m_variableKind; }
QString variable() const { return m_variable; }
void setValue(const QString &value) { m_value = value; }
QString value() const { return m_value; }
ProString variable() const { return m_variable; }
void setValue(const ProString &value) { m_value = value; }
ProString value() const { return m_value; }
private:
VariableOperator m_variableKind;
QString m_variable;
QString m_value;
ProString m_variable;
ProString m_value;
};
class ProCondition : public ProItem
@@ -143,17 +199,18 @@ class ProLoop : public ProItem
{
public:
ProLoop(const QString &var, const QString &expr)
: ProItem(LoopKind), m_variable(var), m_expression(expr) {}
: ProItem(LoopKind), m_variable(ProString(var)),
m_expression(ProString(expr, ProStringConstants::NoHash)) {}
~ProLoop();
QString variable() const { return m_variable; }
QString expression() const { return m_expression; }
ProString variable() const { return m_variable; }
ProString expression() const { return m_expression; }
ProItem *items() const { return m_proitems; }
ProItem **itemsRef() { return &m_proitems; }
private:
QString m_variable;
QString m_expression;
ProString m_variable;
ProString m_expression;
ProItem *m_proitems;
};
@@ -163,10 +220,10 @@ public:
enum FunctionType { TestFunction, ReplaceFunction };
ProFunctionDef(const QString &name, FunctionType type)
: ProItem(FunctionDefKind), m_name(name), m_type(type), m_refCount(1) {}
: ProItem(FunctionDefKind), m_name(ProString(name)), m_type(type), m_refCount(1) {}
~ProFunctionDef();
QString name() const { return m_name; }
ProString name() const { return m_name; }
FunctionType type() const { return m_type; }
ProItem *items() const { return m_proitems; }
ProItem **itemsRef() { return &m_proitems; }
@@ -175,7 +232,7 @@ public:
void deref() { if (!m_refCount.deref()) delete this; }
private:
QString m_name;
ProString m_name;
FunctionType m_type;
ProItemRefCount m_refCount;
ProItem *m_proitems;

View File

@@ -43,7 +43,7 @@ void ProWriter::addFiles(ProFile *profile, QStringList *lines,
for (ProItem *item = profile->items(); item; item = item->next()) {
if (item->kind() == ProItem::VariableKind) {
ProVariable *proVar = static_cast<ProVariable*>(item);
if (var == proVar->variable()
if (var == proVar->variable().toQString()
&& proVar->variableOperator() != ProVariable::RemoveOperator
&& proVar->variableOperator() != ProVariable::ReplaceOperator) {
@@ -93,7 +93,7 @@ static void findProVariables(ProItem *item, const QStringList &vars,
findProVariables(static_cast<ProBranch*>(item)->elseItems(), vars, proVars);
} else if (item->kind() == ProItem::VariableKind) {
ProVariable *proVar = static_cast<ProVariable*>(item);
if (vars.contains(proVar->variable()))
if (vars.contains(proVar->variable().toQString()))
*proVars << proVar;
}
}