beef up Pro* APIs

so they match the Q* counterparts more closely:

ProString: startsWith(), endsWith(), indexOf(), lastIndexOf(),
contains(), at(), length(), QTextStream::operator<<(), toLatin1(),
toInt() & toShort()

ProStringList: length(), removeAt(), contains()

Change-Id: Iab78236f7152f6a4b93709abbf59b9027c44ba2c
Reviewed-by: Daniel Teske <daniel.teske@nokia.com>
This commit is contained in:
Oswald Buddenhagen
2012-08-21 19:57:09 +02:00
parent 1f2189d5a0
commit 2db6609556
2 changed files with 120 additions and 5 deletions

View File

@@ -33,6 +33,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QSet> #include <QSet>
#include <QStringList> #include <QStringList>
#include <QTextStream>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@@ -289,6 +290,12 @@ ProString ProString::trimmed() const
return ret; return ret;
} }
QTextStream &operator<<(QTextStream &t, const ProString &str)
{
t << str.toQString(); // XXX optimize ... somehow
return t;
}
QString ProStringList::join(const QString &sep) const QString ProStringList::join(const QString &sep) const
{ {
int totalLength = 0; int totalLength = 0;
@@ -313,6 +320,20 @@ QString ProStringList::join(const QString &sep) const
return res; return res;
} }
void ProStringList::removeAll(const ProString &str)
{
for (int i = size(); --i >= 0; )
if (at(i) == str)
remove(i);
}
void ProStringList::removeAll(const char *str)
{
for (int i = size(); --i >= 0; )
if (at(i) == str)
remove(i);
}
void ProStringList::removeDuplicates() void ProStringList::removeDuplicates()
{ {
int n = size(); int n = size();
@@ -332,6 +353,13 @@ void ProStringList::removeDuplicates()
erase(begin() + j, end()); erase(begin() + j, end());
} }
ProStringList::ProStringList(const QStringList &list)
{
reserve(list.size());
foreach (const QString &str, list)
*this << ProString(str);
}
QStringList ProStringList::toQStringList() const QStringList ProStringList::toQStringList() const
{ {
QStringList ret; QStringList ret;
@@ -341,6 +369,22 @@ QStringList ProStringList::toQStringList() const
return ret; return ret;
} }
bool ProStringList::contains(const ProString &str, Qt::CaseSensitivity cs) const
{
for (int i = 0; i < size(); i++)
if (!at(i).compare(str, cs))
return true;
return false;
}
bool ProStringList::contains(const char *str, Qt::CaseSensitivity cs) const
{
for (int i = 0; i < size(); i++)
if (!at(i).compare(str, cs))
return true;
return false;
}
ProFile::ProFile(const QString &fileName) ProFile::ProFile(const QString &fileName)
: m_refCount(1), : m_refCount(1),
m_fileName(fileName), m_fileName(fileName),

View File

