Skip to content

Commit

Permalink
More QRegularExpression in providers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 1, 2021
1 parent 731fd33 commit b53f97b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 52 deletions.
5 changes: 3 additions & 2 deletions src/providers/mssql/qgsmssqlnewconnection.cpp
Expand Up @@ -19,7 +19,8 @@
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QRegExpValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>

#include "qgsmssqlnewconnection.h"
#include "qgsmssqlprovider.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ QgsMssqlNewConnection::QgsMssqlNewConnection( QWidget *parent, const QString &co

txtName->setText( connName );
}
txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]+" ), txtName ) );
txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( QStringLiteral( "[^\\/]+" ) ), txtName ) );
cb_trustedConnection_clicked();

schemaView->setModel( &mSchemaModel );
Expand Down
8 changes: 4 additions & 4 deletions src/providers/mssql/qgsmssqlprovider.cpp
Expand Up @@ -25,7 +25,7 @@
#include <QStringList>
#include <QMessageBox>
#include <QSettings>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
Expand Down Expand Up @@ -1301,11 +1301,11 @@ bool QgsMssqlProvider::addFeatures( QgsFeatureList &flist, Flags flags )
// Z and M on the end of a WKT string isn't valid for
// SQL Server so we have to remove it first.
wkt = geom.asWkt();
wkt.replace( QRegExp( "[mzMZ]+\\s*\\(" ), QStringLiteral( "(" ) );
wkt.replace( QRegularExpression( QStringLiteral( "[mzMZ]+\\s*\\(" ) ), QStringLiteral( "(" ) );
// if we have M value only, we need to insert null-s for the Z value
if ( QgsWkbTypes::hasM( geom.wkbType() ) && !QgsWkbTypes::hasZ( geom.wkbType() ) )
{
wkt.replace( QRegExp( "(?=\\s[0-9+-.]+[,)])" ), QStringLiteral( " NULL" ) );
wkt.replace( QRegularExpression( QStringLiteral( "(?=\\s[0-9+-.]+[,)])" ) ), QStringLiteral( " NULL" ) );
}
}
query.addBindValue( wkt );
Expand Down Expand Up @@ -1641,7 +1641,7 @@ bool QgsMssqlProvider::changeGeometryValues( const QgsGeometryMap &geometry_map
QString wkt = it->asWkt();
// Z and M on the end of a WKT string isn't valid for
// SQL Server so we have to remove it first.
wkt.replace( QRegExp( "[mzMZ]+\\s*\\(" ), QStringLiteral( "(" ) );
wkt.replace( QRegularExpression( QStringLiteral( "[mzMZ]+\\s*\\(" ) ), QStringLiteral( "(" ) );
query.addBindValue( wkt );
}

Expand Down
5 changes: 3 additions & 2 deletions src/providers/postgres/qgspgnewconnection.cpp
Expand Up @@ -17,7 +17,8 @@

#include <QMessageBox>
#include <QInputDialog>
#include <QRegExpValidator>
#include <QRegularExpressionValidator>
#include <QRegularExpression>

#include "qgspgnewconnection.h"
#include "qgsauthmanager.h"
Expand Down Expand Up @@ -112,7 +113,7 @@ QgsPgNewConnection::QgsPgNewConnection( QWidget *parent, const QString &connName

txtName->setText( connName );
}
txtName->setValidator( new QRegExpValidator( QRegExp( "[^\\/]*" ), txtName ) );
txtName->setValidator( new QRegularExpressionValidator( QRegularExpression( "[^\\/]*" ), txtName ) );
}

//! Autoconnected SLOTS
Expand Down
46 changes: 25 additions & 21 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsvectorlayer.h"

#include <QMessageBox>
#include <QRegularExpression>

