Skip to content

Commit

Permalink
Migrate remaining uses of QRegExp in src/core
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and nyalldawson committed Jul 16, 2021
1 parent eaa3e52 commit 2006c97
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 129 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsrenderchecker.sip.in
Expand Up @@ -10,6 +10,7 @@




class QgsRenderChecker
{
%Docstring(signature="appended")
Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/vector/qgsvectorlayer.sip.in
Expand Up @@ -212,7 +212,7 @@ may span multiple records. The default type is csv.
escape themselves within quotes. Elsewhere they are treated as
quote characters. The defaults for delimiter, quote, and escape
are ',', '"', '"'.
- "regexp" splits each record using a regular expression (see QRegExp
- "regexp" splits each record using a regular expression (see QRegularExpression
documentation for details).
- "whitespace" splits each record based on whitespace (on or more whitespace
characters. Leading whitespace in the record is ignored.
Expand Down
25 changes: 12 additions & 13 deletions src/core/browser/qgsbrowserproxymodel.cpp
Expand Up @@ -18,6 +18,8 @@
#include "qgslayeritem.h"
#include "qgsdatacollectionitem.h"

#include <QRegularExpression>

QgsBrowserProxyModel::QgsBrowserProxyModel( QObject *parent )
: QSortFilterProxyModel( parent )
{
Expand Down Expand Up @@ -86,9 +88,8 @@ void QgsBrowserProxyModel::updateFilter()
const QStringList filterParts = mFilter.split( '|' );
for ( const QString &f : filterParts )
{
QRegExp rx( QStringLiteral( "*%1*" ).arg( f.trimmed() ) );
rx.setPatternSyntax( QRegExp::Wildcard );
rx.setCaseSensitivity( mCaseSensitivity );
QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( QStringLiteral( "*%1*" ).arg( f.trimmed() ) ) );
rx.setPatternOptions( mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
mREList.append( rx );
}
break;
Expand All @@ -98,18 +99,16 @@ void QgsBrowserProxyModel::updateFilter()
const QStringList filterParts = mFilter.split( '|' );
for ( const QString &f : filterParts )
{
QRegExp rx( f.trimmed() );
rx.setPatternSyntax( QRegExp::Wildcard );
rx.setCaseSensitivity( mCaseSensitivity );
QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( f.trimmed() ) );
rx.setPatternOptions( mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
mREList.append( rx );
}
break;
}
case RegularExpression:
{
QRegExp rx( mFilter.trimmed() );
rx.setPatternSyntax( QRegExp::RegExp );
rx.setCaseSensitivity( mCaseSensitivity );
QRegularExpression rx( mFilter.trimmed() );
rx.setPatternOptions( mCaseSensitivity == Qt::CaseInsensitive ? QRegularExpression::CaseInsensitiveOption : QRegularExpression::NoPatternOption );
mREList.append( rx );
break;
}
Expand All @@ -124,19 +123,19 @@ bool QgsBrowserProxyModel::filterAcceptsString( const QString &value ) const
case Normal:
case Wildcards:
{
for ( const QRegExp &rx : mREList )
for ( const QRegularExpression &rx : mREList )
{
if ( rx.exactMatch( value ) )
if ( rx.match( value ).hasMatch() )
return true;
}
break;
}

case RegularExpression:
{
for ( const QRegExp &rx : mREList )
for ( const QRegularExpression &rx : mREList )
{
if ( rx.indexIn( value ) != -1 )
if ( rx.match( value ).hasMatch() )
return true;
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/core/browser/qgsbrowserproxymodel.h
Expand Up @@ -209,7 +209,7 @@ class CORE_EXPORT QgsBrowserProxyModel : public QSortFilterProxyModel
QStringList mHiddenDataItemsKeys;
QStringList mShownDataItemsKeys;
QString mFilter; //filter string provided
QVector<QRegExp> mREList; //list of filters, separated by "|"
QVector<QRegularExpression> mREList; //list of filters, separated by "|"
FilterSyntax mPatternSyntax = Normal;
Qt::CaseSensitivity mCaseSensitivity = Qt::CaseInsensitive;

Expand Down
32 changes: 16 additions & 16 deletions src/core/browser/qgsdataitem.cpp
Expand Up @@ -15,6 +15,21 @@
* *
***************************************************************************/

#include "qgis.h"
#include "qgsdataitem.h"
#include "qgsapplication.h"
#include "qgsdataitemprovider.h"
#include "qgsdataitemproviderregistry.h"
#include "qgsdataprovider.h"
#include "qgslogger.h"
#include "qgsproviderregistry.h"
#include "qgsconfig.h"
#include "qgssettings.h"
#include "qgsanimatedicon.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsprovidermetadata.h"

#include <QApplication>
#include <QtConcurrentMap>
#include <QtConcurrentRun>
Expand All @@ -32,21 +47,6 @@
#include <mutex>
#include <QRegularExpression>

#include "qgis.h"
#include "qgsdataitem.h"
#include "qgsapplication.h"
#include "qgsdataitemprovider.h"
#include "qgsdataitemproviderregistry.h"
#include "qgsdataprovider.h"
#include "qgslogger.h"
#include "qgsproviderregistry.h"
#include "qgsconfig.h"
#include "qgssettings.h"
#include "qgsanimatedicon.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsprovidermetadata.h"

// use GDAL VSI mechanism
#define CPL_SUPRESS_CPLUSPLUS //#spellok
#include "cpl_vsi.h"
Expand Down Expand Up @@ -89,7 +89,7 @@ QgsDataItem::~QgsDataItem()

QString QgsDataItem::pathComponent( const QString &string )
{
return QString( string ).replace( QRegExp( "[\\\\/]" ), QStringLiteral( "|" ) );
return QString( string ).replace( QRegularExpression( "[\\\\/]" ), QStringLiteral( "|" ) );
}

QVariant QgsDataItem::sortKey() const
Expand Down
12 changes: 6 additions & 6 deletions src/core/network/qgsnetworkreplyparser.cpp
Expand Up @@ -21,7 +21,7 @@

#include <QNetworkReply>
#include <QObject>
#include <QRegExp>
#include <QRegularExpression>
#include <QString>
#include <QStringList>

Expand Down Expand Up @@ -51,15 +51,15 @@ QgsNetworkReplyParser::QgsNetworkReplyParser( QNetworkReply *reply )
QString contentType = mReply->header( QNetworkRequest::ContentTypeHeader ).toString();
QgsDebugMsg( "contentType: " + contentType );

QRegExp re( ".*boundary=\"?([^\"]+)\"?\\s?", Qt::CaseInsensitive );

if ( !( re.indexIn( contentType ) == 0 ) )
const QRegularExpression re( ".*boundary=\"?([^\"]+)\"?\\s?", QRegularExpression::CaseInsensitiveOption );
const QRegularExpressionMatch match = re.match( contentType );
if ( !( match.capturedStart( 0 ) == 0 ) )
{
mError = tr( "Cannot find boundary in multipart content type" );
return;
}

QString boundary = re.cap( 1 );
QString boundary = match.captured( 1 );
QgsDebugMsg( QStringLiteral( "boundary = %1 size = %2" ).arg( boundary ).arg( boundary.size() ) );
boundary = "--" + boundary;

Expand Down Expand Up @@ -118,7 +118,7 @@ QgsNetworkReplyParser::QgsNetworkReplyParser( QNetworkReply *reply )
QByteArray headers = part.left( pos );
QgsDebugMsg( "headers:\n" + headers );

QStringList headerRows = QString( headers ).split( QRegExp( "[\n\r]+" ) );
QStringList headerRows = QString( headers ).split( QRegularExpression( "[\n\r]+" ) );
const auto constHeaderRows = headerRows;
for ( const QString &row : constHeaderRows )
{
Expand Down
37 changes: 21 additions & 16 deletions src/core/proj/qgscoordinatereferencesystem.cpp
Expand Up @@ -28,10 +28,9 @@
#include <QDomNode>
#include <QDomElement>
#include <QFileInfo>
#include <QRegExp>
#include <QRegularExpression>
#include <QTextStream>
#include <QFile>
#include <QRegularExpression>

#include "qgsapplication.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -393,25 +392,31 @@ bool QgsCoordinateReferenceSystem::createFromOgcWmsCrs( const QString &crs )

QString wmsCrs = crs;

thread_local const QRegExp re_uri( QStringLiteral( "http://www\\.opengis\\.net/def/crs/([^/]+).+/([^/]+)" ), Qt::CaseInsensitive );
thread_local const QRegExp re_urn( QStringLiteral( "urn:ogc:def:crs:([^:]+).+([^:]+)" ), Qt::CaseInsensitive );
if ( re_uri.exactMatch( wmsCrs ) )
{
wmsCrs = re_uri.cap( 1 ) + ':' + re_uri.cap( 2 );
}
else if ( re_urn.exactMatch( wmsCrs ) )
thread_local const QRegularExpression re_uri( QRegularExpression::anchoredPattern( QStringLiteral( "http://www\\.opengis\\.net/def/crs/([^/]+).+/([^/]+)" ) ), QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = re_uri.match( wmsCrs );
if ( match.hasMatch() )
{
wmsCrs = re_urn.cap( 1 ) + ':' + re_urn.cap( 2 );
wmsCrs = match.captured( 1 ) + ':' + match.captured( 2 );
}
else
{
thread_local const QRegExp re_urn_custom( QStringLiteral( "(user|custom|qgis):(\\d+)" ), Qt::CaseInsensitive );
if ( re_urn_custom.exactMatch( wmsCrs ) && createFromSrsId( re_urn_custom.cap( 2 ).toInt() ) )
thread_local const QRegularExpression re_urn( QRegularExpression::anchoredPattern( QStringLiteral( "urn:ogc:def:crs:([^:]+).+([^:]+)" ) ), QRegularExpression::CaseInsensitiveOption );
match = re_urn.match( wmsCrs );
if ( match.hasMatch() )
{
locker.changeMode( QgsReadWriteLocker::Write );
if ( !sDisableOgcCache )
sOgcCache()->insert( crs, *this );
return d->mIsValid;
wmsCrs = match.captured( 1 ) + ':' + match.captured( 2 );
}
else
{
thread_local const QRegularExpression re_urn_custom( QRegularExpression::anchoredPattern( QStringLiteral( "(user|custom|qgis):(\\d+)" ) ), QRegularExpression::CaseInsensitiveOption );
match = re_urn_custom.match( wmsCrs );
if ( match.hasMatch() && createFromSrsId( match.captured( 2 ).toInt() ) )
{
locker.changeMode( QgsReadWriteLocker::Write );
if ( !sDisableOgcCache )
sOgcCache()->insert( crs, *this );
return d->mIsValid;
}
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/core/project/qgsprojectfiletransform.cpp
Expand Up @@ -23,19 +23,21 @@
#include "qgsreadwritecontext.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include <QTextStream>
#include <QDomDocument>
#ifndef QT_NO_PRINTER
#include <QPrinter> //to find out screen resolution
#endif
#include <cstdlib>
#include "qgspathresolver.h"
#include "qgsproject.h"
#include "qgsprojectproperty.h"
#include "qgsrasterbandstats.h"
#include "qgsrasterdataprovider.h"
#include "qgsxmlutils.h"

#include <QTextStream>
#include <QDomDocument>
#include <QRegularExpression>
#ifndef QT_NO_PRINTER
#include <QPrinter> //to find out screen resolution
#endif
#include <cstdlib>

typedef QgsProjectVersion PFV;

// Transformer functions below. Declare functions here,
Expand Down Expand Up @@ -1120,11 +1122,11 @@ int rasterBandNumber( const QDomElement &rasterPropertiesElem, const QString &ba
QDomElement rasterBandElem = rasterPropertiesElem.firstChildElement( bandName );
if ( !rasterBandElem.isNull() )
{
QRegExp re( "(\\d+)" );

if ( re.indexIn( rasterBandElem.text() ) >= 0 )
const thread_local QRegularExpression re( "(\\d+)" );
const QRegularExpressionMatch match = re.match( rasterBandElem.text() );
if ( match.hasMatch() )
{
return re.cap( 1 ).toInt();
return match.captured( 1 ).toInt();
}
}
return band;
Expand Down
1 change: 0 additions & 1 deletion src/core/providers/gdal/qgsgdalprovider.cpp
Expand Up @@ -1252,7 +1252,6 @@ QString QgsGdalProvider::generateBandName( int bandNumber ) const
if ( values.at( 0 ) == QLatin1String( "NETCDF_DIM_EXTRA" ) || values.at( 0 ) == QLatin1String( "GTIFF_DIM_EXTRA" ) )
{
dimExtraValues = val.replace( '{', QString() ).replace( '}', QString() ).split( ',' );
//http://qt-project.org/doc/qt-4.8/qregexp.html#capturedTexts
}
else
{
Expand Down
39 changes: 19 additions & 20 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -16,23 +16,6 @@
***************************************************************************/


#include <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <QDomImplementation>
#include <QDomNode>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QUrl>
#include <QTimer>
#include <QStandardPaths>
#include <QUuid>

#include <sqlite3.h>

#include "qgssqliteutils.h"

#include "qgssqliteutils.h"
#include "qgs3drendererregistry.h"
#include "qgsabstract3drenderer.h"
Expand Down Expand Up @@ -64,6 +47,22 @@
#include "qgsdatums.h"
#include "qgsprojoperation.h"

#include <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <QDomImplementation>
#include <QDomNode>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QUrl>
#include <QTimer>
#include <QStandardPaths>
#include <QUuid>
#include <QRegularExpression>

#include <sqlite3.h>

QString QgsMapLayer::extensionPropertyType( QgsMapLayer::PropertyType type )
{
switch ( type )
Expand Down Expand Up @@ -259,8 +258,8 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCon
mDataSource = context.pathResolver().readPath( mne.text() );

// if the layer needs authentication, ensure the master password is set
QRegExp rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
if ( ( rx.indexIn( mDataSource ) != -1 )
const thread_local QRegularExpression rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
if ( rx.match( mDataSource ).hasMatch()
&& !QgsApplication::authManager()->setMasterPassword( true ) )
{
return false;
Expand Down Expand Up @@ -2067,7 +2066,7 @@ QString QgsMapLayer::generateId( const QString &layerName )
// underscore) with an underscore.
// Note that the first backslash in the regular expression is
// there for the compiler, so the pattern is actually \W
id.replace( QRegExp( "[\\W]" ), QStringLiteral( "_" ) );
id.replace( QRegularExpression( "[\\W]" ), QStringLiteral( "_" ) );
return id;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsofflineediting.cpp
Expand Up @@ -365,7 +365,7 @@ void QgsOfflineEditing::synchronize()

// disable offline project
QString projectTitle = QgsProject::instance()->title();
projectTitle.remove( QRegExp( " \\(offline\\)$" ) );
projectTitle.remove( QRegularExpression( " \\(offline\\)$" ) );
QgsProject::instance()->setTitle( projectTitle );
QgsProject::instance()->removeEntry( PROJECT_ENTRY_SCOPE_OFFLINE, PROJECT_ENTRY_KEY_OFFLINE_DB_PATH );
remoteLayer->reload(); //update with other changes
Expand Down

0 comments on commit 2006c97

Please sign in to comment.