@@ -32,12 +32,15 @@
#define PROITEMS_H #define PROITEMS_H
#include "qmake_global.h" #include "qmake_global.h"
#include <QString> #include <QString>
#include <QVector> #include <QVector>
#include <QHash> #include <QHash>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QTextStream;
#ifdef PROPARSER_THREAD_SAFE #ifdef PROPARSER_THREAD_SAFE
typedef QAtomicInt ProItemRefCount; typedef QAtomicInt ProItemRefCount;
#else #else
@@ -70,29 +73,58 @@ public:
PROITEM_EXPLICIT ProString(const char *str); PROITEM_EXPLICIT ProString(const char *str);
ProString(const QString &str, int offset, int length); ProString(const QString &str, int offset, int length);
void setValue(const QString &str); void setValue(const QString &str);
void clear() { m_string.clear(); m_length = 0; }
ProString &setSource(const ProString &other) { m_file = other.m_file; return *this; } ProString &setSource(const ProString &other) { m_file = other.m_file; return *this; }
ProString &setSource(const ProFile *pro) { m_file = pro; return *this; } ProString &setSource(const ProFile *pro) { m_file = pro; return *this; }
const ProFile *sourceFile() const { return m_file; } const ProFile *sourceFile() const { return m_file; }
QString toQString() const;
QString &toQString(QString &tmp) const;
ProString &operator+=(const ProString &other); ProString &operator+=(const ProString &other);
ProString &append(const ProString &other, bool *pending = 0); ProString &append(const ProString &other, bool *pending = 0);
ProString &append(const ProStringList &other, bool *pending = 0, bool skipEmpty1st = false); ProString &append(const ProStringList &other, bool *pending = 0, bool skipEmpty1st = false);
void chop(int n) { Q_ASSERT(n <= m_length); m_length -= n; }
void chopFront(int n) { Q_ASSERT(n <= m_length); m_offset += n; m_length -= n; }
bool operator==(const ProString &other) const { return toQStringRef() == other.toQStringRef(); } bool operator==(const ProString &other) const { return toQStringRef() == other.toQStringRef(); }
bool operator==(const QString &other) const { return toQStringRef() == other; } bool operator==(const QString &other) const { return toQStringRef() == other; }
bool operator==(QLatin1String other) const { return toQStringRef() == other; } bool operator==(QLatin1String other) const { return toQStringRef() == other; }
bool operator==(const char *other) const { return toQStringRef() == QLatin1String(other); }
bool operator!=(const ProString &other) const { return !(*this == other); } bool operator!=(const ProString &other) const { return !(*this == other); }
bool operator!=(const QString &other) const { return !(*this == other); } bool operator!=(const QString &other) const { return !(*this == other); }
bool operator!=(QLatin1String other) const { return !(*this == other); } bool operator!=(QLatin1String other) const { return !(*this == other); }
bool operator!=(const char *other) const { return !(*this == other); }
bool isNull() const { return m_string.isNull(); } bool isNull() const { return m_string.isNull(); }
bool isEmpty() const { return !m_length; } bool isEmpty() const { return !m_length; }
int length() const { return m_length; }
int size() const { return m_length; } int size() const { return m_length; }
QChar at(int i) const { Q_ASSERT((uint)i < (uint)m_length); return constData()[i]; }
const QChar *constData() const { return m_string.constData() + m_offset; } const QChar *constData() const { return m_string.constData() + m_offset; }
ProString mid(int off, int len = -1) const; ProString mid(int off, int len = -1) const;
ProString left(int len) const { return mid(0, len); } ProString left(int len) const { return mid(0, len); }
ProString right(int len) const { return mid(qMax(0, size() - len)); } ProString right(int len) const { return mid(qMax(0, size() - len)); }
ProString trimmed() const; ProString trimmed() const;
void clear() { m_string.clear(); m_length = 0; } int compare(const ProString &sub, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().compare(sub.toQStringRef(), cs); }
int compare(const QString &sub, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().compare(sub, cs); }
int compare(const char *sub, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().compare(QLatin1String(sub), cs); }
bool startsWith(const ProString &sub) const { return toQStringRef().startsWith(sub.toQStringRef()); }
bool startsWith(const QString &sub) const { return toQStringRef().startsWith(sub); }
bool startsWith(const char *sub) const { return toQStringRef().startsWith(QLatin1String(sub)); }
bool startsWith(QChar c) const { return toQStringRef().startsWith(c); }
bool endsWith(const ProString &sub) const { return toQStringRef().endsWith(sub.toQStringRef()); }
bool endsWith(const QString &sub) const { return toQStringRef().endsWith(sub); }
bool endsWith(const char *sub) const { return toQStringRef().endsWith(QLatin1String(sub)); }
bool endsWith(QChar c) const { return toQStringRef().endsWith(c); }
int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().indexOf(s, from, cs); }
int indexOf(const char *s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().indexOf(QLatin1String(s), from, cs); }
int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().indexOf(c, from, cs); }
int lastIndexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().lastIndexOf(s, from, cs); }
int lastIndexOf(const char *s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().lastIndexOf(QLatin1String(s), from, cs); }
int lastIndexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return toQStringRef().lastIndexOf(c, from, cs); }
bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return indexOf(s, 0, cs) >= 0; }
bool contains(const char *s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return indexOf(QLatin1String(s), 0, cs) >= 0; }
bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const { return indexOf(c, 0, cs) >= 0; }
int toInt(bool *ok = 0) const { return toQString().toInt(ok); } // XXX optimize
short toShort(bool *ok = 0) const { return toQString().toShort(ok); } // XXX optimize
static uint hash(const QChar *p, int n); static uint hash(const QChar *p, int n);
@@ -101,6 +133,11 @@ public:
ALWAYS_INLINE ProKey &toKey() { return *(ProKey *)this; } ALWAYS_INLINE ProKey &toKey() { return *(ProKey *)this; }
ALWAYS_INLINE const ProKey &toKey() const { return *(const ProKey *)this; } ALWAYS_INLINE const ProKey &toKey() const { return *(const ProKey *)this; }
QString toQString() const;
QString &toQString(QString &tmp) const;
QByteArray toLatin1() const { return toQStringRef().toLatin1(); }
private: private:
ProString(const ProKey &other); ProString(const ProKey &other);
ProString &operator=(const ProKey &other); ProString &operator=(const ProKey &other);
@@ -160,15 +197,49 @@ inline QString operator+(const ProString &one, const QString &two)
inline QString operator+(const QString &one, const ProString &two) inline QString operator+(const QString &one, const ProString &two)
{ return ProString(one) + two; } { return ProString(one) + two; }
inline QString operator+(const ProString &one, const char *two)
{ return one + ProString(two); } // XXX optimize
inline QString operator+(const char *one, const ProString &two)
{ return ProString(one) + two; } // XXX optimize
inline QString &operator+=(QString &that, const ProString &other)
{ return that += other.toQStringRef(); }
inline bool operator==(const QString &that, const ProString &other)
{ return other == that; }
inline bool operator!=(const QString &that, const ProString &other)
{ return !(other == that); }
QTextStream &operator<<(QTextStream &t, const ProString &str);
class ProStringList : public QVector<ProString> { class ProStringList : public QVector<ProString> {
public: public:
ProStringList() {} ProStringList() {}
ProStringList(const ProString &str) { *this << str; } ProStringList(const ProString &str) { *this << str; }
QString join(const QString &sep) const; explicit ProStringList(const QStringList &list);
void removeDuplicates();
QStringList toQStringList() const; QStringList toQStringList() const;
ProStringList &operator<<(const ProString &str)
{ QVector<ProString>::operator<<(str); return *this; }
int length() const { return size(); }
QString join(const QString &sep) const;
void removeAll(const ProString &str);
void removeAll(const char *str);
void removeAt(int idx) { remove(idx); }
void removeDuplicates();
bool contains(const ProString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
{ return contains(ProString(str), cs); }
bool contains(const char *str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
}; };
inline ProStringList operator+(const ProStringList &one, const ProStringList &two)
{ ProStringList ret = one; ret += two; return ret; }
typedef QHash<ProKey, ProStringList> ProValueMap; typedef QHash<ProKey, ProStringList> ProValueMap;
// These token definitions affect both ProFileEvaluator and ProWriter // These token definitions affect both ProFileEvaluator and ProWriter