Skip to content
Permalink
Browse files

Merge pull request #40737 from m-kuhn/cache_regexps

Initialize regular expressions once
  • Loading branch information
m-kuhn committed Dec 29, 2020
2 parents 97b7ab9 + 3c0b971 commit a898e64a90ec5845d1a9b0b4f801e44d74bac160
@@ -538,33 +538,33 @@ QVariant QgsExpressionNodeBinaryOperator::evalNode( QgsExpression *parent, const
{
esc_regexp.replace( 0, 1, QStringLiteral( ".*" ) );
}
QRegExp rx( "[^\\\\](%)" );
thread_local 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( "%" ) );
thread_local QRegExp rx2( QStringLiteral( "\\\\%" ) );
esc_regexp.replace( rx2, QStringLiteral( "%" ) );
if ( esc_regexp.startsWith( '_' ) )
{
esc_regexp.replace( 0, 1, QStringLiteral( "." ) );
}
rx.setPattern( QStringLiteral( "[^\\\\](_)" ) );
thread_local 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( "_" ) );
esc_regexp.replace( QStringLiteral( "\\\\_" ), 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 +1363,8 @@ 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 thread_local QRegExp re( QStringLiteral( "^[A-Za-z_\x80-\xff][A-Za-z0-9_\x80-\xff]*$" ) );
return re.exactMatch( mName ) ? 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,9 @@ 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 )
const thread_local QRegularExpression regex( QStringLiteral( "^\\s*MultiPoint\\s*[ZM]*\\s*\\(\\s*[-\\d]" ), QRegularExpression::CaseInsensitiveOption );
const QRegularExpressionMatch match = regex.match( collectionWkt );
if ( match.hasMatch() )
{
//alternate style without extra brackets, upgrade to standard
collectionWkt.replace( '(', QLatin1String( "((" ) ).replace( ')', QLatin1String( "))" ) ).replace( ',', QLatin1String( "),(" ) );
@@ -879,7 +879,7 @@ void QgsApplication::setUITheme( const QString &themeName )
{
// apply OS-specific UI scale factor to stylesheet's em values
int index = 0;
QRegularExpression regex( QStringLiteral( "(?<=[\\s:])([0-9\\.]+)(?=em)" ) );
const static QRegularExpression regex( QStringLiteral( "(?<=[\\s:])([0-9\\.]+)(?=em)" ) );
QRegularExpressionMatch match = regex.match( styledata, index );
while ( match.hasMatch() )
{
@@ -1115,7 +1115,8 @@ QString QgsApplication::userStylePath()

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

QString QgsApplication::userLoginName()
@@ -290,7 +290,7 @@ 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 thread_local QRegularExpression reCrsId( QStringLiteral( "^(epsg|esri|osgeo|ignf|zangi|iau2000|postgis|internal|user)\\:(\\w+)$" ), QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = reCrsId.match( definition );
if ( match.capturedStart() == 0 )
{
@@ -318,7 +318,7 @@ bool QgsCoordinateReferenceSystem::createFromString( const QString &definition )
}
else
{
QRegularExpression reCrsStr( "^(?:(wkt|proj4|proj)\\:)?(.+)$", QRegularExpression::CaseInsensitiveOption );
const thread_local QRegularExpression reCrsStr( QStringLiteral( "^(?:(wkt|proj4|proj)\\:)?(.+)$" ), QRegularExpression::CaseInsensitiveOption );
match = reCrsStr.match( definition );
if ( match.capturedStart() == 0 )
{
@@ -407,8 +407,8 @@ 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 );
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 );
@@ -419,8 +419,8 @@ bool QgsCoordinateReferenceSystem::createFromOgcWmsCrs( const QString &crs )
}
else
{
re_urn.setPattern( QStringLiteral( "(user|custom|qgis):(\\d+)" ) );
if ( re_urn.exactMatch( wmsCrs ) && createFromSrsId( re_urn.cap( 2 ).toInt() ) )
thread_local const QRegExp re_urn_custom( QStringLiteral( "(user|custom|qgis):(\\d+)" ), Qt::CaseInsensitive );
if ( re_urn_custom.exactMatch( wmsCrs ) && createFromSrsId( re_urn.cap( 2 ).toInt() ) )
{
locker.changeMode( QgsReadWriteLocker::Write );
if ( !sDisableOgcCache )
@@ -1030,8 +1030,8 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co
#else
Q_UNUSED( identify )

QRegExp myProjRegExp( "\\+proj=(\\S+)" );
int myStart = myProjRegExp.indexIn( myProj4String );
QRegExp projRegExp( QStringLiteral( "\\+proj=(\\S+)" ) );
int myStart = projRegExp.indexIn( myProj4String );
if ( myStart == -1 )
{
locker.changeMode( QgsReadWriteLocker::Write );
@@ -1041,9 +1041,9 @@ bool QgsCoordinateReferenceSystem::createFromProj( const QString &projString, co
return d->mIsValid;
}

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

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

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

long mySrsId = 0;
@@ -1070,8 +1070,8 @@ 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+" );
QRegExp myLat1RegExp( QStringLiteral( "\\+lat_1=\\S+" ) );
QRegExp myLat2RegExp( QStringLiteral( "\\+lat_2=\\S+" ) );
int myStart1 = 0;
int myLength1 = 0;
int myStart2 = 0;
@@ -1118,7 +1118,7 @@ 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+(?=\\+)" );
thread_local const QRegularExpression regExp( "\\s+(?=\\+)" );
{
const auto constSplit = myProj4String.split( regExp, QString::SkipEmptyParts );
for ( const QString &param : constSplit )
@@ -2439,19 +2439,21 @@ bool testIsGeographic( PJ *crs )

void getOperationAndEllipsoidFromProjString( const QString &proj, QString &operation, QString &ellipsoid )
{
QRegExp projRegExp( "\\+proj=(\\S+)" );
if ( projRegExp.indexIn( proj ) < 0 )
thread_local const QRegularExpression projRegExp( QStringLiteral( "\\+proj=(\\S+)" ) );
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+)" );
thread_local const QRegularExpression ellipseRegExp( QStringLiteral( "\\+(?:ellps|datum)=(\\S+)" ) );
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
{
@@ -2460,7 +2462,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();
}
}

@@ -256,10 +256,10 @@ bool QgsProjUtils::coordinateOperationIsAvailable( const QString &projDef )

QList<QgsDatumTransform::GridDetails> QgsProjUtils::gridsUsed( const QString &proj )
{
static QRegularExpression sRegex( QStringLiteral( "\\+(?:nad)?grids=(.*?)\\s" ) );
const thread_local QRegularExpression regex( QStringLiteral( "\\+(?:nad)?grids=(.*?)\\s" ) );

QList< QgsDatumTransform::GridDetails > grids;
QRegularExpressionMatchIterator matches = sRegex.globalMatch( proj );
QRegularExpressionMatchIterator matches = regex.globalMatch( proj );
while ( matches.hasNext() )
{
const QRegularExpressionMatch match = matches.next();

0 comments on commit a898e64

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