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>
bb10
Matthias Rauter 2024-02-09 13:29:57 +01:00 committed by Marc Mutz
parent ab0b2a490e
commit 737c6680b4
4 changed files with 81 additions and 57 deletions

View File

@ -1030,6 +1030,40 @@ bool QProcessEnvironment::operator==(const QProcessEnvironment &other) const
}
#endif // QT_CONFIG(processenvironment)
#if QT_CONFIG(regularexpression)
#include "qregularexpression.h"
bool QRegularExpressionMatch::hasCaptured(QStringView name) const
{
return hasCaptured(QAnyStringView(name));
}
QString QRegularExpressionMatch::captured(QStringView name) const
{
return captured(QAnyStringView(name));
}
QStringView QRegularExpressionMatch::capturedView(QStringView name) const
{
return capturedView(QAnyStringView(name));
}
qsizetype QRegularExpressionMatch::capturedStart(QStringView name) const
{
return capturedStart(QAnyStringView(name));
}
qsizetype QRegularExpressionMatch::capturedLength(QStringView name) const
{
return capturedLength(QAnyStringView(name));
}
qsizetype QRegularExpressionMatch::capturedEnd(QStringView name) const
{
return capturedEnd(QAnyStringView(name));
}
#endif // QT_CONFIG(regularexpression)
#include "qurl.h"
bool QUrl::operator<(const QUrl &url) const

View File

@ -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");

View File

@ -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;

View File

@ -1874,12 +1874,14 @@ void tst_QRegularExpression::captureNamesNul()
QString captureName("name");
QCOMPARE(m.captured(captureName), "456");
QCOMPARE(m.captured(QStringView(captureName)), "456");
QCOMPARE(m.captured(QAnyStringView(captureName)), "456");
QCOMPARE(m.captured(qToStringViewIgnoringNull(captureName)), "456");
QCOMPARE(m.captured(u"name"), "456");
captureName = "anotherName";
QCOMPARE(m.captured(captureName), "789");
QCOMPARE(m.captured(QStringView(captureName)), "789");
QCOMPARE(m.captured(QAnyStringView(captureName)), "789");
QCOMPARE(m.captured(qToStringViewIgnoringNull(captureName)), "789");
QCOMPARE(m.captured(u"anotherName"), "789");
}