16
16
#include " qgsstringutils.h"
17
17
#include " qgslogger.h"
18
18
#include < QVector>
19
- #include < QRegExp>
20
19
#include < QStringList>
21
20
#include < QTextBoundaryFinder>
22
21
#include < QRegularExpression>
@@ -524,33 +523,38 @@ QString QgsStringUtils::insertLinks( const QString &string, bool *foundLinks )
524
523
525
524
// http://alanstorm.com/url_regex_explained
526
525
// 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]+)" );
530
529
531
530
int offset = 0 ;
532
531
bool found = false ;
533
- while ( urlRegEx.indexIn ( converted, offset ) != -1 )
532
+ QRegularExpressionMatch match = urlRegEx.match ( converted );
533
+ while ( match.hasMatch () )
534
534
{
535
535
found = true ;
536
- QString url = urlRegEx. cap ( 1 );
536
+ QString url = match. captured ( 1 );
537
537
QString protoUrl = url;
538
- if ( protoRegEx.indexIn ( protoUrl ) == - 1 )
538
+ if ( ! protoRegEx.match ( protoUrl ). hasMatch () )
539
539
{
540
540
protoUrl.prepend ( " http://" );
541
541
}
542
542
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 );
545
546
}
547
+
546
548
offset = 0 ;
547
- while ( emailRegEx.indexIn ( converted, offset ) != -1 )
549
+ match = emailRegEx.match ( converted );
550
+ while ( match.hasMatch () )
548
551
{
549
552
found = true ;
550
- QString email = emailRegEx. cap ( 1 );
553
+ QString email = match. captured ( 1 );
551
554
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 );
554
558
}
555
559
556
560
if ( foundLinks )
@@ -567,16 +571,19 @@ QString QgsStringUtils::htmlToMarkdown( const QString &html )
567
571
converted.replace ( QLatin1String ( " <b>" ), QLatin1String ( " **" ) );
568
572
converted.replace ( QLatin1String ( " </b>" ), QLatin1String ( " **" ) );
569
573
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
+
571
576
int offset = 0 ;
572
- while ( hrefRegEx.indexIn ( converted, offset ) != -1 )
577
+ QRegularExpressionMatch match = hrefRegEx.match ( converted );
578
+ while ( match.hasMatch () )
573
579
{
574
- QString url = hrefRegEx. cap ( 1 ).replace ( QLatin1String ( " \" " ), QString () );
580
+ QString url = match. captured ( 1 ).replace ( QLatin1String ( " \" " ), QString () );
575
581
url.replace ( ' \' ' , QString () );
576
- QString name = hrefRegEx. cap ( 2 );
582
+ QString name = match. captured ( 2 );
577
583
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 );
580
587
}
581
588
582
589
return converted;
@@ -588,19 +595,18 @@ QString QgsStringUtils::wordWrap( const QString &string, const int length, const
588
595
return string;
589
596
590
597
QString newstr;
591
- QRegExp rx;
598
+ QRegularExpression rx;
592
599
int delimiterLength = 0 ;
593
600
594
601
if ( !customDelimiter.isEmpty () )
595
602
{
596
- rx.setPatternSyntax ( QRegExp::FixedString );
597
- rx.setPattern ( customDelimiter );
603
+ rx.setPattern ( QRegularExpression::escape ( customDelimiter ) );
598
604
delimiterLength = customDelimiter.length ();
599
605
}
600
606
else
601
607
{
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 ]" ) );
604
610
delimiterLength = 1 ;
605
611
}
606
612
@@ -689,8 +695,10 @@ QgsStringReplacement::QgsStringReplacement( const QString &match, const QString
689
695
, mWholeWordOnly( wholeWordOnly )
690
696
{
691
697
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
+ }
694
702
}
695
703
696
704
QString QgsStringReplacement::process ( const QString &input ) const
0 commit comments