make repeatedly used regexps static for some performance boost

This commit is contained in:
Martin Rotter 2023-10-23 08:32:07 +02:00
parent efd2735063
commit 2f5b84cfc7
12 changed files with 47 additions and 22 deletions

View File

@ -76,19 +76,23 @@ Message::Message() {
}
void Message::sanitize(const Feed* feed, bool fix_future_datetimes) {
static QRegularExpression reg_spaces(QString::fromUtf8(QByteArray("[\xE2\x80\xAF]")));
static QRegularExpression reg_whites(QSL("[\\s]{2,}"));
static QRegularExpression reg_news(QSL("([\\n\\r])|(^\\s)"));
// Sanitize title.
m_title = qApp->web()->stripTags(qApp->web()->unescapeHtml(m_title));
m_title = m_title
// Remove non-breaking spaces.
.replace(QRegularExpression(QString::fromUtf8(QByteArray("[\xE2\x80\xAF]"))), QSL(" "))
.replace(reg_spaces, QSL(" "))
// Shrink consecutive whitespaces.
.replace(QRegularExpression(QSL("[\\s]{2,}")), QSL(" "))
.replace(reg_whites, QSL(" "))
// Remove all newlines and leading white space.
.remove(QRegularExpression(QSL("([\\n\\r])|(^\\s)")))
.remove(reg_news)
// Remove non-breaking zero-width spaces.
.remove(QChar(65279));

View File

@ -21,7 +21,7 @@ double TimeSpinBox::valueFromText(const QString& text) const {
return value;
}
else {
QRegularExpression rx(QSL("\\b[0-9]{1,}\\b"));
static QRegularExpression rx(QSL("\\b[0-9]{1,}\\b"));
QStringList numbers;
int pos = 0;
int count = 0;

View File

@ -274,7 +274,9 @@ QList<HttpResponse> Downloader::decodeMultipartAnswer(QNetworkReply* reply) {
QString content_type = reply->header(QNetworkRequest::KnownHeaders::ContentTypeHeader).toString();
QString boundary = content_type.mid(content_type.indexOf(QL1S("boundary=")) + 9);
QRegularExpression regex(QL1S("--") + boundary + QL1S("(--)?(\\r\\n)?"));
static QRegularExpression regex(QL1S("--") + boundary + QL1S("(--)?(\\r\\n)?"));
QStringList list = QString::fromUtf8(data).split(regex,
#if QT_VERSION >= 0x050F00 // Qt >= 5.15.0
Qt::SplitBehaviorFlags::SkipEmptyParts);
@ -289,12 +291,18 @@ QList<HttpResponse> Downloader::decodeMultipartAnswer(QNetworkReply* reply) {
for (const QString& http_response_str : list) {
// We separate headers and body.
HttpResponse new_part;
static QRegularExpression reg_headers(QSL("\\r\\r?\\n"));
static QRegularExpression reg_body(QSL("(\\r\\r?\\n){2,}"));
static QRegularExpression reg_whites(QSL("[\\n\\r]+"));
int start_of_http = http_response_str.indexOf(QL1S("HTTP/1.1"));
int start_of_headers = http_response_str.indexOf(QRegularExpression(QSL("\\r\\r?\\n")), start_of_http);
int start_of_body = http_response_str.indexOf(QRegularExpression(QSL("(\\r\\r?\\n){2,}")), start_of_headers + 2);
int start_of_headers = http_response_str.indexOf(reg_headers, start_of_http);
int start_of_body = http_response_str.indexOf(reg_body, start_of_headers + 2);
QString body = http_response_str.mid(start_of_body);
QString headers = http_response_str.mid(start_of_headers, start_of_body - start_of_headers)
.replace(QRegularExpression(QSL("[\\n\\r]+")), QSL("\n"));
QString headers =
http_response_str.mid(start_of_headers, start_of_body - start_of_headers).replace(reg_whites, QSL("\n"));
auto header_lines = headers.split(QL1C('\n'),
#if QT_VERSION >= 0x050F00 // Qt >= 5.15.0

View File

@ -157,7 +157,9 @@ QString NetworkFactory::networkErrorText(QNetworkReply::NetworkError error_code)
}
QString NetworkFactory::sanitizeUrl(const QString& url) {
return QString(url).replace(QRegularExpression(QSL("[^\\w\\-.~:\\/?#\\[\\]@!$&'()*+,;=% \\|]")), {});
static QRegularExpression reg_non_url(QSL("[^\\w\\-.~:\\/?#\\[\\]@!$&'()*+,;=% \\|]"));
return QString(url).replace(reg_non_url, {});
}
QNetworkReply::NetworkError NetworkFactory::downloadIcon(const QList<IconLocation>& urls,

View File

@ -160,7 +160,9 @@ bool WebFactory::openUrlInExternalBrowser(const QString& url) const {
}
QString WebFactory::stripTags(QString text) {
return text.remove(QRegularExpression(QSL("<[^>]*>")));
static QRegularExpression reg_tags(QSL("<[^>]*>"));
return text.remove(reg_tags);
}
QString WebFactory::unescapeHtml(const QString& html) {

View File

@ -20,7 +20,6 @@
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QRegularExpression>
#include <QUrl>
GmailNetworkFactory::GmailNetworkFactory(QObject* parent)

View File

@ -923,7 +923,9 @@ QString GreaderNetwork::convertShortStreamIdToLongStreamId(const QString& stream
}
QString GreaderNetwork::simplifyStreamId(const QString& stream_id) const {
return QString(stream_id).replace(QRegularExpression(QSL("\\/\\d+\\/")), QSL("/-/"));
static QRegularExpression reg(QSL("\\/\\d+\\/"));
return QString(stream_id).replace(reg, QSL("/-/"));
}
QStringList GreaderNetwork::decodeItemIds(const QString& stream_json_data, QString& continuation) {

View File

@ -68,8 +68,9 @@ QList<StandardFeed*> AtomParser::discoverFeeds(ServiceRoot* root, const QUrl& ur
IOFactory::writeFile("aaaa", data);
// 2.
QRegularExpression rx(QSL(ATOM_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
QRegularExpression rx_href(QSL(ATOM_HREF_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx(QSL(ATOM_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx_href(QSL(ATOM_HREF_REGEX_MATCHER),
QRegularExpression::PatternOption::CaseInsensitiveOption);
rx_href.optimize();
@ -208,6 +209,8 @@ QList<StandardFeed*> AtomParser::discoverFeeds(ServiceRoot* root, const QUrl& ur
QPair<StandardFeed*, QList<IconLocation>> AtomParser::guessFeed(const QByteArray& content,
const QString& content_type) const {
Q_UNUSED(content_type)
QString xml_schema_encoding = QSL(DEFAULT_FEED_ENCODING);
QString xml_contents_encoded;
QString enc =

View File

@ -195,7 +195,9 @@ QList<Message> FeedParser::messages() {
}
// Url.
new_message.m_url = new_message.m_url.replace(QRegularExpression(QSL("[\\t\\n]")), QString());
static QRegularExpression reg_non_url(QSL("[\\t\\n]"));
new_message.m_url = new_message.m_url.replace(reg_non_url, {});
}
return messages;

View File

@ -54,8 +54,9 @@ QList<StandardFeed*> JsonParser::discoverFeeds(ServiceRoot* root, const QUrl& ur
}
// 2.
QRegularExpression rx(QSL(JSON_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
QRegularExpression rx_href(QSL(JSON_HREF_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx(QSL(JSON_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx_href(QSL(JSON_HREF_REGEX_MATCHER),
QRegularExpression::PatternOption::CaseInsensitiveOption);
rx_href.optimize();

View File

@ -55,8 +55,9 @@ QList<StandardFeed*> RdfParser::discoverFeeds(ServiceRoot* root, const QUrl& url
}
// 2.
QRegularExpression rx(QSL(RSS_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
QRegularExpression rx_href(QSL(RSS_HREF_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx(QSL(RSS_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx_href(QSL(RSS_HREF_REGEX_MATCHER),
QRegularExpression::PatternOption::CaseInsensitiveOption);
rx_href.optimize();

View File

@ -55,8 +55,9 @@ QList<StandardFeed*> RssParser::discoverFeeds(ServiceRoot* root, const QUrl& url
}
// 2.
QRegularExpression rx(QSL(RSS_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
QRegularExpression rx_href(QSL(RSS_HREF_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx(QSL(RSS_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption);
static QRegularExpression rx_href(QSL(RSS_HREF_REGEX_MATCHER),
QRegularExpression::PatternOption::CaseInsensitiveOption);
rx_href.optimize();