QRegularExpression: make escape-like functions work on QStringView

They don't store the strings.

[ChangeLog][QtCore][QRegularExpression] The escape(),
wildcardToRegularExpression() and anchoredPattern() functions
now have overloads taking a QStringView parameter.

Change-Id: Icc66ba1201ef1f1064d9565900439e78912b675c
Reviewed-by: Samuel Gaist <samuel.gaist@idiap.ch>
This commit is contained in:
Giuseppe D'Angelo 2020-01-03 12:00:10 +01:00
parent 24b8b2cb6c
commit 6d201aa1bd
2 changed files with 55 additions and 13 deletions

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 Giuseppe D'Angelo <dangelog@gmail.com>. ** Copyright (C) 2020 Giuseppe D'Angelo <dangelog@gmail.com>.
** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
@ -1829,7 +1829,19 @@ uint qHash(const QRegularExpression &key, uint seed) noexcept
return seed; return seed;
} }
#if QT_STRINGVIEW_LEVEL < 2
/*! /*!
\overload
*/
QString QRegularExpression::escape(const QString &str)
{
return escape(QStringView(str));
}
#endif // QT_STRINGVIEW_LEVEL < 2
/*!
\since 5.15
Escapes all characters of \a str so that they no longer have any special Escapes all characters of \a str so that they no longer have any special
meaning when used as a regular expression pattern string, and returns meaning when used as a regular expression pattern string, and returns
the escaped string. For instance: the escaped string. For instance:
@ -1847,7 +1859,7 @@ uint qHash(const QRegularExpression &key, uint seed) noexcept
inside \a str is escaped with the sequence \c{"\\0"} (backslash + inside \a str is escaped with the sequence \c{"\\0"} (backslash +
\c{'0'}), instead of \c{"\\\0"} (backslash + \c{NUL}). \c{'0'}), instead of \c{"\\\0"} (backslash + \c{NUL}).
*/ */
QString QRegularExpression::escape(const QString &str) QString QRegularExpression::escape(QStringView str)
{ {
QString result; QString result;
const int count = str.size(); const int count = str.size();
@ -1882,8 +1894,19 @@ QString QRegularExpression::escape(const QString &str)
return result; return result;
} }
#if QT_STRINGVIEW_LEVEL < 2
/*! /*!
\since 5.12 \since 5.12
\overload
*/
QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
{
return wildcardToRegularExpression(QStringView(pattern));
}
#endif // QT_STRINGVIEW_LEVEL < 2
/*!
\since 5.15
Returns a regular expression representation of the given glob \a pattern. Returns a regular expression representation of the given glob \a pattern.
The transformation is targeting file path globbing, which means in particular The transformation is targeting file path globbing, which means in particular
@ -1928,13 +1951,13 @@ QString QRegularExpression::escape(const QString &str)
\sa escape() \sa escape()
*/ */
QString QRegularExpression::wildcardToRegularExpression(const QString &pattern) QString QRegularExpression::wildcardToRegularExpression(QStringView pattern)
{ {
const int wclen = pattern.length(); const int wclen = pattern.length();
QString rx; QString rx;
rx.reserve(wclen + wclen / 16); rx.reserve(wclen + wclen / 16);
int i = 0; int i = 0;
const QChar *wc = pattern.unicode(); const QChar *wc = pattern.data();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
const QLatin1Char nativePathSeparator('\\'); const QLatin1Char nativePathSeparator('\\');
@ -2006,16 +2029,31 @@ QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
return anchoredPattern(rx); return anchoredPattern(rx);
} }
#if QT_STRINGVIEW_LEVEL < 2
/*! /*!
\fn QRegularExpression::anchoredPattern(const QString &expression) \fn QRegularExpression::anchoredPattern(const QString &expression)
\since 5.12 \since 5.12
\overload
*/
#endif // QT_STRINGVIEW_LEVEL < 2
/*!
\since 5.15
Returns the \a expression wrapped between the \c{\A} and \c{\z} anchors to Returns the \a expression wrapped between the \c{\A} and \c{\z} anchors to
be used for exact matching. be used for exact matching.
\sa {Porting from QRegExp's Exact Matching} \sa {Porting from QRegExp's Exact Matching}
*/ */
QString QRegularExpression::anchoredPattern(QStringView expression)
{
return QString()
+ QLatin1String("\\A(?:")
+ expression
+ QLatin1String(")\\z");
}
/*! /*!
\since 5.1 \since 5.1

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>. ** Copyright (C) 2020 Giuseppe D'Angelo <dangelog@gmail.com>.
** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the QtCore module of the Qt Toolkit. ** This file is part of the QtCore module of the Qt Toolkit.
@ -42,9 +42,8 @@
#define QREGULAREXPRESSION_H #define QREGULAREXPRESSION_H
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#include <QtCore/qstring.h> #include <QtCore/qstring.h>
#include <QtCore/qstringlist.h> #include <QtCore/qstringview.h>
#include <QtCore/qshareddata.h> #include <QtCore/qshareddata.h>
#include <QtCore/qvariant.h> #include <QtCore/qvariant.h>
@ -52,7 +51,8 @@ QT_REQUIRE_CONFIG(regularexpression);
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QStringView; class QStringList;
class QLatin1String;
class QRegularExpressionMatch; class QRegularExpressionMatch;
class QRegularExpressionMatchIterator; class QRegularExpressionMatchIterator;
@ -137,14 +137,18 @@ public:
void optimize() const; void optimize() const;
#if QT_STRINGVIEW_LEVEL < 2
static QString escape(const QString &str); static QString escape(const QString &str);
static QString wildcardToRegularExpression(const QString &str); static QString wildcardToRegularExpression(const QString &str);
static inline QString anchoredPattern(const QString &expression) static inline QString anchoredPattern(const QString &expression)
{ {
return QLatin1String("\\A(?:") return anchoredPattern(QStringView(expression));
+ expression
+ QLatin1String(")\\z");
} }
#endif
static QString escape(QStringView str);
static QString wildcardToRegularExpression(QStringView str);
static QString anchoredPattern(QStringView expression);
bool operator==(const QRegularExpression &re) const; bool operator==(const QRegularExpression &re) const;
inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); } inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }