From bed43ed0c804719f7c42da285b60d415eff11b07 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sun, 24 Feb 2013 10:57:58 +0100 Subject: [PATCH] [Qt5] Use RegExp wrapper to take advantage of new regexp engine. QzRegExp wrapper is using QRegExp directly on Qt 4 and QRegularExpression (with PCRE engine) on Qt 5. --- src/lib/adblock/adblockrule.cpp | 32 ++++---- src/lib/adblock/adblockrule.h | 4 +- src/lib/app/mainapplication.cpp | 3 +- src/lib/app/qupzilla.cpp | 2 +- src/lib/autofill/pageformcompleter.cpp | 3 +- src/lib/bookmarksimport/chromeimporter.cpp | 6 +- src/lib/bookmarksimport/htmlimporter.cpp | 10 +-- src/lib/bookmarksimport/operaimporter.cpp | 7 +- src/lib/downloads/downloadfilehelper.cpp | 11 +-- src/lib/lib.pro | 15 ++-- src/lib/opensearch/opensearchengine.cpp | 6 +- src/lib/other/sourceviewer.cpp | 2 +- src/lib/preferences/thememanager.cpp | 5 +- src/lib/tools/htmlhighlighter.cpp | 14 ++-- src/lib/tools/htmlhighlighter.h | 9 ++- src/lib/tools/iconfetcher.cpp | 10 ++- src/lib/tools/qzregexp.cpp | 87 ++++++++++++++++++++++ src/lib/tools/qzregexp.h | 52 +++++++++++++ src/plugins/GreaseMonkey/gm_downloader.cpp | 6 +- src/plugins/GreaseMonkey/gm_script.cpp | 4 +- src/plugins/GreaseMonkey/gm_urlmatcher.cpp | 14 ++-- src/plugins/GreaseMonkey/gm_urlmatcher.h | 6 +- 22 files changed, 229 insertions(+), 79 deletions(-) create mode 100644 src/lib/tools/qzregexp.cpp create mode 100644 src/lib/tools/qzregexp.h diff --git a/src/lib/adblock/adblockrule.cpp b/src/lib/adblock/adblockrule.cpp index b92519553..95bb7d346 100644 --- a/src/lib/adblock/adblockrule.cpp +++ b/src/lib/adblock/adblockrule.cpp @@ -48,7 +48,7 @@ #include "adblocksubscription.h" #include -#include +#include "qzregexp.h" #include #include #include @@ -467,7 +467,7 @@ void AdBlockRule::parseFilter() parsedLine = parsedLine.left(parsedLine.size() - 1); m_useRegExp = true; - m_regExp = QRegExp(parsedLine, m_caseSensitivity, QRegExp::RegExp); + m_regExp = QzRegExp(parsedLine, m_caseSensitivity); return; } @@ -482,7 +482,7 @@ void AdBlockRule::parseFilter() // We can use fast string matching for domain here if (parsedLine.startsWith(QLatin1String("||")) && parsedLine.endsWith(QLatin1Char('^')) - && !parsedLine.contains(QRegExp("[/:?=&\\*]"))) { + && !parsedLine.contains(QzRegExp("[/:?=&\\*]"))) { parsedLine = parsedLine.mid(2); parsedLine = parsedLine.left(parsedLine.size() - 1); @@ -492,7 +492,7 @@ void AdBlockRule::parseFilter() } // If rule contains only | at end, we can also use string matching - if (parsedLine.endsWith(QLatin1Char('|')) && !parsedLine.contains(QRegExp("[\\^\\*]")) + if (parsedLine.endsWith(QLatin1Char('|')) && !parsedLine.contains(QzRegExp("[\\^\\*]")) && parsedLine.count(QLatin1Char('|')) == 1) { parsedLine = parsedLine.left(parsedLine.size() - 1); @@ -502,24 +502,24 @@ void AdBlockRule::parseFilter() } // If we still find a wildcard (*) or separator (^) or (|) - // we must modify parsedLine to comply with QRegExp + // we must modify parsedLine to comply with QzRegExp if (parsedLine.contains(QLatin1Char('*')) || parsedLine.contains(QLatin1Char('^')) || parsedLine.contains(QLatin1Char('|'))) { - parsedLine.replace(QRegExp(QLatin1String("\\*+")), QLatin1String("*")) // remove multiple wildcards - .replace(QRegExp(QLatin1String("\\^\\|$")), QLatin1String("^")) // remove anchors following separator placeholder - .replace(QRegExp(QLatin1String("^(\\*)")), QString()) // remove leading wildcards - .replace(QRegExp(QLatin1String("(\\*)$")), QString()) - .replace(QRegExp(QLatin1String("(\\W)")), QLatin1String("\\\\1")) // escape special symbols - .replace(QRegExp(QLatin1String("^\\\\\\|\\\\\\|")), + parsedLine.replace(QzRegExp(QLatin1String("\\*+")), QLatin1String("*")) // remove multiple wildcards + .replace(QzRegExp(QLatin1String("\\^\\|$")), QLatin1String("^")) // remove anchors following separator placeholder + .replace(QzRegExp(QLatin1String("^(\\*)")), QString()) // remove leading wildcards + .replace(QzRegExp(QLatin1String("(\\*)$")), QString()) + .replace(QzRegExp(QLatin1String("(\\W)")), QLatin1String("\\\\1")) // escape special symbols + .replace(QzRegExp(QLatin1String("^\\\\\\|\\\\\\|")), QLatin1String("^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?")) // process extended anchor at expression start - .replace(QRegExp(QLatin1String("\\\\\\^")), + .replace(QzRegExp(QLatin1String("\\\\\\^")), QLatin1String("(?:[^\\w\\d\\-.%]|$)")) // process separator placeholders - .replace(QRegExp(QLatin1String("^\\\\\\|")), QLatin1String("^")) // process anchor at expression start - .replace(QRegExp(QLatin1String("\\\\\\|$")), QLatin1String("$")) // process anchor at expression end - .replace(QRegExp(QLatin1String("\\\\\\*")), QLatin1String(".*")); // replace wildcards by .* + .replace(QzRegExp(QLatin1String("^\\\\\\|")), QLatin1String("^")) // process anchor at expression start + .replace(QzRegExp(QLatin1String("\\\\\\|$")), QLatin1String("$")) // process anchor at expression end + .replace(QzRegExp(QLatin1String("\\\\\\*")), QLatin1String(".*")); // replace wildcards by .* m_useRegExp = true; - m_regExp = QRegExp(parsedLine, m_caseSensitivity, QRegExp::RegExp); + m_regExp = QzRegExp(parsedLine, m_caseSensitivity); return; } diff --git a/src/lib/adblock/adblockrule.h b/src/lib/adblock/adblockrule.h index 9d09f99d0..5038a15e1 100644 --- a/src/lib/adblock/adblockrule.h +++ b/src/lib/adblock/adblockrule.h @@ -47,10 +47,10 @@ #define ADBLOCKRULE_H #include -#include #include #include "qz_namespace.h" +#include "qzregexp.h" class QNetworkRequest; class QUrl; @@ -111,7 +111,7 @@ private: bool m_domainRestricted; bool m_useRegExp; - QRegExp m_regExp; + QzRegExp m_regExp; bool m_useDomainMatch; bool m_useEndsMatch; diff --git a/src/lib/app/mainapplication.cpp b/src/lib/app/mainapplication.cpp index 946e3948c..e94c87fa4 100644 --- a/src/lib/app/mainapplication.cpp +++ b/src/lib/app/mainapplication.cpp @@ -50,6 +50,7 @@ #include "useragentmanager.h" #include "restoremanager.h" #include "proxystyle.h" +#include "qzregexp.h" #include "checkboxdialog.h" #include "registerqappassociation.h" #include "html5permissions/html5permissionsmanager.h" @@ -375,7 +376,7 @@ void MainApplication::loadSettings() } QString relativePath = QDir::current().relativeFilePath(m_activeThemePath); - css.replace(QRegExp("url\\s*\\(\\s*([^\\*:\\);]+)\\s*\\)", Qt::CaseSensitive, QRegExp::RegExp2), + css.replace(QzRegExp("url\\s*\\(\\s*([^\\*:\\);]+)\\s*\\)", Qt::CaseSensitive), QString("url(%1\\1)").arg(relativePath + "/")); setStyleSheet(css); diff --git a/src/lib/app/qupzilla.cpp b/src/lib/app/qupzilla.cpp index 942b039df..71e09e790 100644 --- a/src/lib/app/qupzilla.cpp +++ b/src/lib/app/qupzilla.cpp @@ -1732,7 +1732,7 @@ void QupZilla::hideNavigationWithFullScreen() void QupZilla::hideNavigationSlot() { TabbedWebView* view = weView(); - bool mouseInView = view && view->geometry().contains(view->mapFromGlobal(QCursor::pos())); + bool mouseInView = view && view->underMouse(); if (isFullScreen() && mouseInView) { m_navigationContainer->hide(); diff --git a/src/lib/autofill/pageformcompleter.cpp b/src/lib/autofill/pageformcompleter.cpp index 210d27eba..ebd3fafbc 100644 --- a/src/lib/autofill/pageformcompleter.cpp +++ b/src/lib/autofill/pageformcompleter.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . * ============================================================ */ #include "pageformcompleter.h" +#include "qzregexp.h" #include #include @@ -160,7 +161,7 @@ QByteArray PageFormCompleter::convertWebKitFormBoundaryIfNecessary(const QByteAr } QByteArray formatedData; - QRegExp rx("name=\"(.*)------WebKitFormBoundary"); + QzRegExp rx("name=\"(.*)------WebKitFormBoundary"); rx.setMinimal(true); int pos = 0; diff --git a/src/lib/bookmarksimport/chromeimporter.cpp b/src/lib/bookmarksimport/chromeimporter.cpp index 8cf217697..c9d940e7a 100644 --- a/src/lib/bookmarksimport/chromeimporter.cpp +++ b/src/lib/bookmarksimport/chromeimporter.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #include #include #include -#include +#include "qzregexp.h" ChromeImporter::ChromeImporter(QObject* parent) : QObject(parent) @@ -57,7 +57,7 @@ QList ChromeImporter::exportBookmarks() m_file.close(); QStringList parsedBookmarks; - QRegExp rx("\\{(\\s*)\"date_added(.*)\"(\\s*)\\}", Qt::CaseSensitive); + QzRegExp rx("\\{(\\s*)\"date_added(.*)\"(\\s*)\\}", Qt::CaseSensitive); rx.setMinimal(true); int pos = 0; diff --git a/src/lib/bookmarksimport/htmlimporter.cpp b/src/lib/bookmarksimport/htmlimporter.cpp index 4e000a9c9..ebf30a2a6 100644 --- a/src/lib/bookmarksimport/htmlimporter.cpp +++ b/src/lib/bookmarksimport/htmlimporter.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,7 +18,7 @@ #include "htmlimporter.h" #include "bookmarksimportdialog.h" -#include +#include "qzregexp.h" HtmlImporter::HtmlImporter(QObject* parent) : QObject(parent) @@ -100,7 +100,7 @@ QList HtmlImporter::exportBookmarks() if (nearest == posOfFolder) { // Next is folder - QRegExp rx("
(.*)"); + QzRegExp rx("
(.*)"); rx.setMinimal(true); rx.indexIn(string); @@ -121,14 +121,14 @@ QList HtmlImporter::exportBookmarks() } else { // Next is link - QRegExp rx("
(.*)"); + QzRegExp rx("
(.*)"); rx.setMinimal(true); rx.indexIn(string); QString arguments = rx.cap(1); QString linkName = rx.cap(2).trimmed(); - QRegExp rx2("href=\"(.*)\""); + QzRegExp rx2("href=\"(.*)\""); rx2.setMinimal(true); rx2.indexIn(arguments); diff --git a/src/lib/bookmarksimport/operaimporter.cpp b/src/lib/bookmarksimport/operaimporter.cpp index 5b03d01fa..4c7f16b20 100644 --- a/src/lib/bookmarksimport/operaimporter.cpp +++ b/src/lib/bookmarksimport/operaimporter.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,6 +17,7 @@ * ============================================================ */ #include "operaimporter.h" #include "bookmarksimportdialog.h" +#include "qzregexp.h" OperaImporter::OperaImporter(QObject* parent) : QObject(parent) @@ -50,7 +51,7 @@ QList OperaImporter::exportBookmarks() QString bookmarks = QString::fromUtf8(m_file.readAll()); m_file.close(); - QRegExp rx("#URL(.*)CREATED", Qt::CaseSensitive); + QzRegExp rx("#URL(.*)CREATED", Qt::CaseSensitive); rx.setMinimal(true); int pos = 0; @@ -58,7 +59,7 @@ QList OperaImporter::exportBookmarks() QString string = rx.cap(1); pos += rx.matchedLength(); - QRegExp rx2("NAME=(.*)\\n"); + QzRegExp rx2("NAME=(.*)\\n"); rx2.setMinimal(true); rx2.indexIn(string); QString name = rx2.cap(1).trimmed(); diff --git a/src/lib/downloads/downloadfilehelper.cpp b/src/lib/downloads/downloadfilehelper.cpp index 79e03d60e..b12790fc4 100644 --- a/src/lib/downloads/downloadfilehelper.cpp +++ b/src/lib/downloads/downloadfilehelper.cpp @@ -25,6 +25,7 @@ #include "downloadmanager.h" #include "qztools.h" #include "settings.h" +#include "qzregexp.h" #include #include @@ -129,13 +130,13 @@ QString DownloadFileHelper::parseContentDisposition(const QByteArray &header) } // We try to use UTF-8 encoded filename first if present - if (value.contains(QRegExp("[ ;]{1,}filename*\\*\\s*=\\s*UTF-8''", Qt::CaseInsensitive))) { - QRegExp reg("filename\\s*\\*\\s*=\\s*UTF-8''([^;]*)", Qt::CaseInsensitive); + if (value.contains(QzRegExp("[ ;]{1,}filename*\\*\\s*=\\s*UTF-8''", Qt::CaseInsensitive))) { + QzRegExp reg("filename\\s*\\*\\s*=\\s*UTF-8''([^;]*)", Qt::CaseInsensitive); reg.indexIn(value); path = QUrl::fromPercentEncoding(reg.cap(1).toUtf8()).trimmed(); } - else if (value.contains(QRegExp("[ ;]{1,}filename\\s*=", Qt::CaseInsensitive))) { - QRegExp reg("[ ;]{1,}filename\\s*=(.*)", Qt::CaseInsensitive); + else if (value.contains(QzRegExp("[ ;]{1,}filename\\s*=", Qt::CaseInsensitive))) { + QzRegExp reg("[ ;]{1,}filename\\s*=(.*)", Qt::CaseInsensitive); reg.indexIn(value); path = reg.cap(1).trimmed(); @@ -152,7 +153,7 @@ QString DownloadFileHelper::parseContentDisposition(const QByteArray &header) } } else { - QRegExp reg("([^;]*)", Qt::CaseInsensitive); + QzRegExp reg("([^;]*)", Qt::CaseInsensitive); reg.indexIn(path); path = reg.cap(1).trimmed(); } diff --git a/src/lib/lib.pro b/src/lib/lib.pro index d1584f371..e509727e5 100644 --- a/src/lib/lib.pro +++ b/src/lib/lib.pro @@ -14,10 +14,6 @@ include(../defines.pri) include(../../translations/translations.pri) #include(../../tests/modeltest/modeltest.pri) -isEqual(QT_MAJOR_VERSION, 5) { - include(3rdparty/qftp.pri) -} - contains(DEFINES, USE_QTWEBKIT_2_2) { include(plugins/qtwebkit/qtwebkit-plugins.pri) } @@ -215,7 +211,7 @@ SOURCES += \ tools/menubar.cpp \ navigation/navigationcontainer.cpp \ tools/horizontallistwidget.cpp \ - tools/mactoolbutton.cpp + tools/mactoolbutton.cpp \ HEADERS += \ webview/tabpreview.h \ @@ -385,7 +381,8 @@ HEADERS += \ tools/menubar.h \ navigation/navigationcontainer.h \ tools/horizontallistwidget.h \ - tools/mactoolbutton.h + tools/mactoolbutton.h \ + tools/qzregexp.h FORMS += \ preferences/autofillmanager.ui \ @@ -441,6 +438,12 @@ RESOURCES += \ data/html.qrc \ data/data.qrc +isEqual(QT_MAJOR_VERSION, 5) { + include(3rdparty/qftp.pri) + + SOURCES += tools/qzregexp.cpp +} + !mac:unix { target.path = $$library_folder diff --git a/src/lib/opensearch/opensearchengine.cpp b/src/lib/opensearch/opensearchengine.cpp index bb144a26e..edb02962f 100644 --- a/src/lib/opensearch/opensearchengine.cpp +++ b/src/lib/opensearch/opensearchengine.cpp @@ -18,7 +18,7 @@ */ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -35,7 +35,7 @@ * ============================================================ */ #include "opensearchengine.h" - +#include "qzregexp.h" #include "opensearchenginedelegate.h" #include @@ -139,7 +139,7 @@ QString OpenSearchEngine::parseTemplate(const QString &searchTerm, const QString result.replace(QLatin1String("{language}"), language); result.replace(QLatin1String("{inputEncoding}"), QLatin1String("UTF-8")); result.replace(QLatin1String("{outputEncoding}"), QLatin1String("UTF-8")); - result.replace(QRegExp(QLatin1String("\\{([^\\}]*:|)source\\??\\}")), QCoreApplication::applicationName()); + result.replace(QzRegExp(QLatin1String("\\{([^\\}]*:|)source\\??\\}")), QCoreApplication::applicationName()); result.replace(QLatin1String("{searchTerms}"), QLatin1String(QUrl::toPercentEncoding(searchTerm))); return result; diff --git a/src/lib/other/sourceviewer.cpp b/src/lib/other/sourceviewer.cpp index 970bb786a..6bf87504e 100644 --- a/src/lib/other/sourceviewer.cpp +++ b/src/lib/other/sourceviewer.cpp @@ -152,7 +152,7 @@ void SourceViewer::loadSource() QString html = m_frame.data()->toHtml(); // Remove AdBlock element hiding rules - html.remove(QRegExp("")); + html.remove(QzRegExp("")); m_sourceEdit->setPlainText(html); // Highlight selectedHtml diff --git a/src/lib/preferences/thememanager.cpp b/src/lib/preferences/thememanager.cpp index 14ef9cf89..5b4f564cc 100644 --- a/src/lib/preferences/thememanager.cpp +++ b/src/lib/preferences/thememanager.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,7 @@ #include "settings.h" #include "licenseviewer.h" #include "preferences.h" +#include "qzregexp.h" #include #include @@ -119,7 +120,7 @@ ThemeManager::Theme ThemeManager::parseTheme(const QString &name) QString theme_info = QzTools::readAllFileContents(path + "theme.info"); - QRegExp rx("Name:(.*)\\n"); + QzRegExp rx("Name:(.*)\\n"); rx.setMinimal(true); rx.indexIn(theme_info); if (rx.captureCount() == 1) { diff --git a/src/lib/tools/htmlhighlighter.cpp b/src/lib/tools/htmlhighlighter.cpp index e203fbf0d..8daabfde8 100644 --- a/src/lib/tools/htmlhighlighter.cpp +++ b/src/lib/tools/htmlhighlighter.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -67,7 +67,7 @@ HtmlHighlighter::HtmlHighlighter(QTextDocument* parent) QStringList keywordPatterns; keywordPatterns << "| )?" << ">" << "(| tagOptionsFormat.setForeground(Qt::black); tagOptionsFormat.setFontWeight(QFont::Bold); - rule.pattern = QRegExp("(\\S{2,20})=\""); + rule.pattern = QzRegExp("(\\S{2,20})=\""); rule.format = tagOptionsFormat; highlightingRules.append(rule); // " " strings quotationFormat.setForeground(Qt::darkGreen); - QRegExp rx("\".*\""); + QzRegExp rx("\".*\""); rx.setMinimal(true); rule.pattern = rx; rule.format = quotationFormat; @@ -89,14 +89,14 @@ HtmlHighlighter::HtmlHighlighter(QTextDocument* parent) // comments multiLineCommentFormat.setForeground(Qt::gray); - commentStartExpression = QRegExp(""); + commentStartExpression = QzRegExp(""); } void HtmlHighlighter::highlightBlock(const QString &text) { foreach(const HighlightingRule & rule, highlightingRules) { - QRegExp expression(rule.pattern); + QzRegExp expression(rule.pattern); int index = expression.indexIn(text); while (index >= 0) { int length = expression.matchedLength(); diff --git a/src/lib/tools/htmlhighlighter.h b/src/lib/tools/htmlhighlighter.h index dbd758693..b1e3d27d5 100644 --- a/src/lib/tools/htmlhighlighter.h +++ b/src/lib/tools/htmlhighlighter.h @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -62,6 +62,7 @@ #include #include "qz_namespace.h" +#include "qzregexp.h" class QTextDocument; @@ -75,14 +76,14 @@ protected: private: struct HighlightingRule { - QRegExp pattern; + QzRegExp pattern; QTextCharFormat format; }; QVector highlightingRules; - QRegExp commentStartExpression; - QRegExp commentEndExpression; + QzRegExp commentStartExpression; + QzRegExp commentEndExpression; QTextCharFormat tagFormat; QTextCharFormat tagOptionsFormat; diff --git a/src/lib/tools/iconfetcher.cpp b/src/lib/tools/iconfetcher.cpp index 229e5f204..0c7bd2ece 100644 --- a/src/lib/tools/iconfetcher.cpp +++ b/src/lib/tools/iconfetcher.cpp @@ -1,6 +1,6 @@ /* ============================================================ * QupZilla - WebKit based browser -* Copyright (C) 2010-2012 David Rosca +* Copyright (C) 2010-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -15,9 +15,11 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . * ============================================================ */ -#include #include "iconfetcher.h" #include "followredirectreply.h" +#include "qzregexp.h" + +#include IconFetcher::IconFetcher(QObject* parent) : QObject(parent) @@ -48,7 +50,7 @@ void IconFetcher::pageDownloaded() QUrl replyUrl = reply->url(); reply->deleteLater(); - QRegExp rx("", Qt::CaseInsensitive); + QzRegExp rx("", Qt::CaseInsensitive); rx.setMinimal(true); QString shortcutIconTag; @@ -72,7 +74,7 @@ void IconFetcher::pageDownloaded() newReply = new FollowRedirectReply(faviconUrl, m_manager); } else { - QRegExp rx("href=\"(.*)\"", Qt::CaseInsensitive); + QzRegExp rx("href=\"(.*)\"", Qt::CaseInsensitive); rx.setMinimal(true); rx.indexIn(shortcutIconTag); QUrl url = QUrl(rx.cap(1)); diff --git a/src/lib/tools/qzregexp.cpp b/src/lib/tools/qzregexp.cpp new file mode 100644 index 000000000..8418b38e3 --- /dev/null +++ b/src/lib/tools/qzregexp.cpp @@ -0,0 +1,87 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#include "qzregexp.h" +#include "qztools.h" + +#if (QT_VERSION >= 0x050000) +QzRegExp::QzRegExp() + : QRegularExpression(QString(), QRegularExpression::DotMatchesEverythingOption) + , m_matchedLength(-1) +{ +} + +QzRegExp::QzRegExp(const QString &pattern, Qt::CaseSensitivity cs) + : QRegularExpression(pattern, QRegularExpression::DotMatchesEverythingOption) + , m_matchedLength(-1) +{ + if (cs == Qt::CaseInsensitive) { + setPatternOptions(patternOptions() | QRegularExpression::CaseInsensitiveOption); + } +} + +QzRegExp::QzRegExp(const QzRegExp &re) + : QRegularExpression(re) + , m_matchedLength(-1) +{ +} + +void QzRegExp::setMinimal(bool minimal) +{ + QRegularExpression::PatternOptions opt; + + if (minimal) { + opt = patternOptions() | QRegularExpression::InvertedGreedinessOption; + } + else { + opt = patternOptions() & ~QRegularExpression::InvertedGreedinessOption; + } + + setPatternOptions(opt); +} + +int QzRegExp::indexIn(const QString &str, int offset) const +{ + QzRegExp* that = const_cast(this); + QRegularExpressionMatch m = match(str, offset); + + if (!m.hasMatch()) { + that->m_matchedLength = -1; + that->m_capturedTexts.clear(); + return -1; + } + + that->m_matchedLength = m.capturedLength(); + that->m_capturedTexts = m.capturedTexts(); + return m.capturedStart(); +} + +int QzRegExp::matchedLength() const +{ + return m_matchedLength; +} + +QString QzRegExp::cap(int nth) const +{ + if (!QzTools::listContainsIndex(m_capturedTexts, nth)) { + return QString(); + } + + return m_capturedTexts.at(nth); +} +#endif // (QT_VERSION >= 0x050000) + diff --git a/src/lib/tools/qzregexp.h b/src/lib/tools/qzregexp.h new file mode 100644 index 000000000..a9f47f561 --- /dev/null +++ b/src/lib/tools/qzregexp.h @@ -0,0 +1,52 @@ +/* ============================================================ +* QupZilla - WebKit based browser +* Copyright (C) 2013 David Rosca +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +* ============================================================ */ +#ifndef QZREGEXP_H +#define QZREGEXP_H + +#include // Needed for QT_VERSION + +#if (QT_VERSION < 0x050000) +// Qt 4 - use QRegExp directly +#include +#define QzRegExp QRegExp +#else // Qt 5 +#include +#include + +#include "qz_namespace.h" + +class QT_QUPZILLA_EXPORT QzRegExp : public QRegularExpression +{ +public: + QzRegExp(); + QzRegExp(const QString &pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive); + QzRegExp(const QzRegExp &re); + + void setMinimal(bool minimal); + int indexIn(const QString &str, int offset = 0) const; + int matchedLength() const; + QString cap(int nth = 0) const; + +private: + QStringList m_capturedTexts; + int m_matchedLength; + +}; +#endif // Qt 5 + +#endif // QZREGEXP_H diff --git a/src/plugins/GreaseMonkey/gm_downloader.cpp b/src/plugins/GreaseMonkey/gm_downloader.cpp index 26e749bf6..918b9da18 100644 --- a/src/plugins/GreaseMonkey/gm_downloader.cpp +++ b/src/plugins/GreaseMonkey/gm_downloader.cpp @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2012 David Rosca +* Copyright (C) 2012-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,7 +28,7 @@ #include #include -#include +#include "qzregexp.h" #include GM_Downloader::GM_Downloader(const QNetworkRequest &request, GM_Manager* manager) @@ -72,7 +72,7 @@ void GM_Downloader::scriptDownloaded() QSettings settings(m_manager->settinsPath() + "greasemonkey/requires/requires.ini", QSettings::IniFormat); settings.beginGroup("Files"); - QRegExp rx("@require(.*)\\n"); + QzRegExp rx("@require(.*)\\n"); rx.setMinimal(true); rx.indexIn(response); diff --git a/src/plugins/GreaseMonkey/gm_script.cpp b/src/plugins/GreaseMonkey/gm_script.cpp index a2a876f1c..f0a81f1e3 100644 --- a/src/plugins/GreaseMonkey/gm_script.cpp +++ b/src/plugins/GreaseMonkey/gm_script.cpp @@ -19,7 +19,7 @@ #include "gm_manager.h" #include -#include +#include "qzregexp.h" #include #include #include @@ -176,7 +176,7 @@ void GM_Script::parseScript() QString fileData = QString::fromUtf8(file.readAll()); - QRegExp rx("// ==UserScript==(.*)// ==/UserScript=="); + QzRegExp rx("// ==UserScript==(.*)// ==/UserScript=="); rx.indexIn(fileData); QString metadataBlock = rx.cap(1).trimmed(); diff --git a/src/plugins/GreaseMonkey/gm_urlmatcher.cpp b/src/plugins/GreaseMonkey/gm_urlmatcher.cpp index 507d032c4..515a50315 100644 --- a/src/plugins/GreaseMonkey/gm_urlmatcher.cpp +++ b/src/plugins/GreaseMonkey/gm_urlmatcher.cpp @@ -80,22 +80,22 @@ void GM_UrlMatcher::parsePattern(QString pattern) pattern = pattern.mid(1); pattern = pattern.left(pattern.size() - 1); - m_regExp = QRegExp(pattern, Qt::CaseInsensitive); + m_regExp = QzRegExp(pattern, Qt::CaseInsensitive); m_useRegExp = true; return; } if (pattern.contains(QLatin1String(".tld"))) { - pattern.replace(QRegExp("(\\W)"), QLatin1String("\\\\1")) - .replace(QRegExp("\\*+"), QLatin1String("*")) - .replace(QRegExp("^\\\\\\|"), QLatin1String("^")) - .replace(QRegExp("\\\\\\|$"), QLatin1String("$")) - .replace(QRegExp("\\\\\\*"), QLatin1String(".*")) + pattern.replace(QzRegExp("(\\W)"), QLatin1String("\\\\1")) + .replace(QzRegExp("\\*+"), QLatin1String("*")) + .replace(QzRegExp("^\\\\\\|"), QLatin1String("^")) + .replace(QzRegExp("\\\\\\|$"), QLatin1String("$")) + .replace(QzRegExp("\\\\\\*"), QLatin1String(".*")) .replace(QLatin1String("\\.tld"), QLatin1String("\\.[a-z.]{2,6}")); m_useRegExp = true; - m_regExp = QRegExp(pattern, Qt::CaseInsensitive); + m_regExp = QzRegExp(pattern, Qt::CaseInsensitive); } else { m_matchString = pattern; diff --git a/src/plugins/GreaseMonkey/gm_urlmatcher.h b/src/plugins/GreaseMonkey/gm_urlmatcher.h index 6d45d422d..a0ca35e29 100644 --- a/src/plugins/GreaseMonkey/gm_urlmatcher.h +++ b/src/plugins/GreaseMonkey/gm_urlmatcher.h @@ -1,6 +1,6 @@ /* ============================================================ * GreaseMonkey plugin for QupZilla -* Copyright (C) 2012 David Rosca +* Copyright (C) 2012-2013 David Rosca * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ #define GM_URLMATCHER_H #include -#include +#include "qzregexp.h" class GM_UrlMatcher { @@ -36,7 +36,7 @@ private: QString m_pattern; QString m_matchString; - QRegExp m_regExp; + QzRegExp m_regExp; bool m_useRegExp; };