Skip to content

Commit

Permalink
Replace deprecated QRegExp with QRegularExpression (#100)
Browse files Browse the repository at this point in the history
* Replaced QRegExp with QRegularExpression in tests

* Replace most QRegExp occurences with QRegularExpression

* Attempt to migrate last QRegExp

I'm not sure about the implementation, gotta make some debug tests first to ensure it doesn't break any functionality.

* Fix match logic and tests
  • Loading branch information
zneix committed Jul 29, 2021
1 parent a1d54c3 commit 5e35445
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 173 deletions.
4 changes: 2 additions & 2 deletions include/IrcCore/ircdebug_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
#include <IrcConnection>
#include <QtCore/qdebug.h>
#include <QtCore/qstring.h>
#include <QtCore/qregexp.h>
#include <QtCore/qdatetime.h>
#include <QRegularExpression>

IRC_BEGIN_NAMESPACE

Expand Down Expand Up @@ -143,7 +143,7 @@ static bool irc_debug_enabled(IrcConnection* c, uint l)
dbg_init = true;
}

return l <= dbg_level && (dbg_name.isEmpty() || QRegExp(dbg_name, Qt::CaseInsensitive, QRegExp::Wildcard).exactMatch(c->displayName()));
return l <= dbg_level && (dbg_name.isEmpty() || QRegularExpression(QRegularExpression::wildcardToRegularExpression(dbg_name), QRegularExpression::CaseInsensitiveOption).match(c->displayName()).hasMatch());
}

#define ircDebug(Connection, Flag) IrcDebug(Connection, Flag)
Expand Down
4 changes: 2 additions & 2 deletions src/core/ircconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "irccore_p.h"
#include "irc.h"
#include <QLocale>
#include <QRegExp>
#include <QRegularExpression>
#include <QDateTime>
#include <QTcpSocket>
#include <QTextCodec>
Expand Down Expand Up @@ -365,7 +365,7 @@ void IrcConnectionPrivate::_irc_filterDestroyed(QObject* filter)

