Skip to content

Commit

Permalink
Faster WKT conversion using thread_local regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Nov 3, 2021
1 parent 4dbff9f commit d662641
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
14 changes: 6 additions & 8 deletions src/core/geometry/qgsgeometryutils.cpp
Expand Up @@ -1127,8 +1127,8 @@ QgsPointSequence QgsGeometryUtils::pointsFromWKT( const QString &wktCoordinateLi
//first scan through for extra unexpected dimensions
bool foundZ = false;
bool foundM = false;
const QRegularExpression rx( QStringLiteral( "\\s" ) );
const QRegularExpression rxIsNumber( QStringLiteral( "^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$" ) );
const thread_local QRegularExpression rx( QStringLiteral( "\\s" ) );
const thread_local QRegularExpression rxIsNumber( QStringLiteral( "^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$" ) );
for ( const QString &pointCoordinates : coordList )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
Expand Down Expand Up @@ -1336,11 +1336,10 @@ QPair<QgsWkbTypes::Type, QString> QgsGeometryUtils::wktReadBlock( const QString
{
QString wktParsed = wkt;
QString contents;
if ( wkt.contains( QString( "EMPTY" ), Qt::CaseInsensitive ) )
if ( wkt.contains( QLatin1String( "EMPTY" ), Qt::CaseInsensitive ) )
{
QRegularExpression wktRegEx( QStringLiteral( "^\\s*(\\w+)\\s+(\\w+)\\s*$" ) );
wktRegEx.setPatternOptions( QRegularExpression::DotMatchesEverythingOption );
const QRegularExpressionMatch match = wktRegEx.match( wkt );
const thread_local QRegularExpression sWktRegEx( QStringLiteral( "^\\s*(\\w+)\\s+(\\w+)\\s*$" ), QRegularExpression::DotMatchesEverythingOption );
const QRegularExpressionMatch match = sWktRegEx.match( wkt );
if ( match.hasMatch() )
{
wktParsed = match.captured( 1 );
Expand All @@ -1357,8 +1356,7 @@ QPair<QgsWkbTypes::Type, QString> QgsGeometryUtils::wktReadBlock( const QString
// removes extra parentheses
wktParsed.truncate( wktParsed.size() - ( closedParenthesisCount - openedParenthesisCount ) );

QRegularExpression cooRegEx( QStringLiteral( "^[^\\(]*\\((.*)\\)[^\\)]*$" ) );
cooRegEx.setPatternOptions( QRegularExpression::DotMatchesEverythingOption );
const thread_local QRegularExpression cooRegEx( QStringLiteral( "^[^\\(]*\\((.*)\\)[^\\)]*$" ), QRegularExpression::DotMatchesEverythingOption );
const QRegularExpressionMatch match = cooRegEx.match( wktParsed );
contents = match.hasMatch() ? match.captured( 1 ) : QString();
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgspoint.cpp
Expand Up @@ -177,7 +177,7 @@ bool QgsPoint::fromWkt( const QString &wkt )
secondWithoutParentheses.isEmpty() )
return true;

const QRegularExpression rx( QStringLiteral( "\\s" ) );
const thread_local QRegularExpression rx( QStringLiteral( "\\s" ) );
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
QStringList coordinates = parts.second.split( rx, QString::SkipEmptyParts );
#else
Expand All @@ -195,7 +195,7 @@ bool QgsPoint::fromWkt( const QString &wkt )
// True
// p.asWkt()
// 'Point (0 -1.43209999999999993)'
const QRegularExpression rxIsNumber( QStringLiteral( "^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$" ) );
const thread_local QRegularExpression rxIsNumber( QStringLiteral( "^[+-]?(\\d\\.?\\d*[Ee][+\\-]?\\d+|(\\d+\\.\\d*|\\d*\\.\\d+)|\\d+)$" ) );
if ( coordinates.filter( rxIsNumber ).size() != coordinates.size() )
return false;

Expand Down

0 comments on commit d662641

Please sign in to comment.