Commit 2ebafbc7 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

[PATCH 2] Port some QRegExp usage to QRegularExpression

src/common.*

Octal escape sequences with more than three digits \nnn, must be wrapped in
\o{....}, c.f. https://perldoc.perl.org/perlre

Use QRegularExpression::anchoredPattern() to get similar behaviour to
QRegExp::exactMatch().

colorOnlyRegExp isn't used anywhere AFAICS.
parent 4537f3a5
...@@ -145,7 +145,7 @@ namespace Konversation ...@@ -145,7 +145,7 @@ namespace Konversation
{ {
TextUrlData data; TextUrlData data;
QString htmlText(text); QString htmlText(text);
urlPattern.setCaseSensitivity(Qt::CaseInsensitive); urlPattern.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
int pos = 0; int pos = 0;
int urlLen = 0; int urlLen = 0;
...@@ -153,9 +153,10 @@ namespace Konversation ...@@ -153,9 +153,10 @@ namespace Konversation
QString protocol; QString protocol;
QString href; QString href;
while ((pos = urlPattern.indexIn(htmlText, pos)) >= 0) QRegularExpressionMatch rmatch;
while ((pos = htmlText.indexOf(urlPattern, pos, &rmatch)) >= 0)
{ {
urlLen = urlPattern.matchedLength(); urlLen = rmatch.capturedLength(0);
href = htmlText.mid(pos, urlLen); href = htmlText.mid(pos, urlLen);
data.urlRanges << QPair<int, int>(pos, href.length()); data.urlRanges << QPair<int, int>(pos, href.length());
...@@ -164,9 +165,9 @@ namespace Konversation ...@@ -164,9 +165,9 @@ namespace Konversation
if (doUrlFixup) if (doUrlFixup)
{ {
protocol.clear(); protocol.clear();
if (urlPattern.cap(2).isEmpty()) if (rmatch.captured(2).isEmpty())
{ {
QString urlPatternCap1(urlPattern.cap(1)); QString urlPatternCap1(rmatch.captured(1));
if (urlPatternCap1.contains(QLatin1Char('@'))) if (urlPatternCap1.contains(QLatin1Char('@')))
protocol = QStringLiteral("mailto:"); protocol = QStringLiteral("mailto:");
else if (urlPatternCap1.startsWith(QLatin1String("ftp."), Qt::CaseInsensitive)) else if (urlPatternCap1.startsWith(QLatin1String("ftp."), Qt::CaseInsensitive))
...@@ -191,15 +192,16 @@ namespace Konversation ...@@ -191,15 +192,16 @@ namespace Konversation
int chanLen = 0; int chanLen = 0;
QString channel; QString channel;
while ((pos = chanExp.indexIn(ircText, pos)) >= 0) QRegularExpressionMatch rmatch;
while ((pos = ircText.indexOf(chanExp, pos, &rmatch)) >= 0)
{ {
channel = chanExp.cap(2); channel = rmatch.captured(2);
chanLen = channel.length(); chanLen = channel.length();
// we want the pos where #channel starts // we want the pos where #channel starts
// indexIn gives us the first match and the first match may be // indexIn gives us the first match and the first match may be
// "#test", " #test" or " \"test", so the first Index is off by some chars // "#test", " #test" or " \"test", so the first Index is off by some chars
pos = chanExp.pos(2); pos = rmatch.capturedStart(2);
data.channelRanges << QPair<int, int>(pos, chanLen); data.channelRanges << QPair<int, int>(pos, chanLen);
pos += chanLen; pos += chanLen;
...@@ -215,7 +217,10 @@ namespace Konversation ...@@ -215,7 +217,10 @@ namespace Konversation
bool isUrl(const QString& text) bool isUrl(const QString& text)
{ {
return urlPattern.exactMatch(text); QRegularExpression re(urlPattern);
re.setPattern(QRegularExpression::anchoredPattern(urlPattern.pattern()));
return re.match(text).hasMatch();
} }
QString extractColorCodes(const QString& _text) QString extractColorCodes(const QString& _text)
......
...@@ -10,17 +10,22 @@ ...@@ -10,17 +10,22 @@
#include <QByteArray> #include <QByteArray>
#include <QHash> #include <QHash>
#include <QRegularExpression>
#include <QStringList> #include <QStringList>
class QString; class QString;
namespace Konversation namespace Konversation
{ {
static QRegExp ircMarkupsRegExp(QStringLiteral("[\\0000-\\0037]")); static QRegularExpression ircMarkupsRegExp(QStringLiteral("[\\o{0000}-\\o{0037}]"));
static QRegExp colorRegExp(QStringLiteral("((\003([0-9]|0[0-9]|1[0-5])(,([0-9]|0[0-9]|1[0-5])|)|\017)|\x02|\x03|\x09|\x13|\x15|\x16|\x1d|\x1f)")); static QRegExp colorRegExp(QStringLiteral("((\003([0-9]|0[0-9]|1[0-5])(,([0-9]|0[0-9]|1[0-5])|)|\017)|\x02|\x03|\x09|\x13|\x15|\x16|\x1d|\x1f)"));
static QRegExp colorOnlyRegExp(QStringLiteral("(\003([0-9]|0[0-9]|1[0-5]|)(,([0-9]|0[0-9]|1[0-5])|,|)|\017)"));
static QRegExp urlPattern(QStringLiteral("\\b((?:(?:([a-z][\\w\\.-]+:/{1,3})|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|\\}\\]|[^\\s`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6])|[a-z0-9.\\-+_]+@[a-z0-9.\\-]+[.][a-z]{1,5}[^\\s/`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6]))").arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019))); // static QRegExp colorOnlyRegExp(QStringLiteral("(\003([0-9]|0[0-9]|1[0-5]|)(,([0-9]|0[0-9]|1[0-5])|,|)|\017)"));
static QRegExp chanExp(QStringLiteral("(^|\\s|^\"|\\s\"|,|'|\\(|\\:|!|@|%|\\+)(#[^,\\s;\\)\\:\\/\\(\\<\\>]*[^.,\\s;\\)\\:\\/\\(\"\''\\<\\>?%1%2%3%4%5%6])").arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019)));
static QRegularExpression urlPattern(QStringLiteral("\\b((?:(?:([a-z][\\w\\.-]+:/{1,3})|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|\\}\\]|[^\\s`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6])|[a-z0-9.\\-+_]+@[a-z0-9.\\-]+[.][a-z]{1,5}[^\\s/`!()\\[\\]{};:'\".,<>?%1%2%3%4%5%6]))").arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019)));
static QRegularExpression chanExp(QStringLiteral("(^|\\s|^\"|\\s\"|,|'|\\(|\\:|!|@|%|\\+)(#[^,\\s;\\)\\:\\/\\(\\<\\>]*[^.,\\s;\\)\\:\\/\\(\"\''\\<\\>?%1%2%3%4%5%6])").arg(QChar(0x00AB)).arg(QChar(0x00BB)).arg(QChar(0x201C)).arg(QChar(0x201D)).arg(QChar(0x2018)).arg(QChar(0x2019)));
enum TabNotifyType enum TabNotifyType
{ {
......
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