Skip to content
Permalink
Browse files

Initialize regular expressions once

  • Loading branch information
m-kuhn committed Dec 23, 2020
1 parent 14a60c1 commit 9e0bf6df993a8548be3e21e274ee2857728e658d
@@ -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 )
@@ -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
@@ -21,6 +21,7 @@ email : marco.hugentobler at sourcepole dot com

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

QgsMultiPoint::QgsMultiPoint()
@@ -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( "),(" ) );
@@ -1095,7 +1095,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()
@@ -288,7 +288,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 )
{
@@ -316,7 +317,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 )
{
@@ -405,25 +407,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;
}
}
}

@@ -1028,7 +1037,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 )
{
@@ -1041,7 +1051,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 )
{
@@ -1052,7 +1063,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;
@@ -1068,8 +1080,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;
@@ -1116,7 +1130,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 )
@@ -2437,19 +2452,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
{
@@ -2458,7 +2477,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();
}
}

@@ -3123,7 +3142,7 @@ int QgsCoordinateReferenceSystem::syncDatabase()
continue;
}

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

0 comments on commit 9e0bf6d

Please sign in to comment.
You can’t perform that action at this time.