Skip to content

Commit

Permalink
Initialize regular expressions once
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn authored and github-actions[bot] committed May 11, 2021
1 parent 0bcac9e commit db0e987
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 45 deletions.
25 changes: 14 additions & 11 deletions src/core/expression/qgsexpressionnodeimpl.cpp
Expand Up @@ -532,39 +532,40 @@ QVariant QgsExpressionNodeBinaryOperator::evalNode( QgsExpression *parent, const
bool matches;
if ( mOp == boLike || mOp == boILike || mOp == boNotLike || mOp == boNotILike ) // change from LIKE syntax to regexp
{
QString esc_regexp = QRegExp::escape( regexp );
QString esc_regexp = QRegularExpression::escape( regexp );
// manage escape % and _
if ( esc_regexp.startsWith( '%' ) )
{
esc_regexp.replace( 0, 1, QStringLiteral( ".*" ) );
}
QRegExp rx( "[^\\\\](%)" );
QRegExp rx1( QStringLiteral( "[^\\\\](%)" ) );
int pos = 0;
while ( ( pos = rx.indexIn( esc_regexp, pos ) ) != -1 )
while ( ( pos = rx1.indexIn( esc_regexp, pos ) ) != -1 )
{
esc_regexp.replace( pos + 1, 1, QStringLiteral( ".*" ) );
pos += 1;
}
rx.setPattern( QStringLiteral( "\\\\%" ) );
esc_regexp.replace( rx, QStringLiteral( "%" ) );
QRegExp rx2( QStringLiteral( "\\\\%" ) );
esc_regexp.replace( rx2, QStringLiteral( "%" ) );
if ( esc_regexp.startsWith( '_' ) )
{
esc_regexp.replace( 0, 1, QStringLiteral( "." ) );
}
rx.setPattern( QStringLiteral( "[^\\\\](_)" ) );
QRegExp rx3( QStringLiteral( "[^\\\\](_)" ) );
pos = 0;
while ( ( pos = rx.indexIn( esc_regexp, pos ) ) != -1 )
while ( ( pos = rx3.indexIn( esc_regexp, pos ) ) != -1 )
{
esc_regexp.replace( pos + 1, 1, '.' );
pos += 1;
}
rx.setPattern( QStringLiteral( "\\\\_" ) );
esc_regexp.replace( rx, QStringLiteral( "_" ) );
const static QRegularExpression sRx4( QStringLiteral( "\\\\_" ) );
QRegularExpression rx4( sRx4 );
esc_regexp.replace( rx4, QStringLiteral( "_" ) );
matches = QRegExp( esc_regexp, mOp == boLike || mOp == boNotLike ? Qt::CaseSensitive : Qt::CaseInsensitive ).exactMatch( str );
}
else
{
matches = QRegExp( regexp ).indexIn( str ) != -1;
matches = QRegularExpression( regexp ).match( str ).hasMatch();
}

if ( mOp == boNotLike || mOp == boNotILike )
Expand Down Expand Up @@ -1363,7 +1364,9 @@ bool QgsExpressionNodeColumnRef::prepareNode( QgsExpression *parent, const QgsEx

QString QgsExpressionNodeColumnRef::dump() const
{
return QRegExp( "^[A-Za-z_\x80-\xff][A-Za-z0-9_\x80-\xff]*$" ).exactMatch( mName ) ? mName : QgsExpression::quotedColumnRef( mName );
const static QRegularExpression sRe( QStringLiteral( "^[A-Za-z_\x80-\xff][A-Za-z0-9_\x80-\xff]*$" ) );
QRegularExpression re( sRe );
return re.match( mName ).hasMatch() ? mName : QgsExpression::quotedColumnRef( mName );
}

QSet<QString> QgsExpressionNodeColumnRef::referencedColumns() const
Expand Down
8 changes: 5 additions & 3 deletions src/core/geometry/qgsmultipoint.cpp
Expand Up @@ -21,6 +21,7 @@ email : marco.hugentobler at sourcepole dot com

#include <QJsonArray>
#include <QJsonObject>
#include <QRegularExpression>
#include <nlohmann/json.hpp>

QgsMultiPoint::QgsMultiPoint()
Expand Down Expand Up @@ -64,9 +65,10 @@ bool QgsMultiPoint::fromWkt( const QString &wkt )
{
QString collectionWkt( wkt );
//test for non-standard MultiPoint(x1 y1, x2 y2) format
QRegExp regex( "^\\s*MultiPoint\\s*[ZM]*\\s*\\(\\s*[-\\d]" );
regex.setCaseSensitivity( Qt::CaseInsensitive );
if ( regex.indexIn( collectionWkt ) >= 0 )
static const QRegularExpression sRegex( QStringLiteral( "^\\s*MultiPoint\\s*[ZM]*\\s*\\(\\s*[-\\d]" ), QRegularExpression::CaseInsensitiveOption );
QRegularExpression regex( sRegex );
const QRegularExpressionMatch match = regex.match( collectionWkt );
if ( match.hasMatch() )
{
//alternate style without extra brackets, upgrade to standard
collectionWkt.replace( '(', QLatin1String( "((" ) ).replace( ')', QLatin1String( "))" ) ).replace( ',', QLatin1String( "),(" ) );
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsapplication.cpp
Expand Up @@ -1091,7 +1091,8 @@ QString QgsApplication::userStylePath()

QRegExp QgsApplication::shortNameRegExp()
{
return QRegExp( "^[A-Za-z][A-Za-z0-9\\._-]*" );
static const QRegExp regexp( QStringLiteral( "^[A-Za-z][A-Za-z0-9\\._-]*" ) );
return regexp;
}

QString QgsApplication::userLoginName()
Expand Down
79 changes: 49 additions & 30 deletions src/core/qgscoordinatereferencesystem.cpp
Expand Up @@ -290,7 +290,8 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
locker.unlock();

bool result = false;
QRegularExpression reCrsId( QStringLiteral( "^(epsg|esri|osgeo|ignf|zangi|iau2000|postgis|internal|user)\\:(\\w+)$" ), QRegularExpression::CaseInsensitiveOption );
const static QRegularExpression sReCrsId( QStringLiteral( "^(epsg|esri|osgeo|ignf|zangi|iau2000|postgis|internal|user)\\:(\\w+)$" ), QRegularExpression::CaseInsensitiveOption );
QRegularExpression reCrsId( sReCrsId );
QRegularExpressionMatch match = reCrsId.match( definition );
if ( match.capturedStart() == 0 )
{
Expand Down Expand Up @@ -318,7 +319,8 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
}
else
{
QRegularExpression reCrsStr( "^(?:(wkt|proj4|proj)\\:)?(.+)$", QRegularExpression::CaseInsensitiveOption );
const static QRegularExpression sReCrsStr( "^(?:(wkt|proj4|proj)\\:)?(.+)$", QRegularExpression::CaseInsensitiveOption );
QRegularExpression reCrsStr( sReCrsStr );
match = reCrsStr.match( definition );
if ( match.capturedStart() == 0 )
{
Expand Down Expand Up @@ -407,25 +409,32 @@ bool QgsCoordinateReferenceSystem::createFromOgcWmsCrs( const QString &crs )

QString wmsCrs = crs;

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

Expand Down Expand Up @@ -1030,7 +1039,8 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co
#else
Q_UNUSED( identify )

QRegExp myProjRegExp( "\\+proj=(\\S+)" );
static const QRegularExpression sProjRegExp( QStringLiteral( "\\+proj=(\\S+)" ) );
QRegularExpression myProjRegExp( sProjRegExp );
int myStart = myProjRegExp.indexIn( myProj4String );
if ( myStart == -1 )
{
Expand All @@ -1043,7 +1053,8 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co

d->mProjectionAcronym = myProjRegExp.cap( 1 );

QRegExp myEllipseRegExp( "\\+ellps=(\\S+)" );
static const QRegularExpression sEllipseRegExp( QStringLiteral( "\\+ellps=(\\S+)" ) );
QRegularExpression myEllipseRegExp( sEllipseRegExp );
myStart = myEllipseRegExp.indexIn( myProj4String );
if ( myStart == -1 )
{
Expand All @@ -1054,7 +1065,8 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co
d->mEllipsoidAcronym = myEllipseRegExp.cap( 1 );
}

QRegExp myAxisRegExp( "\\+a=(\\S+)" );
static const QRegularExpression sAxisRegExp( QStringLiteral( "\\+a=(\\S+)" ) );
QRegularExpression myAxisRegExp( sAxisRegExp );
myStart = myAxisRegExp.indexIn( myProj4String );

long mySrsId = 0;
Expand All @@ -1070,8 +1082,10 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co
// Ticket #722 - aaronr
// Check if we can swap the lat_1 and lat_2 params (if they exist) to see if we match...
// First we check for lat_1 and lat_2
QRegExp myLat1RegExp( "\\+lat_1=\\S+" );
QRegExp myLat2RegExp( "\\+lat_2=\\S+" );
static const QRegularExpression sLat1RegExp( QStringLiteral( "\\+lat_1=\\S+" ) );
static const QRegularExpression sLat2RegExp( QStringLiteral( "\\+lat_2=\\S+" ) );
QRegularExpression myLat1RegExp( sLat1RegExp );
QRegularExpression myLat2RegExp( sLat2RegExp );
int myStart1 = 0;
int myLength1 = 0;
int myStart2 = 0;
Expand Down Expand Up @@ -1118,7 +1132,8 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co
// also with parameters containing spaces (e.g. +nadgrids)
// make sure result is trimmed (#5598)
QStringList myParams;
const QRegExp regExp( "\\s+(?=\\+)" );
static const QRegularExpression sRegExp( "\\s+(?=\\+)" );
QRegularExpression regExp( sRegExp );
{
const auto constSplit = myProj4String.split( regExp, QString::SkipEmptyParts );
for ( const QString &param : constSplit )
Expand Down Expand Up @@ -2439,19 +2454,23 @@ bool testIsGeographic( PJ *crs )

void getOperationAndEllipsoidFromProjString( const QString &proj, QString &operation, QString &ellipsoid )
{
QRegExp projRegExp( "\\+proj=(\\S+)" );
if ( projRegExp.indexIn( proj ) < 0 )
static const QRegularExpression sProjRegExp( QStringLiteral( "\\+proj=(\\S+)" ) );
QRegularExpression projRegExp( sProjRegExp );
const QRegularExpressionMatch projMatch = projRegExp.match( proj );
if ( !projMatch.hasMatch() )
{
QgsDebugMsgLevel( QStringLiteral( "no +proj argument found [%2]" ).arg( proj ), 2 );
return;
}
operation = projRegExp.cap( 1 );
operation = projMatch.captured( 1 );

QRegExp ellipseRegExp( "\\+(?:ellps|datum)=(\\S+)" );
static const QRegularExpression sEllipseRegExp( QStringLiteral( "\\+(?:ellps|datum)=(\\S+)" ) );
QRegularExpression ellipseRegExp( sEllipseRegExp );
const QRegularExpressionMatch ellipseMatch = projRegExp.match( proj );
QString ellps;
if ( ellipseRegExp.indexIn( proj ) >= 0 )
if ( !ellipseMatch.hasMatch() )
{
ellipsoid = ellipseRegExp.cap( 1 );
ellipsoid = ellipseMatch.captured( 1 );
}
else
{
Expand All @@ -2460,7 +2479,7 @@ void getOperationAndEllipsoidFromProjString( const QString &proj, QString &opera
// and will result in oddities within other areas of QGIS (e.g. project ellipsoid won't be correctly
// set for these CRSes). Better just hack around and make the constraint happy for now,
// and hope that the definitions get corrected in future.
ellipsoid = "";
ellipsoid.clear();
}
}

Expand Down Expand Up @@ -3125,7 +3144,7 @@ int QgsCoordinateReferenceSystem::syncDatabase()
continue;
}

QRegExp ellipseRegExp( "\\+ellps=(\\S+)" );
QReExp ellipseRegExp( "\\+ellps=(\\S+)" );
QString ellps;
if ( ellipseRegExp.indexIn( proj4 ) >= 0 )
{
Expand Down

0 comments on commit db0e987

Please sign in to comment.