diff options
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 187 |
1 files changed, 163 insertions, 24 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 93d81b89b6..06636098af 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -144,6 +144,7 @@ extern "C" void qt_toLatin1_mips_dsp_asm(uchar *dst, const ushort *src, int leng // internal qsizetype qFindStringBoyerMoore(QStringView haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs); +static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept; static inline qsizetype qt_last_index_of(QStringView haystack, QChar needle, qsizetype from, Qt::CaseSensitivity cs); static inline qsizetype qt_string_count(QStringView haystack, QStringView needle, Qt::CaseSensitivity cs); static inline qsizetype qt_string_count(QStringView haystack, QChar needle, Qt::CaseSensitivity cs); @@ -1645,10 +1646,9 @@ const QString::Null QString::null = { }; \snippet qstring/main.cpp 8 All functions except isNull() treat null strings the same as empty - strings. For example, toUtf8().constData() returns a pointer to a - '\\0' character for a null string (\e not a null pointer), and - QString() compares equal to QString(""). We recommend that you - always use the isEmpty() function and avoid isNull(). + strings. For example, toUtf8().constData() returns a valid pointer + (\e not nullptr) to a '\\0' character for a null string. We + recommend that you always use the isEmpty() function and avoid isNull(). \section1 Argument Formats @@ -3680,6 +3680,7 @@ bool QString::operator>(QLatin1String other) const noexcept \sa QT_NO_CAST_FROM_ASCII */ +#if QT_STRINGVIEW_LEVEL < 2 /*! Returns the index position of the first occurrence of the string \a str in this string, searching forward from index position \a @@ -3702,6 +3703,25 @@ int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const // ### Qt6: qsize return int(QtPrivate::findString(QStringView(unicode(), length()), from, QStringView(str.unicode(), str.length()), cs)); } +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! + \fn int QString::indexOf(QStringView str, int from, Qt::CaseSensitivity cs) const + \since 5.14 + \overload indexOf() + + Returns the index position of the first occurrence of the string view \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. + + If \a from is -1, the search starts at the last character; if it is + -2, at the next to last character and so on. + + \sa QStringView::indexOf(), lastIndexOf(), contains(), count() +*/ /*! \since 4.5 @@ -3738,9 +3758,10 @@ int QString::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) const int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const { // ### Qt6: qsize - return int(QtPrivate::findChar(QStringView(unicode(), length()), ch, from, cs)); + return int(qFindChar(QStringView(unicode(), length()), ch, from, cs)); } +#if QT_STRINGVIEW_LEVEL < 2 /*! \since 4.8 @@ -3758,6 +3779,7 @@ int QString::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) co // ### Qt6: qsize return int(QtPrivate::findString(QStringView(unicode(), length()), from, QStringView(str.unicode(), str.length()), cs)); } +#endif // QT_STRINGVIEW_LEVEL < 2 static int lastIndexOfHelper(const ushort *haystack, int from, const ushort *needle, int sl, Qt::CaseSensitivity cs) { @@ -4251,7 +4273,7 @@ int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const return int(qt_string_count(QStringView(unicode(), size()), QStringView(str.unicode(), str.size()), cs)); } - +#if QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const Returns \c true if this string contains an occurrence of the string @@ -4265,6 +4287,7 @@ int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ +#endif // QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QString::contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \since 5.3 @@ -4283,6 +4306,7 @@ int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const character \a ch; otherwise returns \c false. */ +#if QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QString::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \since 4.8 @@ -4294,6 +4318,20 @@ int QString::count(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! \fn bool QString::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.14 + \overload contains() + + Returns \c true if this string contains an occurrence of the string view + \a str; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa indexOf(), count() +*/ /*! \fn bool QString::contains(const QRegExp &rx) const @@ -4443,7 +4481,7 @@ int QString::indexOf(const QRegularExpression& re, int from) const expression \a re in the string, searching forward from index position \a from. Returns -1 if \a re didn't match anywhere. - If the match is successful and \a rmatch is not a null pointer, it also + If the match is successful and \a rmatch is not \nullptr, it also writes the results of the match into the QRegularExpressionMatch object pointed to by \a rmatch. @@ -4494,7 +4532,7 @@ int QString::lastIndexOf(const QRegularExpression &re, int from) const expression \a re in the string, which starts before the index position \a from. Returns -1 if \a re didn't match anywhere. - If the match is successful and \a rmatch is not a null pointer, it also + If the match is successful and \a rmatch is not \nullptr, it also writes the results of the match into the QRegularExpressionMatch object pointed to by \a rmatch. @@ -4545,14 +4583,14 @@ bool QString::contains(const QRegularExpression &re) const Returns \c true if the regular expression \a re matches somewhere in this string; otherwise returns \c false. - If the match is successful and \a match is not a null pointer, it also + If the match is successful and \a rmatch is not \nullptr, it also writes the results of the match into the QRegularExpressionMatch object - pointed to by \a match. + pointed to by \a rmatch. \sa QRegularExpression::match() */ -bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *match) const +bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *rmatch) const { if (!re.isValid()) { qWarning("QString::contains: invalid QRegularExpression object"); @@ -4560,8 +4598,8 @@ bool QString::contains(const QRegularExpression &re, QRegularExpressionMatch *ma } QRegularExpressionMatch m = re.match(*this); bool hasMatch = m.hasMatch(); - if (hasMatch && match) - *match = std::move(m); + if (hasMatch && rmatch) + *rmatch = std::move(m); return hasMatch; } @@ -5760,6 +5798,15 @@ QString QString::trimmed_helper(QString &str) were a QChar &. If you assign to it, the assignment will apply to the character in the QString from which you got the reference. + \note Before Qt 5.14 it was possible to use this operator to access + a character at an out-of-bounds position in the string, and + then assign to such position, causing the string to be + automatically resized. Furthermore, assigning a value to the + returned QCharRef would cause a detach of the string, even if the + string has been copied in the meanwhile (and the QCharRef kept + alive while the copy was taken). These behaviors are deprecated, + and will be changed in a future version of Qt. + \sa at() */ @@ -9481,6 +9528,40 @@ QString &QString::setRawData(const QChar *unicode, int size) */ /*! + \fn int QLatin1String::indexOf(QStringView str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \fn int QLatin1String::indexOf(QLatin1String l1, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \fn int QLatin1String::indexOf(QChar c, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.14 + + Returns the index position of the first occurrence of the string-view \a str, + Latin-1 string \a l1, or character \a ch, respectively, in this Latin-1 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. + + If \a from is -1, the search starts at the last character; if it is + -2, at the next to last character and so on. + + \sa QString::indexOf() +*/ + +/*! + \fn bool QLatin1String::contains(QStringView str, Qt::CaseSensitivity cs) const + \fn bool QLatin1String::contains(QLatin1String l1, Qt::CaseSensitivity cs) const + \fn bool QLatin1String::contains(QChar c, Qt::CaseSensitivity cs) const + \since 5.14 + + Returns \c true if this Latin-1 string contains an occurrence of the string-view + \a str, Latin-1 string \a l1, or character \a ch; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (the default), the search is + case-sensitive; otherwise the search is case-insensitive. + + \sa indexOf(), QStringView::contains(), QStringView::indexOf(), QString::indexOf() +*/ + +/*! \fn QLatin1String::const_iterator QLatin1String::begin() const \since 5.10 @@ -10227,8 +10308,8 @@ ownership of it, no memory is freed when instances are destroyed. /*! \fn bool QStringRef::isNull() const - Returns \c true if string() returns a null pointer or a pointer to a - null string; otherwise returns \c true. + Returns \c true if this string reference does not reference a string or if + the string it references is null (i.e. QString::isNull() is true). \sa size() */ @@ -11035,6 +11116,7 @@ QStringRef QString::midRef(int position, int n) const \sa QString::chop(), truncate() */ +#if QT_STRINGVIEW_LEVEL < 2 /*! \since 4.8 @@ -11055,6 +11137,25 @@ int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) co // ### Qt6: qsize return int(QtPrivate::findString(QStringView(unicode(), length()), from, QStringView(str.unicode(), str.length()), cs)); } +#endif // QT_STRINGVIEW_LEVEL < 2 + +/*! + \fn int QStringRef::indexOf(QStringView str, int from, Qt::CaseSensitivity cs) const + \since 5.14 + \overload indexOf() + + Returns the index position of the first occurrence of the string view \a str + in this string reference, 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. + + If \a from is -1, the search starts at the last character; if it is + -2, at the next to last character and so on. + + \sa QString::indexOf(), QStringView::indexOf(), lastIndexOf(), contains(), count() +*/ /*! \since 4.8 @@ -11069,7 +11170,7 @@ int QStringRef::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) co int QStringRef::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const { // ### Qt6: qsize - return int(QtPrivate::findChar(QStringView(unicode(), length()), ch, from, cs)); + return int(qFindChar(QStringView(unicode(), length()), ch, from, cs)); } /*! @@ -11093,6 +11194,7 @@ int QStringRef::indexOf(QLatin1String str, int from, Qt::CaseSensitivity cs) con return int(QtPrivate::findString(QStringView(unicode(), size()), from, str, cs)); } +#if QT_STRINGVIEW_LEVEL < 2 /*! \since 4.8 @@ -11112,6 +11214,7 @@ int QStringRef::indexOf(const QStringRef &str, int from, Qt::CaseSensitivity cs) // ### Qt6: qsize return int(QtPrivate::findString(QStringView(unicode(), size()), from, QStringView(str.unicode(), str.size()), cs)); } +#endif // QT_STRINGVIEW_LEVEL < 2 /*! \since 4.8 @@ -11442,7 +11545,7 @@ bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const return qt_ends_with(*this, str, cs); } - +#if QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QStringRef::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \since 4.8 @@ -11454,6 +11557,7 @@ bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ +#endif // QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QStringRef::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const @@ -11468,6 +11572,7 @@ bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const */ +#if QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QStringRef::contains(const QStringRef &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const \overload contains() \since 4.8 @@ -11480,6 +11585,7 @@ bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ +#endif // QT_STRINGVIEW_LEVEL < 2 /*! \fn bool QStringRef::contains(QLatin1String str, Qt::CaseSensitivity cs) const \since 4.8 @@ -11494,6 +11600,19 @@ bool QStringRef::endsWith(const QStringRef &str, Qt::CaseSensitivity cs) const \sa indexOf(), count() */ +/*! \fn bool QStringRef::contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const + \since 5.14 + \overload contains() + + Returns \c true if this string reference contains an occurrence of + the string view \a str; otherwise returns \c false. + + If \a cs is Qt::CaseSensitive (default), the search is + case sensitive; otherwise the search is case insensitive. + + \sa indexOf(), count() +*/ + static inline qsizetype qt_last_index_of(QStringView haystack, QChar needle, qsizetype from, Qt::CaseSensitivity cs) { @@ -11711,7 +11830,7 @@ bool QtPrivate::endsWith(QLatin1String haystack, QLatin1String needle, Qt::CaseS position \a from. Returns -1 if \a ch could not be found. */ -qsizetype QtPrivate::findChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept +static inline qsizetype qFindChar(QStringView str, QChar ch, qsizetype from, Qt::CaseSensitivity cs) noexcept { if (from < 0) from = qMax(from + str.size(), qsizetype(0)); @@ -11749,7 +11868,7 @@ qsizetype QtPrivate::findString(QStringView haystack0, qsizetype from, QStringVi return -1; if (sl == 1) - return QtPrivate::findChar(haystack0, needle0[0], from, cs); + return qFindChar(haystack0, needle0[0], from, cs); /* We use the Boyer-Moore algorithm in cases where the overhead @@ -11815,12 +11934,32 @@ qsizetype QtPrivate::findString(QStringView haystack, qsizetype from, QLatin1Str if (haystack.size() < needle.size()) return -1; - const char *latin1 = needle.latin1(); - const qsizetype len = needle.size(); - QVarLengthArray<ushort> s(len); - qt_from_latin1(s.data(), latin1, len); + QVarLengthArray<ushort> s(needle.size()); + qt_from_latin1(s.data(), needle.latin1(), needle.size()); + return QtPrivate::findString(haystack, from, QStringView(reinterpret_cast<const QChar*>(s.constData()), s.size()), cs); +} + +qsizetype QtPrivate::findString(QLatin1String haystack, qsizetype from, QStringView needle, Qt::CaseSensitivity cs) noexcept +{ + if (haystack.size() < needle.size()) + return -1; + + QVarLengthArray<ushort> s(haystack.size()); + qt_from_latin1(s.data(), haystack.latin1(), haystack.size()); + return QtPrivate::findString(QStringView(reinterpret_cast<const QChar*>(s.constData()), s.size()), from, needle, cs); +} + +qsizetype QtPrivate::findString(QLatin1String haystack, qsizetype from, QLatin1String needle, Qt::CaseSensitivity cs) noexcept +{ + if (haystack.size() < needle.size()) + return -1; - return QtPrivate::findString(haystack, from, QStringView(reinterpret_cast<const QChar*>(s.constData()), len), cs); + QVarLengthArray<ushort> h(haystack.size()); + qt_from_latin1(h.data(), haystack.latin1(), haystack.size()); + QVarLengthArray<ushort> n(needle.size()); + qt_from_latin1(n.data(), needle.latin1(), needle.size()); + return QtPrivate::findString(QStringView(reinterpret_cast<const QChar*>(h.constData()), h.size()), from, + QStringView(reinterpret_cast<const QChar*>(n.constData()), n.size()), cs); } /*! |