Skip to content

Commit

Permalink
Some provider QRegularExpression ports
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 1, 2021
1 parent 184e635 commit 1e3b545
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 43 deletions.
13 changes: 7 additions & 6 deletions src/providers/arcgisrest/qgsnewarcgisrestconnection.cpp
Expand Up @@ -23,8 +23,8 @@
#include <QMessageBox>
#include <QUrl>
#include <QPushButton>
#include <QRegExp>
#include <QRegExpValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QtEndian>
#include <QUrlQuery>

Expand All @@ -39,16 +39,17 @@ QgsNewArcGisRestConnectionDialog::QgsNewArcGisRestConnectionDialog( QWidget *par

connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewArcGisRestConnectionDialog::showHelp );

QRegExp rx( "/connections-([^/]+)/" );
if ( rx.indexIn( baseKey ) != -1 )
const QRegularExpression rx( QStringLiteral( "/connections-([^/]+)/" ) );
const QRegularExpressionMatch match = rx.match( baseKey );
if ( match.hasMatch() )
{
QString connectionType( rx.cap( 1 ).toUpper() );
QString connectionType( match.captured( 1 ).toUpper() );
setWindowTitle( tr( "Create a New %1 Connection" ).arg( connectionType ) );
}

mCredentialsBaseKey = mBaseKey.split( '-' ).last().toUpper();

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

