From 8ea11e0cf7f442f687089ffdf54de1f7540cb20b Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 22 May 2020 14:12:03 +0200 Subject: Remove most overloads taking a QStringRef from the QString API Add QStringView overloads where they were missing. This keeps things almost 100% source compatible. Task-number: QTBUG-84319 Change-Id: Ica8ee773b2ce655e9597097786117959e34b236e Reviewed-by: Thiago Macieira Reviewed-by: Volker Hilsheimer --- src/corelib/text/qstring.cpp | 125 +++--------------------- src/corelib/text/qstring.h | 38 ++----- tests/auto/corelib/text/qstring/tst_qstring.cpp | 8 -- 3 files changed, 22 insertions(+), 149 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 4083f89cdf..206d495647 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -3788,24 +3788,6 @@ int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const } #if QT_STRINGVIEW_LEVEL < 2 -/*! - \since 4.8 - - \overload indexOf() - - Returns the index position of the first occurrence of the string - reference \a str in this string, searching forward from index - position \a from. Returns -1 if \a str is not found. - - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. -*/ -int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const -{ - // ### Qt6: qsizetype - return int(QtPrivate::findString(QStringView(unicode(), length()), from, QStringView(str.unicode(), str.length()), cs)); -} - /*! Returns the index position of the last occurrence of the string \a str in this string, searching backward from index position \a @@ -3867,29 +3849,6 @@ int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const return int(qLastIndexOf(QStringView(*this), ch, from, cs)); } -#if QT_STRINGVIEW_LEVEL < 2 -/*! - \since 4.8 - \overload lastIndexOf() - - Returns the index position of the last occurrence of the string - reference \a str in this string, searching backward from index - position \a from. If \a from is -1 (default), the search starts at - the last character; if \a from is -2, at the next to last character - and so on. Returns -1 if \a str is not found. - - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. - - \sa indexOf(), contains(), count() -*/ -int QString::lastIndexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) const -{ - // ### Qt6: qsizetype - return int(QtPrivate::lastIndexOf(*this, from, str, cs)); -} -#endif // QT_STRINGVIEW_LEVEL < 2 - /*! \fn int QString::lastIndexOf(QStringView str, int from, Qt::CaseSensitivity cs) const \since 5.14 @@ -4080,7 +4039,7 @@ int QString::count(QChar ch, Qt::CaseSensitivity cs) const } /*! - \since 4.8 + \since 6.0 \overload count() Returns the number of (potentially overlapping) occurrences of the string reference \a str in this string. @@ -4090,10 +4049,10 @@ int QString::count(QChar ch, Qt::CaseSensitivity cs) const \sa contains(), indexOf() */ -int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const +int QString::count(QStringView str, Qt::CaseSensitivity cs) const { // ### Qt6: qsizetype - return int(QtPrivate::count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); + return int(QtPrivate::count(*this, str, cs)); } #if QT_STRINGVIEW_LEVEL < 2 @@ -4705,24 +4664,6 @@ bool QString::startsWith(QChar c, Qt::CaseSensitivity cs) const return qt_starts_with(*this, c, cs); } -#if QT_STRINGVIEW_LEVEL < 2 -/*! - \since 4.8 - \overload - Returns \c true if the string starts with the string reference \a s; - otherwise returns \c false. - - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. - - \sa endsWith() -*/ -bool QString::startsWith(const QStringRef &s, Qt::CaseSensitivity cs) const -{ - return qt_starts_with(*this, s, cs); -} -#endif - /*! \fn bool QString::startsWith(QStringView str, Qt::CaseSensitivity cs) const \since 5.10 @@ -4753,22 +4694,6 @@ bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs) const { return qt_ends_with(*this, s, cs); } - -/*! - \since 4.8 - \overload endsWith() - Returns \c true if the string ends with the string reference \a s; - otherwise returns \c false. - - If \a cs is Qt::CaseSensitive (default), the search is case - sensitive; otherwise the search is case insensitive. - - \sa startsWith() -*/ -bool QString::endsWith(const QStringRef &s, Qt::CaseSensitivity cs) const -{ - return qt_ends_with(*this, s, cs); -} #endif // QT_STRINGVIEW_LEVEL < 2 /*! @@ -5880,17 +5805,6 @@ int QString::compare(QLatin1String other, Qt::CaseSensitivity cs) const noexcept return qt_compare_strings(*this, other, cs); } -#if QT_STRINGVIEW_LEVEL < 2 -/*! - \fn int QString::compare(const QStringRef &ref, Qt::CaseSensitivity cs = Qt::CaseSensitive) const - \overload compare() - - Compares the string reference, \a ref, with the string and returns - an integer less than, equal to, or greater than zero if the string - is less than, equal to, or greater than \a ref. -*/ -#endif - /*! \internal \since 5.0 @@ -5912,7 +5826,12 @@ int QString::compare_helper(const QChar *data1, int length1, const char *data2, } /*! - \fn int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) + \fn int QString::compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) + \overload compare() +*/ + +/*! + \fn int QString::compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) \overload compare() */ @@ -5946,8 +5865,8 @@ int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2, */ /*! - \fn int QString::localeAwareCompare(const QStringRef &other) const - \since 4.5 + \fn int QString::localeAwareCompare(QStringView other) const + \since 6.0 \overload localeAwareCompare() Compares this string with the \a other string and returns an @@ -5962,8 +5881,8 @@ int QString::compare_helper(const QChar *data1, int length1, QLatin1String s2, */ /*! - \fn int QString::localeAwareCompare(const QString &s1, const QStringRef &s2) - \since 4.5 + \fn int QString::localeAwareCompare(QStringView s1, QStringView s2) + \since 6.0 \overload localeAwareCompare() Compares \a s1 with \a s2 and returns an integer less than, equal @@ -10658,24 +10577,6 @@ QStringRef QStringRef::appendTo(QString *string) const Appends the given string view \a str to this string and returns the result. */ -/*! - \fn QString &QString::append(const QStringRef &reference) - \since 4.4 - - Appends the given string \a reference to this string and returns the result. - */ -QString &QString::append(const QStringRef &str) -{ - if (str.string() == this) { - str.appendTo(this); - } else if (!str.isNull()) { - int oldSize = size(); - resize(oldSize + str.size()); - memcpy(data() + oldSize, str.unicode(), str.size() * sizeof(QChar)); - } - return *this; -} - /*! \fn QStringRef::left(int n) const \since 5.2 diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index 00c61063da..c6ef27c481 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -399,7 +399,6 @@ public: 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: qsizetype @@ -407,7 +406,6 @@ public: int lastIndexOf(QLatin1String s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #if QT_STRINGVIEW_LEVEL < 2 int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - int lastIndexOf(const QStringRef &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #endif Q_REQUIRED_RESULT int lastIndexOf(QStringView s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept @@ -416,13 +414,12 @@ public: 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(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; + int count(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #if QT_CONFIG(regularexpression) int indexOf(const QRegularExpression &re, int from = 0, @@ -469,7 +466,6 @@ public: #if QT_STRINGVIEW_LEVEL < 2 bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - bool startsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #endif Q_REQUIRED_RESULT bool startsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::startsWith(*this, s, cs); } @@ -478,7 +474,6 @@ public: #if QT_STRINGVIEW_LEVEL < 2 bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; - bool endsWith(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #endif Q_REQUIRED_RESULT bool endsWith(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::endsWith(*this, s, cs); } @@ -535,7 +530,6 @@ public: QString &insert(int i, const QChar *uc, int len); #if QT_STRINGVIEW_LEVEL < 2 inline QString &insert(int i, const QString &s) { return insert(i, s.constData(), s.length()); } - inline QString &insert(int i, const QStringRef &s); #endif inline QString &insert(int i, QStringView v) { return insert(i, v.data(), v.length()); } QString &insert(int i, QLatin1String s); @@ -544,7 +538,6 @@ public: QString &append(const QChar *uc, int len); #if QT_STRINGVIEW_LEVEL < 2 QString &append(const QString &s); - QString &append(const QStringRef &s); #endif inline QString &append(QStringView v) { return append(v.data(), v.length()); } QString &append(QLatin1String s); @@ -553,7 +546,6 @@ public: inline QString &prepend(const QChar *uc, int len) { return insert(0, uc, len); } #if QT_STRINGVIEW_LEVEL < 2 inline QString &prepend(const QString &s) { return insert(0, s); } - inline QString &prepend(const QStringRef &s) { return insert(0, s); } #endif inline QString &prepend(QStringView v) { return prepend(v.data(), v.length()); } inline QString &prepend(QLatin1String s) { return insert(0, s); } @@ -568,7 +560,6 @@ public: #if QT_STRINGVIEW_LEVEL < 2 inline QString &operator+=(const QString &s) { return append(s); } - inline QString &operator+=(const QStringRef &s) { return append(s); } #endif inline QString &operator+=(QStringView v) { return append(v); } inline QString &operator+=(QLatin1String s) { return append(s); } @@ -737,7 +728,6 @@ public: #if QT_STRINGVIEW_LEVEL < 2 int compare(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; - inline int compare(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; #endif int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; inline int compare(QStringView s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept; @@ -754,16 +744,17 @@ public: static inline int compare(QLatin1String s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept { return -s2.compare(s1, cs); } - - static int compare(const QString &s1, const QStringRef &s2, - Qt::CaseSensitivity = Qt::CaseSensitive) noexcept; + static int compare(const QString &s1, QStringView s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept + { return s1.compare(s2, cs); } + static int compare(QStringView s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive) noexcept + { return -s2.compare(s1, cs); } int localeAwareCompare(const QString& s) const; + int localeAwareCompare(QStringView s) const; static int localeAwareCompare(const QString& s1, const QString& s2) { return s1.localeAwareCompare(s2); } - int localeAwareCompare(const QStringRef &s) const; - static int localeAwareCompare(const QString& s1, const QStringRef& s2); + static int localeAwareCompare(QStringView s1, QStringView s2); // ### Qt6: make inline except for the long long versions short toShort(bool *ok=nullptr, int base=10) const; @@ -1206,8 +1197,6 @@ inline QString::const_iterator QString::constEnd() const #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; } @@ -1680,14 +1669,8 @@ inline bool operator> (const QStringRef &lhs, const QString &rhs) noexcept { ret inline bool operator<=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs >= lhs; } inline bool operator>=(const QStringRef &lhs, const QString &rhs) noexcept { return rhs <= lhs; } -#if QT_STRINGVIEW_LEVEL < 2 -inline int QString::compare(const QStringRef &s, Qt::CaseSensitivity cs) const noexcept -{ return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); } -#endif inline int QString::compare(QStringView s, Qt::CaseSensitivity cs) const noexcept { return -s.compare(*this, cs); } -inline int QString::compare(const QString &s1, const QStringRef &s2, Qt::CaseSensitivity cs) noexcept -{ return QString::compare_helper(s1.constData(), s1.length(), s2.constData(), s2.length(), cs); } inline int QStringRef::compare(const QString &s, Qt::CaseSensitivity cs) const noexcept { return QString::compare_helper(constData(), length(), s.constData(), s.length(), cs); } inline int QStringRef::compare(const QStringRef &s, Qt::CaseSensitivity cs) const noexcept @@ -1855,9 +1838,9 @@ inline QT_ASCII_CAST_WARN bool operator>=(const char *s1, const QStringRef &s2) { return QString::compare_helper(s2.constData(), s2.size(), s1, -1) <= 0; } #endif // !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) -inline int QString::localeAwareCompare(const QStringRef &s) const +inline int QString::localeAwareCompare(QStringView s) const { return localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } -inline int QString::localeAwareCompare(const QString& s1, const QStringRef& s2) +inline int QString::localeAwareCompare(QStringView s1, QStringView s2) { return localeAwareCompare_helper(s1.constData(), s1.length(), s2.constData(), s2.length()); } inline int QStringRef::localeAwareCompare(const QString &s) const { return QString::localeAwareCompare_helper(constData(), length(), s.constData(), s.length()); } @@ -1881,9 +1864,6 @@ inline bool QStringRef::contains(QChar c, 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) -{ return insert(i, s.constData(), s.length()); } - #if !defined(QT_USE_FAST_OPERATOR_PLUS) && !defined(QT_USE_QSTRINGBUILDER) inline QString operator+(const QString &s1, const QStringRef &s2) { QString t; t.reserve(s1.size() + s2.size()); t += s1; t += s2; return t; } diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 0501a5765d..5c46ae6ae1 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -393,8 +393,6 @@ private slots: void prepend_qstring() { prepend_impl(); } void prepend_qstring_data() { prepend_data(true); } - void prepend_qstringref() { prepend_impl(); } - void prepend_qstringref_data() { prepend_data(true); } void prepend_qstringview() { prepend_impl(); } void prepend_qstringview_data() { prepend_data(true); } void prepend_qlatin1string() { prepend_impl(); } @@ -414,8 +412,6 @@ private slots: void append_qstring() { append_impl(); } void append_qstring_data() { append_data(); } - void append_qstringref() { append_impl(); } - void append_qstringref_data() { append_data(); } void append_qstringview() { append_impl(); } void append_qstringview_data() { append_data(true); } void append_qlatin1string() { append_impl(); } @@ -436,8 +432,6 @@ private slots: void operator_pluseq_qstring() { operator_pluseq_impl(); } void operator_pluseq_qstring_data() { operator_pluseq_data(); } - void operator_pluseq_qstringref() { operator_pluseq_impl(); } - void operator_pluseq_qstringref_data() { operator_pluseq_data(); } void operator_pluseq_qstringview() { operator_pluseq_impl(); } void operator_pluseq_qstringview_data() { operator_pluseq_data(true); } void operator_pluseq_qlatin1string() { operator_pluseq_impl(); } @@ -459,8 +453,6 @@ private slots: void insert_qstring() { insert_impl(); } void insert_qstring_data() { insert_data(true); } - void insert_qstringref() { insert_impl(); } - void insert_qstringref_data() { insert_data(true); } void insert_qstringview() { insert_impl(); } void insert_qstringview_data() { insert_data(true); } void insert_qlatin1string() { insert_impl(); } -- cgit v1.2.3