Skip to content
Permalink
Browse files
Better and faster strategy: replicate QRegExp::escape code
  • Loading branch information
nirvn committed Jul 14, 2021
1 parent 9769b69 commit 358a4af06a9e1680cb2220ca3d4c57bb515bb496
@@ -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

};
@@ -19,6 +19,7 @@

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

#include <QRegularExpression>

@@ -535,10 +536,7 @@ QVariant QgsExpressionNodeBinaryOperator::evalNode( QgsExpression *parent, const
bool matches;
if ( mOp == boLike || mOp == boILike || mOp == boNotLike || mOp == boNotILike ) // change from LIKE syntax to regexp
{
// recreate QRegExp::escape function behavior
const thread_local QRegularExpression escapeRx( QStringLiteral( "([^\\\\]|^)([\\$\\(\\)\\*\\+\\.\\?\\[\\]\\^\\{\\|\\}])" ) );
QString esc_regexp = regexp;
esc_regexp.replace( escapeRx, QStringLiteral( "\\1\\\\2" ) );
QString esc_regexp = QgsStringUtils::qRegExpEscape( regexp );
// manage escape % and _
if ( esc_regexp.startsWith( '%' ) )
{
@@ -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

0 comments on commit 358a4af

Please sign in to comment.