if ( !connectionName.isEmpty() )
{
Expand Down
5 changes: 3 additions & 2 deletions src/providers/db2/qgsdb2newconnection.cpp
Expand Up @@ -19,7 +19,8 @@
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QRegExpValidator>
#include <QRegularExpression>
#include <QRegularExpressionValidator>

#include "qgssettings.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -80,7 +81,7 @@ QgsDb2NewConnection::QgsDb2NewConnection( QWidget *parent, const QString &connNa

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

//! Autoconnected SLOTS
Expand Down
35 changes: 21 additions & 14 deletions src/providers/delimitedtext/qgsdelimitedtextfile.cpp
Expand Up @@ -26,16 +26,17 @@
#include <QFileSystemWatcher>
#include <QTextCodec>
#include <QStringList>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
#include <QUrlQuery>

QgsDelimitedTextFile::QgsDelimitedTextFile( const QString &url )
: mFileName( QString() )
, mEncoding( QStringLiteral( "UTF-8" ) )
, mDefaultFieldName( QStringLiteral( "field_%1" ) )
, mDefaultFieldRegexp( "^(?:field_)(\\d+)$", Qt::CaseInsensitive )
, mDefaultFieldRegexp( QStringLiteral( "^(?:field_)(\\d+)$" ) )
{
mDefaultFieldRegexp.setPatternOptions( QRegularExpression::CaseInsensitiveOption );
// The default type is CSV
setTypeCSV();
if ( ! url.isNull() ) setFromUrl( url );
Expand Down Expand Up @@ -424,9 +425,9 @@ void QgsDelimitedTextFile::setFieldNames( const QStringList &names )
}
// If the name looks like a default field name (field_##), then it is
// valid if the number matches its column number..
else if ( mDefaultFieldRegexp.indexIn( name ) == 0 )
else if ( const QRegularExpressionMatch match = mDefaultFieldRegexp.match( name ); match.capturedStart() == 0 )
{
int col = mDefaultFieldRegexp.capturedTexts().at( 1 ).toInt();
int col = match.captured( 1 ).toInt();
nameOk = col == fieldNo;
}
// Otherwise it is valid if isn't the name of an existing field...
Expand Down Expand Up @@ -480,9 +481,9 @@ int QgsDelimitedTextFile::fieldIndex( const QString &name )
if ( mUseHeader && ! mFile ) reset();
// Try to determine the field based on a default field name, includes
// Field_### and simple integer fields.
if ( mDefaultFieldRegexp.indexIn( name ) == 0 )
if ( const QRegularExpressionMatch match = mDefaultFieldRegexp.match( name ); match.capturedStart() == 0 )
{
return mDefaultFieldRegexp.capturedTexts().at( 1 ).toInt() - 1;
return match.captured( 1 ).toInt() - 1;
}
for ( int i = 0; i < mFieldNames.size(); i++ )
{
Expand Down Expand Up @@ -716,8 +717,10 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::parseRegexp( QString &buffer,
// and extract capture groups
if ( mAnchoredRegexp )
{
if ( mDelimRegexp.indexIn( buffer ) < 0 ) return RecordInvalid;
QStringList groups = mDelimRegexp.capturedTexts();
const QRegularExpressionMatch match = mDelimRegexp.match( buffer );
if ( !match.hasMatch() )
return RecordInvalid;
const QStringList groups = match.capturedTexts();
for ( int i = 1; i < groups.size(); i++ )
{
appendField( fields, groups[i] );
Expand All @@ -729,15 +732,19 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::parseRegexp( QString &buffer,
int size = buffer.size();
while ( true )
{
if ( pos >= size ) break;
int matchPos = mDelimRegexp.indexIn( buffer, pos );
if ( pos >= size )
break;
QRegularExpressionMatch match = mDelimRegexp.match( buffer, pos );

int matchPos = match.capturedStart();
// If match won't advance cursor, then need to force it along one place
// to avoid infinite loop.
int matchLen = mDelimRegexp.matchedLength();
int matchLen = match.capturedLength();
if ( matchPos == pos && matchLen == 0 )
{
matchPos = mDelimRegexp.indexIn( buffer, pos + 1 );
matchLen = mDelimRegexp.matchedLength();
match = mDelimRegexp.match( buffer, pos + 1 );
matchPos = match.capturedStart();
matchLen = match.capturedLength();
}
// If no match, then field is to end of record
if ( matchPos < 0 )
Expand All @@ -750,7 +757,7 @@ QgsDelimitedTextFile::Status QgsDelimitedTextFile::parseRegexp( QString &buffer,
appendField( fields, buffer.mid( pos, matchPos - pos ) );
if ( mDelimRegexp.captureCount() > 0 )
{
QStringList groups = mDelimRegexp.capturedTexts();
QStringList groups = match.capturedTexts();
for ( int i = 1; i < groups.size(); i++ )
{
appendField( fields, groups[i] );
Expand Down
6 changes: 3 additions & 3 deletions src/providers/delimitedtext/qgsdelimitedtextfile.h
Expand Up @@ -19,7 +19,7 @@
#define QGSDELIMITEDTEXTFILE_H

#include <QStringList>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
#include <QObject>

Expand Down Expand Up @@ -419,7 +419,7 @@ class QgsDelimitedTextFile : public QObject
int mMaxNameLength = 200;

// Parameters used by parsers
QRegExp mDelimRegexp;
QRegularExpression mDelimRegexp;
bool mAnchoredRegexp = false;
QString mDelimChars;
QString mQuoteChar;
Expand All @@ -441,7 +441,7 @@ class QgsDelimitedTextFile : public QObject
int mMaxFieldCount = 0;

QString mDefaultFieldName;
QRegExp mDefaultFieldRegexp;
QRegularExpression mDefaultFieldRegexp;
};

#endif
24 changes: 14 additions & 10 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.cpp
Expand Up @@ -24,7 +24,7 @@
#include <QTextStream>
#include <QStringList>
#include <QSettings>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
#include <QUrlQuery>

Expand Down Expand Up @@ -56,8 +56,8 @@ const QString QgsDelimitedTextProvider::TEXT_PROVIDER_DESCRIPTION = QStringLiter

static const int SUBSET_ID_THRESHOLD_FACTOR = 10;

QRegExp QgsDelimitedTextProvider::sWktPrefixRegexp( "^\\s*(?:\\d+\\s+|SRID\\=\\d+\\;)", Qt::CaseInsensitive );
QRegExp QgsDelimitedTextProvider::sCrdDmsRegexp( "^\\s*(?:([-+nsew])\\s*)?(\\d{1,3})(?:[^0-9.]+([0-5]?\\d))?[^0-9.]+([0-5]?\\d(?:\\.\\d+)?)[^0-9.]*([-+nsew])?\\s*$", Qt::CaseInsensitive );
QRegularExpression QgsDelimitedTextProvider::sWktPrefixRegexp( QStringLiteral( "^\\s*(?:\\d+\\s+|SRID\\=\\d+\\;)" ), QRegularExpression::CaseInsensitiveOption );
QRegularExpression QgsDelimitedTextProvider::sCrdDmsRegexp( QStringLiteral( "^\\s*(?:([-+nsew])\\s*)?(\\d{1,3})(?:[^0-9.]+([0-5]?\\d))?[^0-9.]+([0-5]?\\d(?:\\.\\d+)?)[^0-9.]*([-+nsew])?\\s*$" ), QRegularExpression::CaseInsensitiveOption );

QgsDelimitedTextProvider::QgsDelimitedTextProvider( const QString &uri, const ProviderOptions &options, QgsDataProvider::ReadFlags flags )
: QgsVectorDataProvider( uri, options, flags )
Expand Down Expand Up @@ -223,8 +223,9 @@ QStringList QgsDelimitedTextProvider::readCsvtFieldTypes( const QString &filenam
// not allowed in OGR CSVT files. Also doesn't care if int and string fields have

strTypeList = strTypeList.toLower();
QRegExp reTypeList( "^(?:\\s*(\\\"?)(?:integer|real|double|long|longlong|int8|string|date|datetime|time)(?:\\(\\d+(?:\\.\\d+)?\\))?\\1\\s*(?:,|$))+" );
if ( ! reTypeList.exactMatch( strTypeList ) )
const QRegularExpression reTypeList( QRegularExpression::anchoredPattern( QStringLiteral( "^(?:\\s*(\\\"?)(?:integer|real|double|long|longlong|int8|string|date|datetime|time)(?:\\(\\d+(?:\\.\\d+)?\\))?\\1\\s*(?:,|$))+" ) ) );
const QRegularExpressionMatch match = reTypeList.match( strTypeList );
if ( !match.hasMatch() )
{
// Looks like this was supposed to be a CSVT file, so report bad formatted string
if ( message ) { *message = tr( "File type string in %1 is not correctly formatted" ).arg( csvtInfo.fileName() ); }
Expand All @@ -236,13 +237,16 @@ QStringList QgsDelimitedTextProvider::readCsvtFieldTypes( const QString &filenam
QgsDebugMsgLevel( QStringLiteral( "Field type string: %1" ).arg( strTypeList ), 2 );

int pos = 0;
QRegExp reType( "(integer|real|double|string|date|datetime|time)" );
const QRegularExpression reType( QStringLiteral( "(integer|real|double|string|date|datetime|time)" ) );

while ( ( pos = reType.indexIn( strTypeList, pos ) ) != -1 )
QRegularExpressionMatch typeMatch = reType.match( strTypeList, pos );
while ( typeMatch.hasMatch() )
{
QgsDebugMsgLevel( QStringLiteral( "Found type: %1" ).arg( reType.cap( 1 ) ), 2 );
types << reType.cap( 1 );
pos += reType.matchedLength();
QgsDebugMsgLevel( QStringLiteral( "Found type: %1" ).arg( typeMatch.captured( 1 ) ), 2 );
types << typeMatch.captured( 1 );
pos += typeMatch.capturedLength();

typeMatch = reType.match( strTypeList, pos );
}

if ( message )
Expand Down
5 changes: 3 additions & 2 deletions src/providers/delimitedtext/qgsdelimitedtextprovider.h
Expand Up @@ -19,6 +19,7 @@
#define QGSDELIMITEDTEXTPROVIDER_H

#include <QStringList>
#include <QRegularExpression>

#include "qgsvectordataprovider.h"
#include "qgscoordinatereferencesystem.h"
Expand Down Expand Up @@ -70,8 +71,8 @@ class QgsDelimitedTextProvider final: public QgsVectorDataProvider
* Regular expression defining possible prefixes to WKT string,
* (EWKT srid, Informix SRID)
*/
static QRegExp sWktPrefixRegexp;
static QRegExp sCrdDmsRegexp;
static QRegularExpression sWktPrefixRegexp;
static QRegularExpression sCrdDmsRegexp;

enum GeomRepresentationType
{
Expand Down
13 changes: 7 additions & 6 deletions src/providers/delimitedtext/qgsdelimitedtextsourceselect.cpp
Expand Up @@ -27,7 +27,7 @@
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QRegExp>
#include <QRegularExpression>
#include <QTextStream>
#include <QTextCodec>
#include <QUrl>
Expand Down Expand Up @@ -123,7 +123,7 @@ void QgsDelimitedTextSourceSelect::addButtonClicked()
}
if ( delimiterRegexp->isChecked() )
{
QRegExp re( txtDelimiterRegexp->text() );
const QRegularExpression re( txtDelimiterRegexp->text() );
if ( ! re.isValid() )
{
QMessageBox::warning( this, tr( "Invalid regular expression" ), tr( "Please enter a valid regular expression as the delimiter, or choose a different delimiter type" ) );
Expand Down Expand Up @@ -256,7 +256,7 @@ void QgsDelimitedTextSourceSelect::setSelectedChars( const QString &delimiters )
cbxDelimTab->setChecked( chars.contains( '\t' ) );
cbxDelimColon->setChecked( chars.contains( ':' ) );
cbxDelimSemicolon->setChecked( chars.contains( ';' ) );
chars = chars.remove( QRegExp( "[ ,:;\t]" ) );
chars = chars.remove( QRegularExpression( QStringLiteral( "[ ,:;\t]" ) ) );
chars = QgsDelimitedTextFile::encodeChars( chars );
txtDelimiterOther->setText( chars );
}
Expand Down Expand Up @@ -453,7 +453,7 @@ void QgsDelimitedTextSourceSelect::updateFieldLists()
int counter = 0;
mBadRowCount = 0;
QStringList values;
QRegExp wktre( "^\\s*(?:MULTI)?(?:POINT|LINESTRING|POLYGON)\\s*Z?\\s*M?\\(", Qt::CaseInsensitive );
const QRegularExpression wktre( "^\\s*(?:MULTI)?(?:POINT|LINESTRING|POLYGON)\\s*Z?\\s*M?\\(", QRegularExpression::CaseInsensitiveOption );

while ( counter < mExampleRowCount )
{
Expand Down Expand Up @@ -506,7 +506,8 @@ void QgsDelimitedTextSourceSelect::updateFieldLists()
}
if ( xyDms )
{
ok = QgsDelimitedTextProvider::sCrdDmsRegexp.indexIn( value ) == 0;
const QRegularExpressionMatch match = QgsDelimitedTextProvider::sCrdDmsRegexp.match( value );
ok = match.capturedStart() == 0;
}
else
{
Expand Down Expand Up @@ -717,7 +718,7 @@ bool QgsDelimitedTextSourceSelect::validate()

if ( message.isEmpty() && delimiterRegexp->isChecked() )
{
QRegExp re( txtDelimiterRegexp->text() );
const QRegularExpression re( txtDelimiterRegexp->text() );
if ( ! re.isValid() )
{
message = tr( "Regular expression is not valid" );
Expand Down

0 comments on commit 1e3b545

Please sign in to comment.