diff options
author | Matthias Rauter <matthias.rauter@qt.io> | 2024-02-09 13:29:57 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2024-04-29 07:40:29 +0000 |
commit | 737c6680b48d1e02832d9b71be5e9620f26ec484 (patch) | |
tree | a747aaccaea27079a12158683ea042e50771c623 /src/corelib/text | |
parent | ab0b2a490eb98f3216403e87e4ea108a10fbeed2 (diff) |
QRegularExpressionMatch: port API from QString to QAnyStringView keys
The QRegularExpression capture-by-name keys are currently QStringViews,
but are only ever used to compare them against a const char16_t*, so
this API is a perfect candidate for replacing all of these overload sets
with a single QAnyStringView function.
[ChangeLog][QtCore][QRegularExpression] Keys can now be passed as
QAnyStringView (was QStringView).
Fixes: QTBUG-103097
Change-Id: I1a80e85f301cc08370d70b3b5eb0ae10c6a51f33
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r-- | src/corelib/text/qregularexpression.cpp | 79 | ||||
-rw-r--r-- | src/corelib/text/qregularexpression.h | 23 |
2 files changed, 45 insertions, 57 deletions
diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 95fd0e3d9a..67c422568b 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -721,7 +721,7 @@ struct QRegularExpressionPrivate : QSharedData CheckSubjectStringOption checkSubjectStringOption = CheckSubjectString, const QRegularExpressionMatchPrivate *previous = nullptr) const; - int captureIndexForName(QStringView name) const; + int captureIndexForName(QAnyStringView name) const; // sizeof(QSharedData) == 4, so start our members with an enum QRegularExpression::PatternOptions patternOptions; @@ -1013,7 +1013,7 @@ void QRegularExpressionPrivate::optimizePattern() Returns the capturing group number for the given name. Duplicated names for capturing groups are not supported. */ -int QRegularExpressionPrivate::captureIndexForName(QStringView name) const +int QRegularExpressionPrivate::captureIndexForName(QAnyStringView name) const { Q_ASSERT(!name.isEmpty()); @@ -2217,8 +2217,7 @@ int QRegularExpressionMatch::lastCapturedIndex() const } /*! - \fn bool QRegularExpressionMatch::hasCaptured(const QString &name) const - \fn bool QRegularExpressionMatch::hasCaptured(QStringView name) const + \fn bool QRegularExpressionMatch::hasCaptured(QAnyStringView name) const \since 6.3 Returns true if the capturing group named \a name captured something @@ -2235,9 +2234,12 @@ int QRegularExpressionMatch::lastCapturedIndex() const Similarly, a capturing group may capture a substring of length 0; this function will return \c{true} for such a capturing group. + \note In Qt versions prior to 6.8, this function took QString or + QStringView, not QAnyStringView. + \sa captured(), hasMatch() */ -bool QRegularExpressionMatch::hasCaptured(QStringView name) const +bool QRegularExpressionMatch::hasCaptured(QAnyStringView name) const { const int nth = d->regularExpression.d->captureIndexForName(name); return hasCaptured(nth); @@ -2317,17 +2319,6 @@ QStringView QRegularExpressionMatch::capturedView(int nth) const return d->subject.mid(start, capturedLength(nth)); } -/*! \fn QString QRegularExpressionMatch::captured(const QString &name) const - - Returns the substring captured by the capturing group named \a name. - - If the named capturing group \a name did not capture a string, or if - there is no capturing group named \a name, returns a null QString. - - \sa capturedView(), capturedStart(), capturedEnd(), capturedLength(), - QString::isNull() -*/ - /*! \since 5.10 @@ -2336,10 +2327,13 @@ QStringView QRegularExpressionMatch::capturedView(int nth) const If the named capturing group \a name did not capture a string, or if there is no capturing group named \a name, returns a null QString. + \note In Qt versions prior to 6.8, this function took QString or + QStringView, not QAnyStringView. + \sa capturedView(), capturedStart(), capturedEnd(), capturedLength(), QString::isNull() */ -QString QRegularExpressionMatch::captured(QStringView name) const +QString QRegularExpressionMatch::captured(QAnyStringView name) const { if (name.isEmpty()) { qWarning("QRegularExpressionMatch::captured: empty capturing group name passed"); @@ -2358,10 +2352,13 @@ QString QRegularExpressionMatch::captured(QStringView name) const If the named capturing group \a name did not capture a string, or if there is no capturing group named \a name, returns a null QStringView. + \note In Qt versions prior to 6.8, this function took QString or + QStringView, not QAnyStringView. + \sa captured(), capturedStart(), capturedEnd(), capturedLength(), QStringView::isNull() */ -QStringView QRegularExpressionMatch::capturedView(QStringView name) const +QStringView QRegularExpressionMatch::capturedView(QAnyStringView name) const { if (name.isEmpty()) { qWarning("QRegularExpressionMatch::capturedView: empty capturing group name passed"); @@ -2433,37 +2430,6 @@ qsizetype QRegularExpressionMatch::capturedEnd(int nth) const return d->capturedOffsets.at(nth * 2 + 1); } -/*! \fn qsizetype QRegularExpressionMatch::capturedStart(const QString &name) const - - Returns the offset inside the subject string corresponding to the starting - position of the substring captured by the capturing group named \a name. - If the capturing group named \a name did not capture a string or doesn't - exist, returns -1. - - \sa capturedEnd(), capturedLength(), captured() -*/ - -/*! \fn qsizetype QRegularExpressionMatch::capturedLength(const QString &name) const - - Returns the length of the substring captured by the capturing group named - \a name. - - \note This function returns 0 if the capturing group named \a name did not - capture a string or doesn't exist. - - \sa capturedStart(), capturedEnd(), captured() -*/ - -/*! \fn qsizetype QRegularExpressionMatch::capturedEnd(const QString &name) const - - Returns the offset inside the subject string immediately after the ending - position of the substring captured by the capturing group named \a name. If - the capturing group named \a name did not capture a string or doesn't - exist, returns -1. - - \sa capturedStart(), capturedLength(), captured() -*/ - /*! \since 5.10 @@ -2472,9 +2438,12 @@ qsizetype QRegularExpressionMatch::capturedEnd(int nth) const If the capturing group named \a name did not capture a string or doesn't exist, returns -1. + \note In Qt versions prior to 6.8, this function took QString or + QStringView, not QAnyStringView. + \sa capturedEnd(), capturedLength(), captured() */ -qsizetype QRegularExpressionMatch::capturedStart(QStringView name) const +qsizetype QRegularExpressionMatch::capturedStart(QAnyStringView name) const { if (name.isEmpty()) { qWarning("QRegularExpressionMatch::capturedStart: empty capturing group name passed"); @@ -2495,9 +2464,12 @@ qsizetype QRegularExpressionMatch::capturedStart(QStringView name) const \note This function returns 0 if the capturing group named \a name did not capture a string or doesn't exist. + \note In Qt versions prior to 6.8, this function took QString or + QStringView, not QAnyStringView. + \sa capturedStart(), capturedEnd(), captured() */ -qsizetype QRegularExpressionMatch::capturedLength(QStringView name) const +qsizetype QRegularExpressionMatch::capturedLength(QAnyStringView name) const { if (name.isEmpty()) { qWarning("QRegularExpressionMatch::capturedLength: empty capturing group name passed"); @@ -2517,9 +2489,12 @@ qsizetype QRegularExpressionMatch::capturedLength(QStringView name) const the capturing group named \a name did not capture a string or doesn't exist, returns -1. + \note In Qt versions prior to 6.8, this function took QString or + QStringView, not QAnyStringView. + \sa capturedStart(), capturedLength(), captured() */ -qsizetype QRegularExpressionMatch::capturedEnd(QStringView name) const +qsizetype QRegularExpressionMatch::capturedEnd(QAnyStringView name) const { if (name.isEmpty()) { qWarning("QRegularExpressionMatch::capturedEnd: empty capturing group name passed"); diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h index ed838327ef..2b51b94b15 100644 --- a/src/corelib/text/qregularexpression.h +++ b/src/corelib/text/qregularexpression.h @@ -213,18 +213,26 @@ public: int lastCapturedIndex() const; +#if QT_CORE_REMOVED_SINCE(6, 8) bool hasCaptured(const QString &name) const - { return hasCaptured(QStringView(name)); } + { return hasCaptured(qToAnyStringViewIgnoringNull(name)); } bool hasCaptured(QStringView name) const; +#endif + bool hasCaptured(QAnyStringView name) const; bool hasCaptured(int nth) const; QString captured(int nth = 0) const; QStringView capturedView(int nth = 0) const; +#if QT_CORE_REMOVED_SINCE(6, 8) QString captured(const QString &name) const - { return captured(QStringView(name)); } + { return captured(qToAnyStringViewIgnoringNull(name)); } + QString captured(QStringView name) const; QStringView capturedView(QStringView name) const; +#endif + QString captured(QAnyStringView name) const; + QStringView capturedView(QAnyStringView name) const; QStringList capturedTexts() const; @@ -232,16 +240,21 @@ public: qsizetype capturedLength(int nth = 0) const; qsizetype capturedEnd(int nth = 0) const; +#if QT_CORE_REMOVED_SINCE(6, 8) qsizetype capturedStart(const QString &name) const - { return capturedStart(QStringView(name)); } + { return capturedStart(qToAnyStringViewIgnoringNull(name)); } qsizetype capturedLength(const QString &name) const - { return capturedLength(QStringView(name)); } + { return capturedLength(qToAnyStringViewIgnoringNull(name)); } qsizetype capturedEnd(const QString &name) const - { return capturedEnd(QStringView(name)); } + { return capturedEnd(qToAnyStringViewIgnoringNull(name)); } qsizetype capturedStart(QStringView name) const; qsizetype capturedLength(QStringView name) const; qsizetype capturedEnd(QStringView name) const; +#endif + qsizetype capturedStart(QAnyStringView name) const; + qsizetype capturedLength(QAnyStringView name) const; + qsizetype capturedEnd(QAnyStringView name) const; private: friend class QRegularExpression; |