#include "qgsvectorlayerexporter.h"
#include "qgspostgresprovider.h"
Expand Down Expand Up @@ -1006,11 +1007,12 @@ bool QgsPostgresProvider::loadFields()
}
else
{
QRegExp re( "numeric\\((\\d+),(\\d+)\\)" );
if ( re.exactMatch( formattedFieldType ) )
const QRegularExpression re( QRegularExpression::anchoredPattern( QStringLiteral( "numeric\\((\\d+),(\\d+)\\)" ) ) );
const QRegularExpressionMatch match = re.match( formattedFieldType );
if ( match.hasMatch() )
{
fieldSize = re.cap( 1 ).toInt();
fieldPrec = re.cap( 2 ).toInt();
fieldSize = match.captured( 1 ).toInt();
fieldPrec = match.captured( 2 ).toInt();
}
else if ( formattedFieldType != QLatin1String( "numeric" ) )
{
Expand All @@ -1027,10 +1029,11 @@ bool QgsPostgresProvider::loadFields()
{
fieldType = QVariant::String;

QRegExp re( "character varying\\((\\d+)\\)" );
if ( re.exactMatch( formattedFieldType ) )
const QRegularExpression re( QRegularExpression::anchoredPattern( "character varying\\((\\d+)\\)" ) );
const QRegularExpressionMatch match = re.match( formattedFieldType );
if ( match.hasMatch() )
{
fieldSize = re.cap( 1 ).toInt();
fieldSize = match.captured( 1 ).toInt();
}
else
{
Expand Down Expand Up @@ -1079,10 +1082,11 @@ bool QgsPostgresProvider::loadFields()

fieldType = QVariant::String;

QRegExp re( "character\\((\\d+)\\)" );
if ( re.exactMatch( formattedFieldType ) )
const QRegularExpression re( QRegularExpression::anchoredPattern( "character\\((\\d+)\\)" ) );
const QRegularExpressionMatch match = re.match( formattedFieldType );
if ( match.hasMatch() )
{
fieldSize = re.cap( 1 ).toInt();
fieldSize = match.captured( 1 ).toInt();
}
else
{
Expand All @@ -1097,10 +1101,11 @@ bool QgsPostgresProvider::loadFields()
{
fieldType = QVariant::String;

QRegExp re( "char\\((\\d+)\\)" );
if ( re.exactMatch( formattedFieldType ) )
const QRegularExpression re( QRegularExpression::anchoredPattern( QStringLiteral( "char\\((\\d+)\\)" ) ) );
const QRegularExpressionMatch match = re.match( formattedFieldType );
if ( match.hasMatch() )
{
fieldSize = re.cap( 1 ).toInt();
fieldSize = match.captured( 1 ).toInt();
}
else
{
Expand Down Expand Up @@ -3747,15 +3752,14 @@ QgsRectangle QgsPostgresProvider::extent() const
{
QgsDebugMsgLevel( "Got extents using: " + sql, 2 );

QRegExp rx( "\\((.+) (.+),(.+) (.+)\\)" );
if ( ext.contains( rx ) )
const QRegularExpression rx( "\\((.+) (.+),(.+) (.+)\\)" );
const QRegularExpressionMatch match = rx.match( ext );
if ( match.hasMatch() )
{
QStringList ex = rx.capturedTexts();

mLayerExtent.setXMinimum( ex[1].toDouble() );
mLayerExtent.setYMinimum( ex[2].toDouble() );
mLayerExtent.setXMaximum( ex[3].toDouble() );
mLayerExtent.setYMaximum( ex[4].toDouble() );
mLayerExtent.setXMinimum( match.captured( 1 ).toDouble() );
mLayerExtent.setYMinimum( match.captured( 2 ).toDouble() );
mLayerExtent.setXMaximum( match.captured( 3 ).toDouble() );
mLayerExtent.setYMaximum( match.captured( 4 ).toDouble() );
}
else
{
Expand Down
28 changes: 15 additions & 13 deletions src/providers/virtual/qgsvirtuallayerqueryparser.cpp
Expand Up @@ -20,7 +20,7 @@ email : hugo dot mercier at oslandia dot com

#include "sqlite3.h"

#include <QRegExp>
#include <QRegularExpression>
#include <QtDebug>

namespace QgsVirtualLayerQueryParser
Expand Down Expand Up @@ -72,13 +72,14 @@ namespace QgsVirtualLayerQueryParser

// look for special comments in SQL
// a column name followed by /*:type*/
QRegExp rx( "([a-zA-Z_\x80-\xFF][a-zA-Z0-9_\x80-\xFF]*)\\s*/\\*:(int|real|text|((?:multi)?(?:point|linestring|polygon)):(\\d+))\\s*\\*/", Qt::CaseInsensitive );
QRegularExpression rx( "([a-zA-Z_\x80-\xFF][a-zA-Z0-9_\x80-\xFF]*)\\s*/\\*:(int|real|text|((?:multi)?(?:point|linestring|polygon)):(\\d+))\\s*\\*/", QRegularExpression::CaseInsensitiveOption );
int pos = 0;

while ( ( pos = rx.indexIn( query, pos ) ) != -1 )
QRegularExpressionMatch match = rx.match( query, pos );
while ( match.hasMatch() )
{
QString column = rx.cap( 1 );
QString type = rx.cap( 2 );
QString column = match.captured( 1 );
QString type = match.captured( 2 );
ColumnDef def;
def.setName( column );
if ( type == QLatin1String( "int" ) )
Expand All @@ -90,12 +91,13 @@ namespace QgsVirtualLayerQueryParser
else
{
// there should be 2 more captures
def.setGeometry( QgsWkbTypes::parseType( rx.cap( 3 ) ) );
def.setSrid( static_cast<QgsWkbTypes::Type>( rx.cap( 4 ).toLong() ) );
def.setGeometry( QgsWkbTypes::parseType( match.captured( 3 ) ) );
def.setSrid( static_cast<QgsWkbTypes::Type>( match.captured( 4 ).toLong() ) );
}
defs[column] = def;

pos += rx.matchedLength();
pos += match.capturedLength();
match = rx.match( query, pos );
}
return defs;
}
Expand All @@ -104,7 +106,7 @@ namespace QgsVirtualLayerQueryParser
void setColumnDefType( const QString &columnType, ColumnDef &d )
{
// geometry type
QRegExp geometryTypeRx( "\\(([0-9]+),([0-9]+)\\)" );
const QRegularExpression geometryTypeRx( "\\(([0-9]+),([0-9]+)\\)" );

// see qgsvirtuallayersqlitemodule for possible declared types
// the type returned by PRAGMA table_info will be either
Expand All @@ -120,11 +122,11 @@ namespace QgsVirtualLayerQueryParser
{
// parse the geometry type and srid
// geometry(type,srid)
int pos = geometryTypeRx.indexIn( columnType, 0 );
if ( pos != -1 )
const QRegularExpressionMatch match = geometryTypeRx.match( columnType );
if ( match.hasMatch() )
{
QgsWkbTypes::Type type = static_cast<QgsWkbTypes::Type>( geometryTypeRx.cap( 1 ).toLong() );
long srid = geometryTypeRx.cap( 2 ).toLong();
QgsWkbTypes::Type type = static_cast<QgsWkbTypes::Type>( match.captured( 1 ).toLong() );
long srid = match.captured( 2 ).toLong();
d.setGeometry( type );
d.setSrid( srid );
}
Expand Down
13 changes: 7 additions & 6 deletions src/providers/wfs/qgswfscapabilities.cpp
Expand Up @@ -27,6 +27,7 @@
#include <QDomDocument>
#include <QStringList>
#include <QUrlQuery>
#include <QRegularExpression>

QgsWfsCapabilities::QgsWfsCapabilities( const QString &uri, const QgsDataProvider::ProviderOptions &options )
: QgsWfsRequest( QgsWFSDataSourceURI( uri ) ),
Expand Down Expand Up @@ -657,16 +658,16 @@ void QgsWfsCapabilities::capabilitiesReplyFinished()

QString QgsWfsCapabilities::NormalizeSRSName( QString crsName )
{
QRegExp re( "urn:ogc:def:crs:([^:]+).+([^:]+)", Qt::CaseInsensitive );
if ( re.exactMatch( crsName ) )
const QRegularExpression re( QRegularExpression::anchoredPattern( QStringLiteral( "urn:ogc:def:crs:([^:]+).+([^:]+)" ) ), QRegularExpression::CaseInsensitiveOption );
if ( const QRegularExpressionMatch match = re.match( crsName ); match.hasMatch() )
{
return re.cap( 1 ) + ':' + re.cap( 2 );
return match.captured( 1 ) + ':' + match.captured( 2 );
}
// urn:x-ogc:def:crs:EPSG:xxxx as returned by http://maps.warwickshire.gov.uk/gs/ows? in WFS 1.1
QRegExp re2( "urn:x-ogc:def:crs:([^:]+).+([^:]+)", Qt::CaseInsensitive );
if ( re2.exactMatch( crsName ) )
const QRegularExpression re2( QRegularExpression::anchoredPattern( QStringLiteral( "urn:x-ogc:def:crs:([^:]+).+([^:]+)" ) ), QRegularExpression::CaseInsensitiveOption );
if ( const QRegularExpressionMatch match = re2.match( crsName ); match.hasMatch() )
{
return re2.cap( 1 ) + ':' + re2.cap( 2 );
return match.captured( 1 ) + ':' + match.captured( 2 );
}
return crsName;
}
Expand Down
14 changes: 10 additions & 4 deletions src/providers/wfs/qgswfsprovider.cpp
Expand Up @@ -47,6 +47,7 @@
#include <QPair>
#include <QTimer>
#include <QUrlQuery>
#include <QRegularExpression>

#include <cfloat>

Expand Down Expand Up @@ -1527,8 +1528,8 @@ bool QgsWFSProvider::readAttributesFromSchema( QDomDocument &schemaDoc,
// attribute ref
QString ref = attributeElement.attribute( QStringLiteral( "ref" ) );

QRegExp gmlPT( "gml:(.*)PropertyType" );
QRegExp gmlRefProperty( "gml:(.*)Property" );
const QRegularExpression gmlPT( QStringLiteral( "gml:(.*)PropertyType" ) );
const QRegularExpression gmlRefProperty( QStringLiteral( "gml:(.*)Property" ) );

// gmgml: is Geomedia Web Server
if ( ! foundGeometryAttribute && type == QLatin1String( "gmgml:Polygon_Surface_MultiSurface_CompositeSurfacePropertyType" ) )
Expand Down Expand Up @@ -1560,15 +1561,20 @@ bool QgsWFSProvider::readAttributesFromSchema( QDomDocument &schemaDoc,
if ( attributeElement.parentNode().nodeName() == QLatin1String( "choice" ) && ! attributeElement.nextSibling().isNull() )
geomType = QgsWkbTypes::Unknown;
else
geomType = geomTypeFromPropertyType( geometryAttribute, gmlPT.cap( 1 ) );
{
const QRegularExpressionMatch match = gmlPT.match( type );
geomType = geomTypeFromPropertyType( geometryAttribute, match.captured( 1 ) );
}
}
//MH 090428: sometimes the <element> tags for geometry attributes have only attribute ref="gml:polygonProperty"
//Note: this was deprecated with GML3.
else if ( ! foundGeometryAttribute && ref.indexOf( gmlRefProperty ) == 0 )
{
foundGeometryAttribute = true;
geometryAttribute = ref.mid( 4 ); // Strip gml: prefix
QString propertyType( gmlRefProperty.cap( 1 ) );

const QRegularExpressionMatch match = gmlRefProperty.match( ref );
QString propertyType( match.captured( 1 ) );
// Set the first character in upper case
propertyType = propertyType.at( 0 ).toUpper() + propertyType.mid( 1 );
geomType = geomTypeFromPropertyType( geometryAttribute, propertyType );
Expand Down

0 comments on commit b53f97b

Please sign in to comment.