static bool parseServer(const QString& server, QString* host, int* port, bool* ssl)
{
QStringList p = server.split(QRegExp("[: ]"), Qt::SkipEmptyParts);
QStringList p = server.split(QRegularExpression("[: ]"), Qt::SkipEmptyParts);
*host = p.value(0);
*ssl = p.value(1).startsWith(QLatin1Char('+'));
bool ok = false;
Expand Down
3 changes: 2 additions & 1 deletion src/util/irccommandparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "irctoken_p.h"
#include "irccore_p.h"
#include <climits>
#include <QRegularExpression>

IRC_BEGIN_NAMESPACE

Expand Down Expand Up @@ -374,7 +375,7 @@ QString IrcCommandParser::syntax(const QString& command, Details details) const
QString str = info.fullSyntax();
if (details != Full) {
if (details & NoTarget)
str.remove(QRegExp("\\[[^\\]]+\\]"));
str.remove(QRegularExpression("\\[[^\\]]+\\]"));
if (details & NoPrefix)
str.remove("#");
if (details & NoEllipsis)
Expand Down
46 changes: 10 additions & 36 deletions src/util/irctextformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
#include "irctextformat.h"
#include "ircpalette.h"
#include "irccore_p.h"
#if QT_VERSION >= 0x050000
#include <QRegularExpression>
#endif
#include <QStringList>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
#include <QDebug>
#include "irc.h"

IRC_BEGIN_NAMESPACE
Expand Down Expand Up @@ -113,15 +111,16 @@ static bool parseColors(const QString& message, int pos, int* len, int* fg = nul
*fg = -1;
if (bg)
*bg = -1;
QRegExp rx(QLatin1String("(\\d{1,2})(?:,(\\d{1,2}))?"));
int idx = rx.indexIn(message, pos);
if (idx == pos) {
*len = rx.matchedLength();

QRegularExpression re("(\\d{1,2})(?:,(\\d{1,2}))?");
auto match = re.match(message, pos);
if (match.hasMatch()) {
*len = match.captured().length();
if (fg)
*fg = rx.cap(1).toInt();
*fg = match.captured(1).toInt();
if (bg) {
bool ok = false;
int tmp = rx.cap(2).toInt(&ok);
int tmp = match.captured(2).toInt(&ok);
if (ok)
*bg = tmp;
}
Expand All @@ -139,7 +138,7 @@ static QString generateLink(const QString& protocol, const QString &raw, const Q
static QString parseUrls(const QString& message, const QString& pattern, QList<QUrl>* urls)
{
QString processed = message;
#if QT_VERSION >= 0x050000

int offset = 0;
QRegularExpression rx(pattern);
QRegularExpressionMatchIterator it = rx.globalMatch(message);
Expand Down Expand Up @@ -167,32 +166,7 @@ static QString parseUrls(const QString& message, const QString& pattern, QList<Q
if (urls)
urls->append(QUrl(protocol + raw));
}
#else
int pos = 0;
QRegExp rx(pattern);
while ((pos = rx.indexIn(processed, pos)) >= 0) {
int len = rx.matchedLength();
QString href = processed.mid(pos, len);
QString raw = href;
raw.replace("&amp;", "&");

QString protocol;
if (rx.cap(2).isEmpty()) {
if (rx.cap(1).contains(QLatin1Char('@')))
protocol = QLatin1String("mailto:");
else if (rx.cap(1).startsWith(QLatin1String("ftp."), Qt::CaseInsensitive))
protocol = QLatin1String("ftp://");
else
protocol = QLatin1String("http://");
}

QString link = generateLink(protocol, raw, href);
processed.replace(pos, len, link);
pos += link.length();
if (urls)
urls->append(QUrl(protocol + raw));
}
#endif
return processed;
}

Expand Down
1 change: 0 additions & 1 deletion tests/auto/irc/tst_irc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "ircmodel.h"
#include "ircutil.h"
#include <QtTest/QtTest>
#include <QtCore/QRegExp>

class tst_Irc : public QObject
{
Expand Down
7 changes: 3 additions & 4 deletions tests/auto/ircbuffer/tst_ircbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "ircmessage.h"
#include "ircfilter.h"
#include <QtTest/QtTest>
#include <QtCore/QRegExp>

class tst_IrcBuffer : public QObject
{
Expand Down Expand Up @@ -148,17 +147,17 @@ void tst_IrcBuffer::testDebug()

IrcBuffer buffer;
dbg << &buffer;
QVERIFY(QRegExp("IrcBuffer\\(0x[0-9A-Fa-f]+\\) ").exactMatch(str));
QVERIFY(QRegularExpression("IrcBuffer\\(0x[0-9A-Fa-f]+\\) ").match(str).hasMatch());
str.clear();

buffer.setObjectName("obj");
dbg << &buffer;
QVERIFY(QRegExp("IrcBuffer\\(0x[0-9A-Fa-f]+, name=obj\\) ").exactMatch(str));
QVERIFY(QRegularExpression("IrcBuffer\\(0x[0-9A-Fa-f]+, name=obj\\) ").match(str).hasMatch());
str.clear();

buffer.setName("buf");
dbg << &buffer;
QVERIFY(QRegExp("IrcBuffer\\(0x[0-9A-Fa-f]+, name=obj, title=buf\\) ").exactMatch(str));
QVERIFY(QRegularExpression("IrcBuffer\\(0x[0-9A-Fa-f]+, name=obj, title=buf\\) ").match(str).hasMatch());
str.clear();
}

Expand Down
7 changes: 3 additions & 4 deletions tests/auto/ircchannel/tst_ircchannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "ircchannel.h"
#include <QtTest/QtTest>
#include <QtCore/QRegExp>

class tst_IrcChannel : public QObject
{
Expand Down Expand Up @@ -59,18 +58,18 @@ void tst_IrcChannel::testDebug()

IrcChannel channel;
dbg << &channel;
QVERIFY(QRegExp("IrcChannel\\(0x[0-9A-Fa-f]+\\) ").exactMatch(str));
QVERIFY(QRegularExpression("IrcChannel\\(0x[0-9A-Fa-f]+\\) ").match(str).hasMatch());
str.clear();

channel.setObjectName("obj");
dbg << &channel;
QVERIFY(QRegExp("IrcChannel\\(0x[0-9A-Fa-f]+, name=obj\\) ").exactMatch(str));
QVERIFY(QRegularExpression("IrcChannel\\(0x[0-9A-Fa-f]+, name=obj\\) ").match(str).hasMatch());
str.clear();

channel.setPrefix("#");
channel.setName("communi");
dbg << &channel;
QVERIFY(QRegExp("IrcChannel\\(0x[0-9A-Fa-f]+, name=obj, title=#communi\\) ").exactMatch(str));
QVERIFY(QRegularExpression("IrcChannel\\(0x[0-9A-Fa-f]+, name=obj, title=#communi\\) ").match(str).hasMatch());
str.clear();
}

Expand Down
Loading

1 comment on commit 5e35445

@barracuda156
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zneix Apparently this has broken the build: #117
Using unsupported includes should be conditional.

Please sign in to comment.