Skip to content
Permalink
Browse files
Merge pull request #44137 from nirvn/core_regexp_more
[qt6] A few more QRegExp aurevoirs in src/core
  • Loading branch information
nirvn committed Jul 14, 2021
2 parents 298a451 + 358a4af commit 94aa4a4f76412b46d1365e15065359cd53d7fcda
@@ -315,6 +315,17 @@ Convert simple HTML to markdown. Only br, b and link are supported.
:return: String formatted as markdown

.. versionadded:: 3.10
%End

static QString qRegExpEscape( const QString &string );
%Docstring
Returns an escaped string matching the behavior of QRegExp.escape.

:param string: String to escape

:return: Escaped string

.. versionadded:: 3.22
%End

};
@@ -499,14 +499,13 @@ QSet<QString> QgsExpression::referencedVariables( const QString &text )
int index = 0;
while ( index < text.size() )
{
QRegExp rx = QRegExp( "\\[%([^\\]]+)%\\]" );

int pos = rx.indexIn( text, index );
if ( pos < 0 )
const thread_local QRegularExpression rx( "\\[%([^\\]]+)%\\]" );
const QRegularExpressionMatch match = rx.match( text );
if ( !match.hasMatch() )
break;

index = pos + rx.matchedLength();
QString to_replace = rx.cap( 1 ).trimmed();
index = match.capturedStart() + match.capturedLength();
QString to_replace = match.captured( 1 ).trimmed();

QgsExpression exp( to_replace );
variables.unite( exp.referencedVariables() );
@@ -19,6 +19,7 @@

#include "qgsgeometry.h"
#include "qgsfeaturerequest.h"
#include "qgsstringutils.h"

#include <QRegularExpression>

@@ -531,39 +532,39 @@ QVariant QgsExpressionNodeBinaryOperator::evalNode( QgsExpression *parent, const
ENSURE_NO_EVAL_ERROR
QString regexp = QgsExpressionUtils::getStringValue( vR, parent );
ENSURE_NO_EVAL_ERROR
// TODO: cache QRegExp in case that regexp is a literal string (i.e. it will stay constant)
// TODO: cache QRegularExpression in case that regexp is a literal string (i.e. it will stay constant)
bool matches;
if ( mOp == boLike || mOp == boILike || mOp == boNotLike || mOp == boNotILike ) // change from LIKE syntax to regexp
{
QString esc_regexp = QRegExp::escape( regexp );
QString esc_regexp = QgsStringUtils::qRegExpEscape( regexp );
// manage escape % and _
if ( esc_regexp.startsWith( '%' ) )
{
esc_regexp.replace( 0, 1, QStringLiteral( ".*" ) );
}
thread_local QRegExp rx1( QStringLiteral( "[^\\\\](%)" ) );
const thread_local QRegularExpression rx1( QStringLiteral( "[^\\\\](%)" ) );
int pos = 0;
while ( ( pos = rx1.indexIn( esc_regexp, pos ) ) != -1 )
while ( ( pos = esc_regexp.indexOf( rx1, pos ) ) != -1 )
{
esc_regexp.replace( pos + 1, 1, QStringLiteral( ".*" ) );
pos += 1;
}
thread_local QRegExp rx2( QStringLiteral( "\\\\%" ) );
const thread_local QRegularExpression rx2( QStringLiteral( "\\\\%" ) );
esc_regexp.replace( rx2, QStringLiteral( "%" ) );
if ( esc_regexp.startsWith( '_' ) )
{
esc_regexp.replace( 0, 1, QStringLiteral( "." ) );
}
thread_local QRegExp rx3( QStringLiteral( "[^\\\\](_)" ) );
const thread_local QRegularExpression rx3( QStringLiteral( "[^\\\\](_)" ) );
pos = 0;
while ( ( pos = rx3.indexIn( esc_regexp, pos ) ) != -1 )
while ( ( pos = esc_regexp.indexOf( rx3, pos ) ) != -1 )
{
esc_regexp.replace( pos + 1, 1, '.' );
pos += 1;
}
esc_regexp.replace( QLatin1String( "\\\\_" ), QLatin1String( "_" ) );

matches = QRegExp( esc_regexp, mOp == boLike || mOp == boNotLike ? Qt::CaseSensitive : Qt::CaseInsensitive ).exactMatch( str );
matches = QRegularExpression( QRegularExpression::anchoredPattern( esc_regexp ), mOp == boLike || mOp == boNotLike ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption ).match( str ).hasMatch();
}
else
{
@@ -1479,8 +1480,9 @@ bool QgsExpressionNodeColumnRef::prepareNode( QgsExpression *parent, const QgsEx

QString QgsExpressionNodeColumnRef::dump() const
{
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 );
const thread_local QRegularExpression re( QStringLiteral( "^[A-Za-z_\x80-\xff][A-Za-z0-9_\x80-\xff]*$" ) );
const QRegularExpressionMatch match = re.match( mName );
return match.hasMatch() ? mName : QgsExpression::quotedColumnRef( mName );
}

QSet<QString> QgsExpressionNodeColumnRef::referencedColumns() const
@@ -30,10 +30,12 @@
#include "qgsprovidersublayerdetails.h"

#include <QFileInfo>
#include <mutex>
#include <QRegularExpression>
#include <QUrlQuery>
#include <QUrl>

#include <mutex>

// defined in qgsgdalprovider.cpp
void buildSupportedRasterFileFilterAndExtensions( QString &fileFiltersString, QStringList &extensions, QStringList &wildcards );

@@ -225,8 +227,8 @@ QgsDataItem *QgsGdalDataItemProvider::createDataItem( const QString &pathIn, Qgs
const auto constSWildcards = sWildcards;
for ( const QString &wildcard : constSWildcards )
{
QRegExp rx( wildcard, Qt::CaseInsensitive, QRegExp::Wildcard );
if ( rx.exactMatch( info.fileName() ) )
const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( wildcard ), QRegularExpression::CaseInsensitiveOption );
if ( rx.match( info.fileName() ).hasMatch() )
{
matches = true;
break;
@@ -2455,7 +2455,7 @@ void buildSupportedRasterFileFilterAndExtensions( QString &fileFiltersString, QS
QString myGdalDriverLongName = GDALGetMetadataItem( myGdalDriver, GDAL_DMD_LONGNAME, "" );
// remove any superfluous (.*) strings at the end as
// they'll confuse QFileDialog::getOpenFileNames()
myGdalDriverLongName.remove( QRegExp( "\\(.*\\)$" ) );
myGdalDriverLongName.remove( QRegularExpression( "\\(.*\\)$" ) );

// if we have both the file name extension and the long name,
// then we've all the information we need for the current
@@ -630,8 +630,8 @@ QgsDataItem *QgsOgrDataItemProvider::createDataItem( const QString &pathIn, QgsD
const auto constWildcards = QgsOgrProviderUtils::wildcards();
for ( const QString &wildcard : constWildcards )
{
QRegExp rx( wildcard, Qt::CaseInsensitive, QRegExp::Wildcard );
if ( rx.exactMatch( info.fileName() ) )
const QRegularExpression rx( QRegularExpression::wildcardToRegularExpression( wildcard ), QRegularExpression::CaseInsensitiveOption );
if ( rx.match( info.fileName() ).hasMatch() )
{
matches = true;
break;
@@ -18,10 +18,6 @@

#include "qgsproviderregistry.h"

#include <QString>
#include <QDir>
#include <QLibrary>

#include "qgis.h"
#include "qgsdataprovider.h"
#include "qgsdataitemprovider.h"
@@ -51,6 +47,11 @@
#include "qgspostgresprovider.h"
#endif

#include <QString>
#include <QDir>
#include <QLibrary>
#include <QRegularExpression>

static QgsProviderRegistry *sInstance = nullptr;

QgsProviderRegistry *QgsProviderRegistry::instance( const QString &pluginPath )
@@ -218,7 +219,7 @@ void QgsProviderRegistry::init()

// provider file regex pattern, only files matching the pattern are loaded if the variable is defined
QString filePattern = getenv( "QGIS_PROVIDER_FILE" );
QRegExp fileRegexp;
QRegularExpression fileRegexp;
if ( !filePattern.isEmpty() )
{
fileRegexp.setPattern( filePattern );
@@ -229,9 +230,9 @@ void QgsProviderRegistry::init()
const auto constEntryInfoList = mLibraryDirectory.entryInfoList();
for ( const QFileInfo &fi : constEntryInfoList )
{
if ( !fileRegexp.isEmpty() )
if ( !filePattern.isEmpty() )
{
if ( fileRegexp.indexIn( fi.fileName() ) == -1 )
if ( fi.fileName().indexOf( fileRegexp ) == -1 )
{
QgsDebugMsg( "provider " + fi.fileName() + " skipped because doesn't match pattern " + filePattern );
continue;
@@ -688,6 +688,39 @@ QString QgsStringUtils::substituteVerticalCharacters( QString string )
return string;
}

QString QgsStringUtils::qRegExpEscape( const QString &string )
{
// code and logic taken from the Qt source code
const QLatin1Char backslash( '\\' );
const int count = string.count();

QString escaped;
escaped.reserve( count * 2 );
for ( int i = 0; i < count; i++ )
{
switch ( string.at( i ).toLatin1() )
{
case '$':
case '(':
case ')':
case '*':
case '+':
case '.':
case '?':
case '[':
case '\\':
case ']':
case '^':
case '{':
case '|':
case '}':
escaped.append( backslash );
}
escaped.append( string.at( i ) );
}
return escaped;
}

QgsStringReplacement::QgsStringReplacement( const QString &match, const QString &replacement, bool caseSensitive, bool wholeWordOnly )
: mMatch( match )
, mReplacement( replacement )
@@ -307,6 +307,14 @@ class CORE_EXPORT QgsStringUtils
*/
static QString htmlToMarkdown( const QString &html );

/**
* Returns an escaped string matching the behavior of QRegExp::escape.
* \param string String to escape
* \returns Escaped string
* \since QGIS 3.22
*/
static QString qRegExpEscape( const QString &string );

};

#endif //QGSSTRINGUTILS_H
@@ -15,15 +15,6 @@
* *
***************************************************************************/

#include <QApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QVector>
#include <QStyle>
#include <QDomDocument>
#include <QDomElement>

#include "qgssettings.h"
#include "qgscptcityarchive.h"
#include "qgis.h"
@@ -34,6 +25,16 @@
#include "qgsapplication.h"
#include "qgssymbollayerutils.h"

#include <QApplication>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QVector>
#include <QStyle>
#include <QDomDocument>
#include <QDomElement>
#include <QRegularExpression>

typedef QMap< QString, QgsCptCityArchive * > ArchiveRegistry;
typedef QMap< QString, QMap< QString, QString > > CopyingInfoMap;

@@ -974,12 +975,12 @@ QMap< QString, QStringList > QgsCptCityDirectoryItem::rampsMap()
}
else
{
QRegExp rxVariant( "^(.*[^\\d])(\\d{1,3})$" );
int pos = rxVariant.indexIn( schemeName );
if ( pos > -1 )
const thread_local QRegularExpression rxVariant( "^(.*[^\\d])(\\d{1,3})$" );
const QRegularExpressionMatch match = rxVariant.match( schemeName );
if ( match.hasMatch() )
{
curName = rxVariant.cap( 1 );
curVariant = rxVariant.cap( 2 );
curName = match.captured( 1 );
curVariant = match.captured( 2 );
}
}

@@ -16,6 +16,7 @@
#include "qgsrendererrange.h"
#include "qgsclassificationmethod.h"
#include "qgssymbol.h"

#include <QLocale>


@@ -220,7 +221,7 @@ QString QgsRendererRangeLabelFormat::formatNumber( double value ) const
QString valueStr = QLocale().toString( value, 'f', mPrecision );
if ( mTrimTrailingZeroes )
valueStr = valueStr.remove( mReTrailingZeroes );
if ( mReNegativeZero.exactMatch( valueStr ) )
if ( mReNegativeZero.match( valueStr ).hasMatch() )
valueStr = valueStr.mid( 1 );
return valueStr;
}
@@ -16,12 +16,12 @@
#ifndef QGSRENDERERRANGE_H
#define QGSRENDERERRANGE_H

#include <QRegExp>

#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgssymbollayerutils.h"

#include <QRegularExpression>

class QDomDocument;
class QDomElement;

@@ -141,8 +141,8 @@ class CORE_EXPORT Q_DECL_DEPRECATED QgsRendererRangeLabelFormat SIP_DEPRECATED
// values used to manage number formatting - precision and trailing zeroes
double mNumberScale = 1.0;
QString mNumberSuffix;
QRegExp mReTrailingZeroes;
QRegExp mReNegativeZero;
QRegularExpression mReTrailingZeroes;
QRegularExpression mReNegativeZero;
};


0 comments on commit 94aa4a4

Please sign in to comment.