diff --git a/src/shared/proparser/proitems.cpp b/src/shared/proparser/proitems.cpp index 1b16ded4d58..5dd7898cd0f 100644 --- a/src/shared/proparser/proitems.cpp +++ b/src/shared/proparser/proitems.cpp @@ -33,6 +33,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -289,6 +290,12 @@ ProString ProString::trimmed() const return ret; } +QTextStream &operator<<(QTextStream &t, const ProString &str) +{ + t << str.toQString(); // XXX optimize ... somehow + return t; +} + QString ProStringList::join(const QString &sep) const { int totalLength = 0; @@ -313,6 +320,20 @@ QString ProStringList::join(const QString &sep) const 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() { int n = size(); @@ -332,6 +353,13 @@ void ProStringList::removeDuplicates() 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 ret; @@ -341,6 +369,22 @@ QStringList ProStringList::toQStringList() const 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) : m_refCount(1), m_fileName(fileName), diff --git a/src/shared/proparser/proitems.h b/src/shared/proparser/proitems.h index 7aa893a0a83..3a4b69b5b6e 100644 --- a/src/shared/proparser/proitems.h +++ b/src/shared/proparser/proitems.h @@ -32,12 +32,15 @@ #define PROITEMS_H #include "qmake_global.h" + #include #include #include QT_BEGIN_NAMESPACE +class QTextStream; + #ifdef PROPARSER_THREAD_SAFE typedef QAtomicInt ProItemRefCount; #else @@ -70,29 +73,58 @@ public: PROITEM_EXPLICIT ProString(const char *str); ProString(const QString &str, int offset, int length); 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 ProFile *pro) { m_file = pro; return *this; } const ProFile *sourceFile() const { return m_file; } - QString toQString() const; - QString &toQString(QString &tmp) const; + ProString &operator+=(const ProString &other); ProString &append(const ProString &other, bool *pending = 0); 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 QString &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 QString &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 isEmpty() const { return !m_length; } + int length() 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; } 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; } + 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); @@ -101,6 +133,11 @@ public: ALWAYS_INLINE ProKey &toKey() { return *(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: ProString(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) { 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 { public: ProStringList() {} ProStringList(const ProString &str) { *this << str; } - QString join(const QString &sep) const; - void removeDuplicates(); + explicit ProStringList(const QStringList &list); QStringList toQStringList() const; + + ProStringList &operator<<(const ProString &str) + { QVector::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 ProValueMap; // These token definitions affect both ProFileEvaluator and ProWriter