Commit 4a4456ab authored by Ahmad Samir's avatar Ahmad Samir Committed by Albert Astals Cid
Browse files

Port QRegExp to QRegularExpression in a couple of locations

Some instances of QRegExp are still left:
generators/mobipocket and generators/epub
parent 2da1cd73
Pipeline #13891 passed with stage
in 10 minutes and 22 seconds
......@@ -44,6 +44,7 @@
#include <QMimeDatabase>
#include <QDesktopServices>
#include <QPageSize>
#include <QRegularExpression>
#include <QStandardPaths>
#include <kauthorized.h>
......@@ -2139,11 +2140,15 @@ void DocumentPrivate::loadSyncFile( const QString & filePath )
// first row: core name of the pdf output
const QString coreName = ts.readLine();
// second row: version string, in the form 'Version %u'
QString versionstr = ts.readLine();
QRegExp versionre( QStringLiteral("Version (\\d+)") );
versionre.setCaseSensitivity( Qt::CaseInsensitive );
if ( !versionre.exactMatch( versionstr ) )
const QString versionstr = ts.readLine();
// anchor the pattern with \A and \z to match the entire subject string
// TODO: with Qt 5.12 QRegularExpression::anchoredPattern() can be used instead
QRegularExpression versionre( QStringLiteral("\\AVersion \\d+\\z")
, QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = versionre.match( versionstr );
if ( !match.hasMatch() ) {
return;
}
QHash<int, pdfsyncpoint> points;
QStack<QString> fileStack;
......
......@@ -21,7 +21,7 @@
#include <QDir>
#include <QString>
#include <QRegExp>
#include <QRegularExpression>
namespace LCHMUrlFactory
{
......@@ -29,7 +29,8 @@ namespace LCHMUrlFactory
static inline bool isRemoteURL( const QString & url, QString & protocol )
{
// Check whether the URL is external
QRegExp uriregex ( QStringLiteral("^(\\w+):\\/\\/") );
QRegularExpression uriregex( QStringLiteral("^(\\w+):\\/\\/") );
QRegularExpressionMatch match;
// mailto: can also have different format, so handle it
if ( url.startsWith( QLatin1String("mailto:") ) )
......@@ -37,9 +38,9 @@ static inline bool isRemoteURL( const QString & url, QString & protocol )
protocol = QStringLiteral("mailto");
return true;
}
else if ( uriregex.indexIn( url ) != -1 )
else if ( (match = uriregex.match( url ) ).hasMatch() )
{
QString proto = uriregex.cap ( 1 ).toLower();
const QString proto = match.captured( 1 ).toLower();
// Filter the URLs which need to be opened by a browser
if ( proto == QLatin1String("http")
......@@ -64,13 +65,13 @@ static inline bool isJavascriptURL( const QString & url )
// Parse urls like "ms-its:file name.chm::/topic.htm"
static inline bool isNewChmURL( const QString & url, QString & chmfile, QString & page )
{
QRegExp uriregex ( QStringLiteral("^ms-its:(.*)::(.*)$") );
uriregex.setCaseSensitivity( Qt::CaseInsensitive );
if ( uriregex.indexIn ( url ) != -1 )
QRegularExpression uriregex ( QStringLiteral("^ms-its:(.*)::(.*)$") );
uriregex.setPatternOptions( QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match = uriregex.match( url );
if ( match.hasMatch() )
{
chmfile = uriregex.cap ( 1 );
page = uriregex.cap ( 2 );
chmfile = match.captured( 1 );
page = match.captured( 2 );
return true;
}
......
......@@ -12,7 +12,6 @@
#include <QEventLoop>
#include <QFile>
#include <QFileInfo>
#include <QRegExp>
#include <QGlobalStatic>
#include <QTemporaryDir>
......
......@@ -9,7 +9,7 @@
#include "unrarflavours.h"
#include <QRegExp>
#include <QRegularExpression>
ProcessArgs::ProcessArgs()
{
......@@ -74,13 +74,15 @@ FreeUnrarFlavour::FreeUnrarFlavour()
QStringList FreeUnrarFlavour::processListing( const QStringList &data )
{
QRegExp re( QStringLiteral("^ ([^/]+/([^\\s]+))$") );
QRegularExpression re( QStringLiteral("^ ([^/]+/([^\\s]+))$") );
QStringList newdata;
for ( const QString &line : data )
{
if ( re.exactMatch( line ) )
newdata.append( re.cap( 1 ) );
QRegularExpressionMatch match = re.match( line );
if ( match.hasMatch() ) {
newdata.append( match.captured( 1 ) );
}
}
return newdata;
}
......
......@@ -38,7 +38,7 @@
#include <QLabel>
#include <QPainter>
#include <QProgressBar>
#include <QRegExp>
#include <QRegularExpression>
//------ now comes the dviRenderer class implementation ----------
......@@ -708,19 +708,20 @@ QString dviRenderer::PDFencodingToQString(const QString& _pdfstring)
pdfstring = pdfstring.replace(QLatin1String("\\\\"), QLatin1String("\\"));
// Now replace octal character codes with the characters they encode
int pos;
QRegExp rx( QStringLiteral("(\\\\)(\\d\\d\\d)") ); // matches "\xyz" where x,y,z are numbers
while((pos = rx.indexIn( pdfstring )) != -1) {
pdfstring = pdfstring.replace(pos, 4, QChar(rx.cap(2).toInt(nullptr,8)));
QRegularExpression regex( QStringLiteral("(\\\\)(\\d\\d\\d)") ); // matches "\xyz" where x,y,z are numbers
QRegularExpressionMatch match;
while ((match = regex.match(pdfstring)).hasMatch()) {
pdfstring = pdfstring.replace(match.capturedStart(0), 4, QChar(match.captured(2).toInt(nullptr, 8)));
}
rx.setPattern( QStringLiteral("(\\\\)(\\d\\d)") ); // matches "\xy" where x,y are numbers
while((pos = rx.indexIn( pdfstring )) != -1) {
pdfstring = pdfstring.replace(pos, 3, QChar(rx.cap(2).toInt(nullptr,8)));
regex.setPattern( QStringLiteral("(\\\\)(\\d\\d)") ); // matches "\xy" where x,y are numbers
while ((match = regex.match(pdfstring)).hasMatch()) {
pdfstring = pdfstring.replace(match.capturedStart(0), 3, QChar(match.captured(2).toInt(nullptr,8)));
}
rx.setPattern( QStringLiteral("(\\\\)(\\d)") ); // matches "\x" where x is a number
while((pos = rx.indexIn( pdfstring )) != -1) {
pdfstring = pdfstring.replace(pos, 4, QChar(rx.cap(2).toInt(nullptr,8)));
regex.setPattern( QStringLiteral("(\\\\)(\\d)") ); // matches "\x" where x is a number
while ((match = regex.match(pdfstring)).hasMatch()) {
pdfstring = pdfstring.replace(match.capturedStart(0), 2, QChar(match.captured(2).toInt(nullptr,8)));
}
return pdfstring;
}
......
......@@ -25,7 +25,6 @@
#include <qimage.h>
#include <qlayout.h>
#include <qmutex.h>
#include <qregexp.h>
#include <qstack.h>
#include <qtemporaryfile.h>
#include <qtextstream.h>
......
......@@ -23,7 +23,7 @@
#include <QList>
#include <QTimer>
#include <QRegExp>
#include <QRegularExpression>
#include <QApplication>
#include <QContextMenuEvent>
#include <QHBoxLayout>
......@@ -259,18 +259,21 @@ bool KTreeViewSearchLine::itemMatches( const QModelIndex &parentIndex, int row,
return false;
// Construct a regular expression object with the right options.
QRegExp expression = QRegExp( pattern,
d->caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive,
d->regularExpression ? QRegExp::RegExp : QRegExp::FixedString );
QRegularExpression re(
d->regularExpression ? pattern : QRegularExpression::escape(pattern),
d->caseSensitive ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption );
QRegularExpressionMatch match;
// If the search column list is populated, search just the columns
// specified. If it is empty default to searching all of the columns.
QAbstractItemModel *model = d->treeView->model();
const int columncount = model->columnCount( parentIndex );
for ( int i = 0; i < columncount; ++i) {
if ( expression.indexIn( model->data( model->index( row, i, parentIndex ), Qt::DisplayRole ).toString() ) >= 0 )
return true;
const QString str = model->data( model->index( row, i, parentIndex ), Qt::DisplayRole ).toString();
match = re.match( str );
if ( match.hasMatch() ) {
return true;
}
}
return false;
......
......@@ -22,6 +22,7 @@
#include <QImage>
#include <qtemporaryfile.h>
#include <QTextStream>
#include <QRegularExpression>
#include <QStandardPaths>
#include "debug_ui.h"
......@@ -46,42 +47,41 @@ LatexRenderer::Error LatexRenderer::renderLatexInHtml( QString& html, const QCol
if( !html.contains(QStringLiteral("$$")))
return NoError;
// this searches for $$formula$$
QRegExp rg(QStringLiteral("\\$\\$.+\\$\\$"));
rg.setMinimal(true);
int pos = 0;
// this searches for $$formula$$
QRegularExpression rg(QStringLiteral("\\$\\$.+?\\$\\$"));
QRegularExpressionMatchIterator it = rg.globalMatch(html);
QMap<QString, QString> replaceMap;
while (pos >= 0 && pos < html.length())
{
pos = rg.indexIn(html, pos);
if (pos >= 0 )
{
const QString match = rg.cap(0);
pos += rg.matchedLength();
QString formul=match;
// first remove the $$ delimiters on start and end
formul.remove(QStringLiteral("$$"));
// then trim the result, so we can skip totally empty/whitespace-only formulas
formul = formul.trimmed();
if (formul.isEmpty() || !securityCheck(formul))
continue;
//unescape formula
formul.replace(QLatin1String("&gt;"),QLatin1String(">")).replace(QLatin1String("&lt;"),QLatin1String("<")).replace(QLatin1String("&amp;"),QLatin1String("&")).replace(QLatin1String("&quot;"),QLatin1String("\"")).replace(QLatin1String("&apos;"),QLatin1String("\'")).replace(QLatin1String("<br>"),QLatin1String(" "));
QString fileName;
Error returnCode = handleLatex(fileName, formul, textColor, fontSize, resolution, latexOutput);
if (returnCode != NoError)
return returnCode;
replaceMap[match] = fileName;
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
const QString matchedString = match.captured(0);
QString formul = matchedString;
// first remove the $$ delimiters on start and end
formul.remove(QStringLiteral("$$"));
// then trim the result, so we can skip totally empty/whitespace-only formulas
formul = formul.trimmed();
if (formul.isEmpty() || !securityCheck(formul)) {
continue;
}
//unescape formula
formul.replace(QLatin1String("&gt;"), QLatin1String(">"));
formul.replace(QLatin1String("&lt;"), QLatin1String("<"));
formul.replace(QLatin1String("&amp;"), QLatin1String("&"));
formul.replace(QLatin1String("&quot;"), QLatin1String("\""));
formul.replace(QLatin1String("&apos;"), QLatin1String("\'"));
formul.replace(QLatin1String("<br>"), QLatin1String(" "));
QString fileName;
Error returnCode = handleLatex(fileName, formul, textColor, fontSize, resolution, latexOutput);
if (returnCode != NoError) {
return returnCode;
}
replaceMap[matchedString] = fileName;
}
if(replaceMap.isEmpty()) //we haven't found any LaTeX strings
return NoError;
......@@ -105,9 +105,8 @@ bool LatexRenderer::mightContainLatex (const QString& text)
return false;
// this searches for $$formula$$
QRegExp rg(QStringLiteral("\\$\\$.+\\$\\$"));
rg.setMinimal(true);
if( rg.lastIndexIn(text) == -1 )
QRegularExpression rg(QStringLiteral("\\$\\$.+?\\$\\$"));
if( !rg.match(text).hasMatch() )
return false;
return true;
......@@ -191,7 +190,7 @@ LatexRenderer::Error LatexRenderer::handleLatex( QString& fileName, const QStrin
bool LatexRenderer::securityCheck( const QString &latexFormula )
{
return !latexFormula.contains(QRegExp(QString::fromLatin1("\\\\(def|let|futurelet|newcommand|renewcommand|else|fi|write|input|include"
return !latexFormula.contains(QRegularExpression(QString::fromLatin1("\\\\(def|let|futurelet|newcommand|renewcommand|else|fi|write|input|include"
"|chardef|catcode|makeatletter|noexpand|toksdef|every|errhelp|errorstopmode|scrollmode|nonstopmode|batchmode"
"|read|csname|newhelp|relax|afterground|afterassignment|expandafter|noexpand|special|command|loop|repeat|toks"
"|output|line|mathcode|name|item|section|mbox|DeclareRobustCommand)[^a-zA-Z]")));
......
......@@ -10,26 +10,32 @@
#ifndef URL_UTILS_H
#define URL_UTILS_H
#include <QRegExp>
#include <QRegularExpression>
namespace UrlUtils
{
QString getUrl( QString txt )
{
// match the url
QRegExp reg( QStringLiteral( "\\b((https?|ftp)://(www\\d{0,3}[.])?[\\S]+)|((www\\d{0,3}[.])[\\S]+)" ) );
QRegularExpression reg( QStringLiteral( "\\b((https?|ftp)://(www\\d{0,3}[.])?[\\S]+)|((www\\d{0,3}[.])[\\S]+)" ) );
// blocks from detecting invalid urls
QRegExp reg1( QStringLiteral( "[\\w'\"\\(\\)]+https?://|[\\w'\"\\(\\)]+ftp://|[\\w'\"\\(\\)]+www\\d{0,3}[.]" ) );
txt = txt.remove( QStringLiteral("\n") );
if( reg1.indexIn( txt ) < 0 && reg.indexIn( txt ) >= 0 && QUrl( reg.cap() ).isValid() )
{
QString url = reg.cap();
if( url.startsWith( QLatin1String("www") ) )
QRegularExpression reg1( QStringLiteral( "[\\w'\"\\(\\)]+https?://|[\\w'\"\\(\\)]+ftp://|[\\w'\"\\(\\)]+www\\d{0,3}[.]" ) );
txt = txt.remove( QLatin1Char('\n') );
if ( reg1.match( txt ).hasMatch() ) { // return early if there is a match (url is not valid)
return QString();
}
QRegularExpressionMatch match = reg.match( txt );
QString url = match.captured();
if ( match.hasMatch() && QUrl( url ).isValid() ) {
if( url.startsWith( QLatin1String("www") ) ) {
url.prepend( QLatin1String("http://") );
}
return url;
}
else
return QString();
return QString();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment