From 3ef35daec3fa7be0597255102e5db2288023e809 Mon Sep 17 00:00:00 2001
From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com>
Date: Wed, 24 Jul 2024 16:54:34 +0800
Subject: [PATCH] opt: use static regex (#1706)
* opt: use static regex
* [autofix.ci] apply automated fixes
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
---
src/dict/stardict.cc | 35 +++++++++++++++++++----------------
1 file changed, 19 insertions(+), 16 deletions(-)
diff --git a/src/dict/stardict.cc b/src/dict/stardict.cc
index cbc51292..4aaa9623 100644
--- a/src/dict/stardict.cc
+++ b/src/dict/stardict.cc
@@ -422,10 +422,12 @@ private:
}
old = s;
}
- s.replace( QRegularExpression( "&.\\s*\\{",
- QRegularExpression::UseUnicodePropertiesOption
- | QRegularExpression::DotMatchesEverythingOption ),
- "" );
+
+ static QRegularExpression leadingBrace( "&.\\s*\\{",
+ QRegularExpression::UseUnicodePropertiesOption
+ | QRegularExpression::DotMatchesEverythingOption );
+
+ s.replace( leadingBrace, "" );
s.replace( "}", "" );
}
@@ -448,18 +450,18 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
{
QString articleText = QString( "
" ) + QString::fromUtf8( resource, size ) + "
";
- QRegularExpression imgRe( R"((<\s*(?:img|script)\s+[^>]*src\s*=\s*["']?)(?!(?:data|https?|ftp):))",
- QRegularExpression::CaseInsensitiveOption );
- QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']?)(?!(?:data|https?|ftp):))",
- QRegularExpression::CaseInsensitiveOption );
+ static QRegularExpression imgRe( R"((<\s*(?:img|script)\s+[^>]*src\s*=\s*["']?)(?!(?:data|https?|ftp):))",
+ QRegularExpression::CaseInsensitiveOption );
+ static QRegularExpression linkRe( R"((<\s*link\s+[^>]*href\s*=\s*["']?)(?!(?:data|https?|ftp):))",
+ QRegularExpression::CaseInsensitiveOption );
articleText.replace( imgRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" )
.replace( linkRe, "\\1bres://" + QString::fromStdString( getId() ) + "/" );
// Handle links to articles
- QRegularExpression linksReg( R"(]*)href\s*=\s*['"](bword://)?([^'"]+)['"])",
- QRegularExpression::CaseInsensitiveOption );
+ static QRegularExpression linksReg( R"(]*)href\s*=\s*['"](bword://)?([^'"]+)['"])",
+ QRegularExpression::CaseInsensitiveOption );
int pos = 0;
@@ -508,9 +510,9 @@ string StardictDictionary::handleResource( char type, char const * resource, siz
// Handle "audio" tags
- QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*))",
- QRegularExpression::CaseInsensitiveOption
- | QRegularExpression::DotMatchesEverythingOption );
+ static QRegularExpression audioRe( R"(<\s*audio\s*src\s*=\s*(["']+)([^"']+)(["'])\s*>(.*))",
+ QRegularExpression::CaseInsensitiveOption
+ | QRegularExpression::DotMatchesEverythingOption );
pos = 0;
@@ -614,8 +616,8 @@ void StardictDictionary::pangoToHtml( QString & text )
* Attributes "fallback", "lang", "gravity", "gravity_hint" just ignored
*/
- QRegularExpression spanRegex( "]*)>", QRegularExpression::CaseInsensitiveOption );
- QRegularExpression styleRegex( "(\\w+)=\"([^\"]*)\"" );
+ static QRegularExpression spanRegex( "]*)>", QRegularExpression::CaseInsensitiveOption );
+ static QRegularExpression styleRegex( "(\\w+)=\"([^\"]*)\"" );
text.replace( "\n", "
" );
@@ -1554,7 +1556,8 @@ void StardictResourceRequest::run()
QString id = QString::fromUtf8( dict.getId().c_str() );
int pos = 0;
- QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))", QRegularExpression::CaseInsensitiveOption );
+ static QRegularExpression links( R"(url\(\s*(['"]?)([^'"]*)(['"]?)\s*\))",
+ QRegularExpression::CaseInsensitiveOption );
QString newCSS;
QRegularExpressionMatchIterator it = links.globalMatch( css );