diff options
Diffstat (limited to 'src/corelib/tools/qstring.h')
-rw-r--r-- | src/corelib/tools/qstring.h | 126 |
1 files changed, 111 insertions, 15 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index e4798eb51b..da8260a999 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -2,6 +2,7 @@ ** ** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2019 Mail.ru Group. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -130,6 +131,20 @@ public: Q_REQUIRED_RESULT inline bool endsWith(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::endsWith(*this, QStringView(&c, 1), cs); } + Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsize + Q_REQUIRED_RESULT int indexOf(QLatin1String s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsize + Q_REQUIRED_RESULT inline int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::findString(*this, from, QStringView(&c, 1), cs)); } // ### Qt6: qsize + + Q_REQUIRED_RESULT bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return indexOf(s, 0, cs) != -1; } + Q_REQUIRED_RESULT bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return indexOf(s, 0, cs) != -1; } + Q_REQUIRED_RESULT inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return indexOf(QStringView(&c, 1), 0, cs) != -1; } + using value_type = const char; using reference = value_type&; using const_reference = reference; @@ -214,6 +229,10 @@ bool QStringView::startsWith(QLatin1String s, Qt::CaseSensitivity cs) const noex { return QtPrivate::startsWith(*this, s, cs); } bool QStringView::endsWith(QLatin1String s, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::endsWith(*this, s, cs); } +qsizetype QStringView::indexOf(QLatin1String s, qsizetype from, Qt::CaseSensitivity cs) const noexcept +{ return QtPrivate::findString(*this, from, s, cs); } +bool QStringView::contains(QLatin1String s, Qt::CaseSensitivity cs) const noexcept +{ return indexOf(s, 0, cs) != qsizetype(-1); } class Q_CORE_EXPORT QString { @@ -261,9 +280,9 @@ public: inline const QChar at(int i) const; const QChar operator[](int i) const; - QCharRef operator[](int i); + Q_REQUIRED_RESULT QCharRef operator[](int i); const QChar operator[](uint i) const; - QCharRef operator[](uint i); + Q_REQUIRED_RESULT QCharRef operator[](uint i); Q_REQUIRED_RESULT inline QChar front() const { return at(0); } Q_REQUIRED_RESULT inline QCharRef front(); @@ -328,18 +347,25 @@ public: static QString asprintf(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2); int indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int indexOf(QLatin1String s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_STRINGVIEW_LEVEL < 2 + int indexOf(const QString &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int indexOf(const QStringRef &s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsize int lastIndexOf(QChar c, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(QLatin1String s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(const QStringRef &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; inline bool contains(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_STRINGVIEW_LEVEL < 2 inline bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; inline bool contains(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + inline bool contains(QLatin1String s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; @@ -361,7 +387,7 @@ public: int lastIndexOf(const QRegularExpression &re, int from = -1) const; int lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads bool contains(const QRegularExpression &re) const; - bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const; // ### Qt 6: merge overloads + bool contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch) const; // ### Qt 6: merge overloads int count(const QRegularExpression &re) const; #endif @@ -531,6 +557,30 @@ public: Q_REQUIRED_RESULT QStringList split(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const; Q_REQUIRED_RESULT QVector<QStringRef> splitRef(const QRegularExpression &sep, SplitBehavior behavior = KeepEmptyParts) const; #endif + +private: + static Q_DECL_CONSTEXPR SplitBehavior _sb(Qt::SplitBehavior sb) Q_DECL_NOTHROW + { return sb & Qt::SkipEmptyParts ? SkipEmptyParts : KeepEmptyParts; } +public: + + Q_REQUIRED_RESULT inline QStringList split(const QString &sep, Qt::SplitBehavior behavior, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT inline QVector<QStringRef> splitRef(const QString &sep, Qt::SplitBehavior behavior, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT inline QStringList split(QChar sep, Qt::SplitBehavior behavior, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT inline QVector<QStringRef> splitRef(QChar sep, Qt::SplitBehavior behavior, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#ifndef QT_NO_REGEXP + Q_REQUIRED_RESULT inline QStringList split(const QRegExp &sep, Qt::SplitBehavior behavior) const; + Q_REQUIRED_RESULT inline QVector<QStringRef> splitRef(const QRegExp &sep, Qt::SplitBehavior behavior) const; +#endif +#ifndef QT_NO_REGULAREXPRESSION + Q_REQUIRED_RESULT inline QStringList split(const QRegularExpression &sep, Qt::SplitBehavior behavior) const; + Q_REQUIRED_RESULT inline QVector<QStringRef> splitRef(const QRegularExpression &sep, Qt::SplitBehavior behavior) const; +#endif + + enum NormalizationForm { NormalizationForm_D, NormalizationForm_C, @@ -1030,8 +1080,11 @@ inline QString QString::fromWCharArray(const wchar_t *string, int size) : fromUcs4(reinterpret_cast<const uint *>(string), size); } - -class Q_CORE_EXPORT QCharRef { +class +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) +Q_CORE_EXPORT +#endif +QCharRef { QString &s; int i; inline QCharRef(QString &str, int idx) @@ -1043,10 +1096,33 @@ public: // all this is not documented: We just say "like QChar" and let it be. inline operator QChar() const - { return i < s.d->size ? s.d->data()[i] : 0; } + { + using namespace QtPrivate::DeprecatedRefClassBehavior; + if (Q_LIKELY(i < s.d->size)) + return s.d->data()[i]; +#ifdef QT_DEBUG + warn(WarningType::OutOfRange, EmittingClass::QCharRef); +#endif + return 0; + } inline QCharRef &operator=(QChar c) - { if (i >= s.d->size) s.resize(i + 1, QLatin1Char(' ')); else s.detach(); - s.d->data()[i] = c.unicode(); return *this; } + { + using namespace QtPrivate::DeprecatedRefClassBehavior; + if (Q_UNLIKELY(i >= s.d->size)) { +#ifdef QT_DEBUG + warn(WarningType::OutOfRange, EmittingClass::QCharRef); +#endif + s.resize(i + 1, QLatin1Char(' ')); + } else { +#ifdef QT_DEBUG + if (Q_UNLIKELY(!s.isDetached())) + warn(WarningType::DelayedDetach, EmittingClass::QCharRef); +#endif + s.detach(); + } + s.d->data()[i] = c.unicode(); + return *this; + } // An operator= for each QChar cast constructors #ifndef QT_NO_CAST_FROM_ASCII @@ -1155,9 +1231,9 @@ inline void QString::squeeze() inline QString &QString::setUtf16(const ushort *autf16, int asize) { return setUnicode(reinterpret_cast<const QChar *>(autf16), asize); } inline QCharRef QString::operator[](int i) -{ Q_ASSERT(i >= 0); return QCharRef(*this, i); } +{ Q_ASSERT(i >= 0); detach(); return QCharRef(*this, i); } inline QCharRef QString::operator[](uint i) -{ return QCharRef(*this, i); } +{ detach(); return QCharRef(*this, i); } inline QCharRef QString::front() { return operator[](0); } inline QCharRef QString::back() { return operator[](size() - 1); } inline QString::iterator QString::begin() @@ -1176,14 +1252,18 @@ inline QString::const_iterator QString::cend() const { return reinterpret_cast<const QChar*>(d->data() + d->size); } inline QString::const_iterator QString::constEnd() const { return reinterpret_cast<const QChar*>(d->data() + d->size); } +#if QT_STRINGVIEW_LEVEL < 2 inline bool QString::contains(const QString &s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } inline bool QString::contains(const QStringRef &s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } +#endif inline bool QString::contains(QLatin1String s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } inline bool QString::contains(QChar c, Qt::CaseSensitivity cs) const { return indexOf(c, 0, cs) != -1; } +inline bool QString::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept +{ return indexOf(s, 0, cs) != -1; } #if QT_DEPRECATED_SINCE(5, 9) inline bool operator==(QString::Null, QString::Null) { return true; } @@ -1451,19 +1531,26 @@ public: inline int count() const { return m_size; } inline int length() const { return m_size; } +#if QT_STRINGVIEW_LEVEL < 2 int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif + Q_REQUIRED_RESULT int indexOf(QStringView s, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept + { return int(QtPrivate::findString(*this, from, s, cs)); } // ### Qt6: qsize int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int indexOf(QLatin1String str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int indexOf(const QStringRef &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(QLatin1String str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(const QStringRef &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#if QT_STRINGVIEW_LEVEL < 2 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; +#endif inline bool contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - inline bool contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + inline bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int count(QChar c, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; @@ -1474,6 +1561,11 @@ public: Q_REQUIRED_RESULT QVector<QStringRef> split(QChar sep, QString::SplitBehavior behavior = QString::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT inline QVector<QStringRef> split(const QString &sep, Qt::SplitBehavior behavior, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT inline QVector<QStringRef> split(QChar sep, Qt::SplitBehavior behavior, + Qt::CaseSensitivity cs = Qt::CaseSensitive) const; + Q_REQUIRED_RESULT QStringRef left(int n) const; Q_REQUIRED_RESULT QStringRef right(int n) const; Q_REQUIRED_RESULT QStringRef mid(int pos, int n = -1) const; @@ -1820,13 +1912,17 @@ inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QString &s inline int QStringRef::localeAwareCompare(const QStringRef &s1, const QStringRef &s2) { return QString::localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } +#if QT_STRINGVIEW_LEVEL < 2 inline bool QStringRef::contains(const QString &s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } +inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const +{ return indexOf(s, 0, cs) != -1; } +#endif inline bool QStringRef::contains(QLatin1String s, Qt::CaseSensitivity cs) const { return indexOf(s, 0, cs) != -1; } inline bool QStringRef::contains(QChar c, Qt::CaseSensitivity cs) const { return indexOf(c, 0, cs) != -1; } -inline bool QStringRef::contains(const QStringRef &s, Qt::CaseSensitivity cs) const +inline bool QStringRef::contains(QStringView s, Qt::CaseSensitivity cs) const noexcept { return indexOf(s, 0, cs) != -1; } inline QString &QString::insert(int i, const QStringRef &s) |