diff options
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 142 |
1 files changed, 117 insertions, 25 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 029499039c..aa602559fe 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); @@ -826,9 +827,9 @@ static int ucstricmp(const QChar *a, const QChar *ae, const QChar *b, const QCha uint alast = 0; uint blast = 0; while (a < e) { -// qDebug() << hex << alast << blast; -// qDebug() << hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast); -// qDebug() << hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast); +// qDebug() << Qt::hex << alast << blast; +// qDebug() << Qt::hex << "*a=" << *a << "alast=" << alast << "folded=" << foldCase (*a, alast); +// qDebug() << Qt::hex << "*b=" << *b << "blast=" << blast << "folded=" << foldCase (*b, blast); int diff = foldCase(a->unicode(), alast) - foldCase(b->unicode(), blast); if ((diff)) return diff; @@ -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) { @@ -4443,7 +4465,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 +4516,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 +4567,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 +4582,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 +5782,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 +9512,25 @@ 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 QLatin1String::const_iterator QLatin1String::begin() const \since 5.10 @@ -10227,8 +10277,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 +11085,7 @@ QStringRef QString::midRef(int position, int n) const \sa QString::chop(), truncate() */ +#if QT_STRINGVIEW_LEVEL < 2 /*! \since 4.8 @@ -11055,6 +11106,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 +11139,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 +11163,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 +11183,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 @@ -11711,7 +11783,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 +11821,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 +11887,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); } /*! |