Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Migrate away from QRegExp in the core application class
  • Loading branch information
nirvn committed Jul 14, 2021
1 parent 5762fe3 commit 03d99a4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
12 changes: 11 additions & 1 deletion python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -380,9 +380,19 @@ colors are specified then the ``size`` argument also must be set.
Returns the path to user's style.
%End

static QRegExp shortNameRegExp();

static QRegExp shortNameRegExp() /Deprecated/;
%Docstring
Returns the short name regular expression for line edit validator

.. deprecated:: QGIS 3.22
%End

static QRegularExpression shortNameRegularExpression();
%Docstring
Returns the short name regular expression for line edit validator

.. versionadded:: 3.22
%End

static QString userLoginName();
Expand Down
7 changes: 4 additions & 3 deletions src/core/project/qgsprojectservervalidator.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsprojectservervalidator.h"
#include "qgsvectorlayer.h"

#include <QRegularExpression>

QString QgsProjectServerValidator::displayValidationError( QgsProjectServerValidator::ValidationError error )
{
Expand Down Expand Up @@ -94,11 +95,11 @@ bool QgsProjectServerValidator::validate( QgsProject *project, QList<QgsProjectS
browseLayerTree( project->layerTreeRoot(), owsNames, encodingMessages );

QStringList duplicateNames, regExpMessages;
QRegExp snRegExp = QgsApplication::shortNameRegExp();
const QRegularExpression snRegExp = QgsApplication::shortNameRegularExpression();
const auto constOwsNames = owsNames;
for ( const QString &name : constOwsNames )
{
if ( !snRegExp.exactMatch( name ) )
if ( !snRegExp.match( name ).hasMatch() )
{
regExpMessages << name;
}
Expand Down Expand Up @@ -146,7 +147,7 @@ bool QgsProjectServerValidator::validate( QgsProject *project, QList<QgsProjectS
results << ValidationResult( QgsProjectServerValidator::ProjectRootNameConflict, rootLayerName );
}

if ( !snRegExp.exactMatch( rootLayerName ) )
if ( !snRegExp.match( rootLayerName ).hasMatch() )
{
result = false;
results << ValidationResult( QgsProjectServerValidator::ProjectShortName, rootLayerName );
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -1178,11 +1178,19 @@ QString QgsApplication::userStylePath()
return qgisSettingsDirPath() + QStringLiteral( "symbology-style.db" );
}

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QRegExp QgsApplication::shortNameRegExp()
{
const thread_local QRegExp regexp( QStringLiteral( "^[A-Za-z][A-Za-z0-9\\._-]*" ) );
return regexp;
}
#endif

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

QString QgsApplication::userLoginName()
{
Expand Down
16 changes: 14 additions & 2 deletions src/core/qgsapplication.h
Expand Up @@ -404,8 +404,20 @@ class CORE_EXPORT QgsApplication : public QApplication
//! Returns the path to user's style.
static QString userStylePath();

//! Returns the short name regular expression for line edit validator
static QRegExp shortNameRegExp();
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)

/**
* Returns the short name regular expression for line edit validator
* \deprecated since QGIS 3.22
*/
static QRegExp shortNameRegExp() SIP_DEPRECATED;
#endif

/**
* Returns the short name regular expression for line edit validator
* \since QGIS 3.22
*/
static QRegularExpression shortNameRegularExpression();

/**
* Returns the user's operating system login account name.
Expand Down

0 comments on commit 03d99a4

Please sign in to comment.