Skip to content

Commit 0847f55

Browse files
committedJul 13, 2021
[qt6] Mirate string utils class away from QRegExp
1 parent 666b229 commit 0847f55

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed
 

‎python/core/auto_generated/qgsstringutils.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212

13+
1314
class QgsStringReplacement
1415
{
1516
%Docstring(signature="appended")

‎src/core/qgsstringutils.cpp

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "qgsstringutils.h"
1717
#include "qgslogger.h"
1818
#include <QVector>
19-
#include <QRegExp>
2019
#include <QStringList>
2120
#include <QTextBoundaryFinder>
2221
#include <QRegularExpression>
@@ -524,33 +523,38 @@ QString QgsStringUtils::insertLinks( const QString &string, bool *foundLinks )
524523

525524
// http://alanstorm.com/url_regex_explained
526525
// note - there's more robust implementations available, but we need one which works within the limitation of QRegExp
527-
static QRegExp urlRegEx( "(\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~\\s]|/))))" );
528-
static QRegExp protoRegEx( "^(?:f|ht)tps?://|file://" );
529-
static QRegExp emailRegEx( "([\\w._%+-]+@[\\w.-]+\\.[A-Za-z]+)" );
526+
static thread_local QRegularExpression urlRegEx( "(\\b(([\\w-]+://?|www[.])[^\\s()<>]+(?:\\([\\w\\d]+\\)|([^!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~\\s]|/))))" );
527+
static thread_local QRegularExpression protoRegEx( "^(?:f|ht)tps?://|file://" );
528+
static thread_local QRegularExpression emailRegEx( "([\\w._%+-]+@[\\w.-]+\\.[A-Za-z]+)" );
530529

531530
int offset = 0;
532531
bool found = false;
533-
while ( urlRegEx.indexIn( converted, offset ) != -1 )
532+
QRegularExpressionMatch match = urlRegEx.match( converted );
533+
while ( match.hasMatch() )
534534
{
535535
found = true;
536-
QString url = urlRegEx.cap( 1 );
536+
QString url = match.captured( 1 );
537537
QString protoUrl = url;
538-
if ( protoRegEx.indexIn( protoUrl ) == -1 )
538+
if ( !protoRegEx.match( protoUrl ).hasMatch() )
539539
{
540540
protoUrl.prepend( "http://" );
541541
}
542542
QString anchor = QStringLiteral( "<a href=\"%1\">%2</a>" ).arg( protoUrl.toHtmlEscaped(), url.toHtmlEscaped() );
543-
converted.replace( urlRegEx.pos( 1 ), url.length(), anchor );
544-
offset = urlRegEx.pos( 1 ) + anchor.length();
543+
converted.replace( match.capturedStart( 1 ), url.length(), anchor );
544+
offset = match.capturedStart( 1 ) + anchor.length();
545+
match = urlRegEx.match( converted, offset );
545546
}
547+
546548
offset = 0;
547-
while ( emailRegEx.indexIn( converted, offset ) != -1 )
549+
match = emailRegEx.match( converted );
550+
while ( match.hasMatch() )
548551
{
549552
found = true;
550-
QString email = emailRegEx.cap( 1 );
553+
QString email = match.captured( 1 );
551554
QString anchor = QStringLiteral( "<a href=\"mailto:%1\">%1</a>" ).arg( email.toHtmlEscaped() );
552-
converted.replace( emailRegEx.pos( 1 ), email.length(), anchor );
553-
offset = emailRegEx.pos( 1 ) + anchor.length();
555+
converted.replace( match.capturedStart( 1 ), email.length(), anchor );
556+
offset = match.capturedStart( 1 ) + anchor.length();
557+
match = emailRegEx.match( converted, offset );
554558
}
555559

556560
if ( foundLinks )
@@ -567,16 +571,19 @@ QString QgsStringUtils::htmlToMarkdown( const QString &html )
567571
converted.replace( QLatin1String( "<b>" ), QLatin1String( "**" ) );
568572
converted.replace( QLatin1String( "</b>" ), QLatin1String( "**" ) );
569573

570-
static QRegExp hrefRegEx( "<a\\s+href\\s*=\\s*([^<>]*)\\s*>([^<>]*)</a>" );
574+
static thread_local QRegularExpression hrefRegEx( "<a\\s+href\\s*=\\s*([^<>]*)\\s*>([^<>]*)</a>" );
575+
571576
int offset = 0;
572-
while ( hrefRegEx.indexIn( converted, offset ) != -1 )
577+
QRegularExpressionMatch match = hrefRegEx.match( converted );
578+
while ( match.hasMatch() )
573579
{
574-
QString url = hrefRegEx.cap( 1 ).replace( QLatin1String( "\"" ), QString() );
580+
QString url = match.captured( 1 ).replace( QLatin1String( "\"" ), QString() );
575581
url.replace( '\'', QString() );
576-
QString name = hrefRegEx.cap( 2 );
582+
QString name = match.captured( 2 );
577583
QString anchor = QStringLiteral( "[%1](%2)" ).arg( name, url );
578-
converted.replace( hrefRegEx, anchor );
579-
offset = hrefRegEx.pos( 1 ) + anchor.length();
584+
converted.replace( match.capturedStart(), match.capturedLength(), anchor );
585+
offset = match.capturedStart() + anchor.length();
586+
match = hrefRegEx.match( converted, offset );
580587
}
581588

582589
return converted;
@@ -588,19 +595,18 @@ QString QgsStringUtils::wordWrap( const QString &string, const int length, const
588595
return string;
589596

590597
QString newstr;
591-
QRegExp rx;
598+
QRegularExpression rx;
592599
int delimiterLength = 0;
593600

594601
if ( !customDelimiter.isEmpty() )
595602
{
596-
rx.setPatternSyntax( QRegExp::FixedString );
597-
rx.setPattern( customDelimiter );
603+
rx.setPattern( QRegularExpression::escape( customDelimiter ) );
598604
delimiterLength = customDelimiter.length();
599605
}
600606
else
601607
{
602-
// \x200B is a ZERO-WIDTH SPACE, needed for worwrap to support a number of complex scripts (Indic, Arabic, etc.)
603-
rx.setPattern( QStringLiteral( "[\\s\\x200B]" ) );
608+
// \x{200B} is a ZERO-WIDTH SPACE, needed for worwrap to support a number of complex scripts (Indic, Arabic, etc.)
609+
rx.setPattern( QStringLiteral( "[\\x{200B}\\s]" ) );
604610
delimiterLength = 1;
605611
}
606612

@@ -689,8 +695,10 @@ QgsStringReplacement::QgsStringReplacement( const QString &match, const QString
689695
, mWholeWordOnly( wholeWordOnly )
690696
{
691697
if ( mWholeWordOnly )
692-
mRx = QRegExp( QString( "\\b%1\\b" ).arg( mMatch ),
693-
mCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive );
698+
{
699+
mRx.setPattern( QString( "\\b%1\\b" ).arg( mMatch ) );
700+
mRx.setPatternOptions( mCaseSensitive ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption );
701+
}
694702
}
695703

696704
QString QgsStringReplacement::process( const QString &input ) const

‎src/core/qgsstringutils.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
***************************************************************************/
1515

1616
#include "qgis_core.h"
17+
#include "qgis.h"
18+
1719
#include <QString>
18-
#include <QRegExp>
20+
#include <QRegularExpression>
1921
#include <QList>
2022
#include <QDomDocument>
2123
#include <QFont> // for enum values
22-
#include "qgis.h"
2324

2425
#ifndef QGSSTRINGUTILS_H
2526
#define QGSSTRINGUTILS_H
@@ -101,7 +102,7 @@ class CORE_EXPORT QgsStringReplacement
101102

102103
bool mWholeWordOnly;
103104

104-
QRegExp mRx;
105+
QRegularExpression mRx;
105106
};
106107

107108

0 commit comments

Comments
 (0)