Replace use of QRegExp

This commit is contained in:
Jonas Kvinge 2020-07-20 00:57:42 +02:00
parent eb270df835
commit 0b7b7656b2
15 changed files with 105 additions and 85 deletions

View File

@ -38,7 +38,6 @@
#include <QString>
#include <QStringList>
#include <QRegularExpression>
#include <QRegExp>
#include <QUrl>
#include <QImage>
#include <QIcon>

View File

@ -51,7 +51,7 @@
#include <QStringList>
#include <QUrl>
#include <QRegularExpression>
#include <QRegExp>
#include <QRegularExpressionMatch>
#include <QTcpServer>
#include <QTemporaryFile>
#include <QPoint>
@ -604,8 +604,9 @@ bool ParseUntilElementCI(QXmlStreamReader *reader, const QString &name) {
QDateTime ParseRFC822DateTime(const QString &text) {
QRegExp regexp("(\\d{1,2}) (\\w{3,12}) (\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})");
if (regexp.indexIn(text) == -1) {
QRegularExpression regexp("(\\d{1,2}) (\\w{3,12}) (\\d+) (\\d{1,2}):(\\d{1,2}):(\\d{1,2})");
QRegularExpressionMatch re_match = regexp.match(text);
if (!re_match.hasMatch()) {
return QDateTime();
}
@ -637,9 +638,9 @@ QDateTime ParseRFC822DateTime(const QString &text) {
monthmap["November"] = 11;
monthmap["December"] = 12;
const QDate date(regexp.cap(static_cast<int>(MatchNames::YEARS)).toInt(), monthmap[regexp.cap(static_cast<int>(MatchNames::MONTHS))], regexp.cap(static_cast<int>(MatchNames::DAYS)).toInt());
const QDate date(re_match.captured(static_cast<int>(MatchNames::YEARS)).toInt(), monthmap[re_match.captured(static_cast<int>(MatchNames::MONTHS))], re_match.captured(static_cast<int>(MatchNames::DAYS)).toInt());
const QTime time(regexp.cap(static_cast<int>(MatchNames::HOURS)).toInt(), regexp.cap(static_cast<int>(MatchNames::MINUTES)).toInt(), regexp.cap(static_cast<int>(MatchNames::SECONDS)).toInt());
const QTime time(re_match.captured(static_cast<int>(MatchNames::HOURS)).toInt(), re_match.captured(static_cast<int>(MatchNames::MINUTES)).toInt(), re_match.captured(static_cast<int>(MatchNames::SECONDS)).toInt());
return QDateTime(date, time);
@ -923,16 +924,17 @@ QString MacAddress() {
QString ReplaceMessage(const QString &message, const Song &song, const QString &newline) {
QRegExp variable_replacer("[%][a-z]+[%]");
QRegularExpression variable_replacer("[%][a-z]+[%]");
QString copy(message);
// Replace the first line
int pos = 0;
variable_replacer.indexIn(message);
while ((pos = variable_replacer.indexIn(message, pos)) != -1) {
QStringList captured = variable_replacer.capturedTexts();
QRegularExpressionMatch match;
for (match = variable_replacer.match(message, pos) ; match.hasMatch() ; match = variable_replacer.match(message, pos)) {
pos = match.capturedStart();
QStringList captured = match.capturedTexts();
copy.replace(captured[0], ReplaceVariable(captured[0], song, newline));
pos += variable_replacer.matchedLength();
pos += match.capturedLength();
}
int index_of = copy.indexOf(QRegularExpression(" - (>|$)"));

View File

@ -27,7 +27,7 @@
#include <QString>
#include <QStringBuilder>
#include <QStringList>
#include <QRegExp>
#include <QRegularExpression>
#include <QUrl>
// This must come after Qt includes
@ -123,7 +123,7 @@ bool CddaLister::Init() {
}
#ifdef Q_OS_MACOS
// Every track is detected as a separate device on Darwin. The raw disk looks like /dev/rdisk1
if (!device.contains(QRegExp("^/dev/rdisk[0-9]$"))) {
if (!device.contains(QRegularExpression("^/dev/rdisk[0-9]$"))) {
continue;
}
#endif

View File

@ -33,7 +33,7 @@
#include <QString>
#include <QStringList>
#include <QRegularExpression>
#include <QRegExp>
#include <QRegularExpressionMatch>
#include <QUrl>
#include <QUrlQuery>
@ -237,11 +237,12 @@ QList<QUrl> GioLister::MakeDeviceUrls(const QString &id) {
url.setScheme("ipod");
}
QRegExp device_re("usb/(\\d+)/(\\d+)");
if (device_re.indexIn(unix_device) >= 0) {
QRegularExpression device_re("usb/(\\d+)/(\\d+)");
QRegularExpressionMatch re_match = device_re.match(unix_device);
if (re_match.hasMatch()) {
QUrlQuery url_query(url);
url_query.addQueryItem("busnum", device_re.cap(1));
url_query.addQueryItem("devnum", device_re.cap(2));
url_query.addQueryItem("busnum", re_match.captured(1));
url_query.addQueryItem("devnum", re_match.captured(2));
url.setQuery(url_query);
}

View File

@ -27,9 +27,10 @@
#include <QList>
#include <QByteArray>
#include <QString>
#include <QRegExp>
#include <QUrl>
#include <QUrlQuery>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QtDebug>
#include "core/logging.h"
@ -39,16 +40,17 @@ MtpConnection::MtpConnection(const QUrl &url) : device_(nullptr) {
QString hostname = url.host();
// Parse the URL
QRegExp host_re("^usb-(\\d+)-(\\d+)$");
QRegularExpression host_re("^usb-(\\d+)-(\\d+)$");
unsigned int bus_location = 0;
unsigned int device_num = 0;
QUrlQuery url_query(url);
if (host_re.indexIn(hostname) >= 0) {
bus_location = host_re.cap(1).toUInt();
device_num = host_re.cap(2).toUInt();
QRegularExpressionMatch re_match = host_re.match(hostname);
if (re_match.hasMatch()) {
bus_location = re_match.captured(1).toUInt();
device_num = re_match.captured(2).toUInt();
}
else if (url_query.hasQueryItem("busnum")) {
bus_location = url_query.queryItemValue("busnum").toUInt();

View File

@ -36,7 +36,8 @@
#include <QIcon>
#include <QImage>
#include <QPixmap>
#include <QRegExp>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QStyle>
#include <QHostAddress>
#include <QSsl>
@ -344,16 +345,18 @@ void LocalRedirectServer::WriteTemplate() const {
page_file.open(QIODevice::ReadOnly);
QString page_data = QString::fromUtf8(page_file.readAll());
QRegExp tr_regexp("tr\\(\"([^\"]+)\"\\)");
QRegularExpression tr_regexp("tr\\(\"([^\"]+)\"\\)");
int offset = 0;
forever {
offset = tr_regexp.indexIn(page_data, offset);
QRegularExpressionMatch re_match = tr_regexp.match(page_data, offset);
if (!re_match.hasMatch()) break;
offset = re_match.capturedStart();
if (offset == -1) {
break;
}
page_data.replace(offset, tr_regexp.matchedLength(), tr(tr_regexp.cap(1).toUtf8()));
offset += tr_regexp.matchedLength();
page_data.replace(offset, re_match.capturedLength(), tr(re_match.captured(1).toUtf8()));
offset += re_match.capturedLength();
}
QBuffer image_buffer;

View File

@ -31,7 +31,8 @@
#include <QStringList>
#include <QUrl>
#include <QUrlQuery>
#include <QRegExp>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QtAlgorithms>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
@ -270,9 +271,10 @@ void MusicBrainzClient::DiscIdRequestFinished(const QString &discid, QNetworkRep
album = reader.readElementText();
}
else if (name == "date") {
QRegExp regex(kDateRegex);
if (regex.indexIn(reader.readElementText()) == 0) {
year = regex.cap(0).toInt();
QRegularExpression regex(kDateRegex);
QRegularExpressionMatch re_match = regex.match(reader.readElementText());
if (re_match.capturedStart() == 0) {
year = re_match.captured(0).toInt();
}
}
else if (name == "artist-credit") {
@ -465,9 +467,10 @@ MusicBrainzClient::Release MusicBrainzClient::ParseRelease(QXmlStreamReader *rea
ret.SetStatusFromString(reader->readElementText());
}
else if (name == "date") {
QRegExp regex(kDateRegex);
if (regex.indexIn(reader->readElementText()) == 0) {
ret.year_ = regex.cap(0).toInt();
QRegularExpression regex(kDateRegex);
QRegularExpressionMatch re_match = regex.match(reader->readElementText());
if (re_match.capturedStart() == 0) {
ret.year_ = re_match.captured(0).toInt();
}
}
else if (name == "track-list") {

View File

@ -28,7 +28,7 @@
#include <QStringBuilder>
#include <QStringList>
#include <QRegularExpression>
#include <QRegExp>
#include <QRegularExpressionMatch>
#include <QUrl>
#include <QFileInfo>
#include <QDir>
@ -180,30 +180,33 @@ QString OrganiseFormat::GetFilenameForSong(const Song &song) const {
QString OrganiseFormat::ParseBlock(QString block, const Song &song, bool *any_empty) const {
QRegExp tag_regexp(kTagPattern);
QRegExp block_regexp(kBlockPattern);
QRegularExpression tag_regexp(kTagPattern);
QRegularExpression block_regexp(kBlockPattern);
// Find any blocks first
int pos = 0;
while ((pos = block_regexp.indexIn(block, pos)) != -1) {
QRegularExpressionMatch re_match;
for (re_match = block_regexp.match(block, pos) ; re_match.hasMatch() ; re_match = block_regexp.match(block, pos)) {
pos = re_match.capturedStart();
// Recursively parse the block
bool empty = false;
QString value = ParseBlock(block_regexp.cap(1), song, &empty);
QString value = ParseBlock(re_match.captured(1), song, &empty);
if (empty) value = "";
// Replace the block's value
block.replace(pos, block_regexp.matchedLength(), value);
block.replace(pos, re_match.capturedLength(), value);
pos += value.length();
}
// Now look for tags
bool empty = false;
pos = 0;
while ((pos = tag_regexp.indexIn(block, pos)) != -1) {
QString value = TagValue(tag_regexp.cap(1), song);
for (re_match = tag_regexp.match(block, pos) ; re_match.hasMatch() ; re_match = tag_regexp.match(block, pos)) {
pos = re_match.capturedStart();
QString value = TagValue(re_match.captured(1), song);
if (value.isEmpty()) empty = true;
block.replace(pos, tag_regexp.matchedLength(), value);
block.replace(pos, re_match.capturedLength(), value);
pos += value.length();
}
@ -281,7 +284,7 @@ OrganiseFormat::Validator::Validator(QObject *parent) : QValidator(parent) {}
QValidator::State OrganiseFormat::Validator::validate(QString &input, int&) const {
QRegExp tag_regexp(kTagPattern);
QRegularExpression tag_regexp(kTagPattern);
// Make sure all the blocks match up
int block_level = 0;
@ -297,12 +300,14 @@ QValidator::State OrganiseFormat::Validator::validate(QString &input, int&) cons
if (block_level != 0) return QValidator::Invalid;
// Make sure the tags are valid
QRegularExpressionMatch re_match;
int pos = 0;
while ((pos = tag_regexp.indexIn(input, pos)) != -1) {
if (!OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1)))
for (re_match = tag_regexp.match(input, pos) ; re_match.hasMatch() ; re_match = tag_regexp.match(input, pos)) {
pos = re_match.capturedStart();
if (!OrganiseFormat::kKnownTags.contains(re_match.captured(1)))
return QValidator::Invalid;
pos += tag_regexp.matchedLength();
pos += re_match.capturedLength();
}
return QValidator::Acceptable;
@ -325,8 +330,8 @@ void OrganiseFormat::SyntaxHighlighter::highlightBlock(const QString &text) {
const QRgb valid_tag_color = light ? kValidTagColorLight : kValidTagColorDark;
const QRgb invalid_tag_color = light ? kInvalidTagColorLight : kInvalidTagColorDark;
QRegExp tag_regexp(kTagPattern);
QRegExp block_regexp(kBlockPattern);
QRegularExpression tag_regexp(kTagPattern);
QRegularExpression block_regexp(kBlockPattern);
QTextCharFormat block_format;
block_format.setBackground(QColor(block_color));
@ -335,20 +340,23 @@ void OrganiseFormat::SyntaxHighlighter::highlightBlock(const QString &text) {
setFormat(0, text.length(), QTextCharFormat());
// Blocks
QRegularExpressionMatch re_match;
int pos = 0;
while ((pos = block_regexp.indexIn(text, pos)) != -1) {
setFormat(pos, block_regexp.matchedLength(), block_format);
pos += block_regexp.matchedLength();
for (re_match = block_regexp.match(text, pos) ; re_match.hasMatch() ; re_match = block_regexp.match(text, pos)) {
pos = re_match.capturedStart();
setFormat(pos, re_match.capturedLength(), block_format);
pos += re_match.capturedLength();
}
// Tags
pos = 0;
while ((pos = tag_regexp.indexIn(text, pos)) != -1) {
for (re_match = tag_regexp.match(text, pos) ; re_match.hasMatch() ; re_match = tag_regexp.match(text, pos)) {
pos = re_match.capturedStart();
QTextCharFormat f = format(pos);
f.setForeground(QColor(OrganiseFormat::kKnownTags.contains(tag_regexp.cap(1)) ? valid_tag_color : invalid_tag_color));
f.setForeground(QColor(OrganiseFormat::kKnownTags.contains(re_match.captured(1)) ? valid_tag_color : invalid_tag_color));
setFormat(pos, tag_regexp.matchedLength(), f);
pos += tag_regexp.matchedLength();
setFormat(pos, re_match.capturedLength(), f);
pos += re_match.capturedLength();
}
}

View File

@ -42,7 +42,6 @@
#include <QString>
#include <QStringBuilder>
#include <QUrl>
#include <QRegExp>
#include <QIcon>
#include <QPixmap>
#include <QPainter>

View File

@ -27,7 +27,7 @@
#include <QString>
#include <QStringBuilder>
#include <QRegularExpression>
#include <QRegExp>
#include <QRegularExpressionMatch>
#include <QUrl>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
@ -49,14 +49,16 @@ SongList ASXParser::Load(QIODevice *device, const QString &playlist_path, const
QByteArray data = device->readAll();
// Some playlists have unescaped & characters in URLs :(
QRegExp ex("(href\\s*=\\s*\")([^\"]+)\"");
QRegularExpression ex("(href\\s*=\\s*\")([^\"]+)\"");
QRegularExpressionMatch re_match;
int index = 0;
while ((index = ex.indexIn(data, index)) != -1) {
QString url = ex.cap(2);
for (re_match = ex.match(data, index) ; re_match.hasMatch() ; re_match = ex.match(data, index)) {
index = re_match.capturedStart();
QString url = re_match.captured(2);
url.replace(QRegularExpression("&(?!amp;|quot;|apos;|lt;|gt;)"), "&amp;");
QByteArray replacement = QString(ex.cap(1) + url + "\"").toLocal8Bit();
data.replace(ex.cap(0).toLocal8Bit(), replacement);
QByteArray replacement = QString(re_match.captured(1) + url + "\"").toLocal8Bit();
data.replace(re_match.captured(0).toLocal8Bit(), replacement);
index += replacement.length();
}

View File

@ -28,7 +28,7 @@
#include <QString>
#include <QStringList>
#include <QRegularExpression>
#include <QRegExp>
#include <QRegularExpressionMatch>
#include <QTextCodec>
#include <QTextStream>
#include <QtDebug>
@ -272,13 +272,14 @@ SongList CueParser::Load(QIODevice *device, const QString &playlist_path, const
// line into logical parts and getting rid of all the unnecessary whitespaces and quoting.
QStringList CueParser::SplitCueLine(const QString &line) const {
QRegExp line_regexp(kFileLineRegExp);
if (!line_regexp.exactMatch(line.trimmed())) {
QRegularExpression line_regexp(kFileLineRegExp);
QRegularExpressionMatch re_match = line_regexp.match(line.trimmed());
if (!re_match.hasMatch()) {
return QStringList();
}
// Let's remove the empty entries while we're at it
return line_regexp.capturedTexts().filter(QRegularExpression(".+")).mid(1, -1);
return re_match.capturedTexts().filter(QRegularExpression(".+")).mid(1, -1);
}
@ -336,12 +337,13 @@ bool CueParser::UpdateLastSong(const CueEntry &entry, Song *song) const {
qint64 CueParser::IndexToMarker(const QString &index) const {
QRegExp index_regexp(kIndexRegExp);
if (!index_regexp.exactMatch(index)) {
QRegularExpression index_regexp(kIndexRegExp);
QRegularExpressionMatch re_match = index_regexp.match(index);
if (!re_match.hasMatch()) {
return -1;
}
QStringList splitted = index_regexp.capturedTexts().mid(1, -1);
QStringList splitted = re_match.capturedTexts().mid(1, -1);
qlonglong frames = splitted.at(0).toLongLong() * 60 * 75 + splitted.at(1).toLongLong() * 75 + splitted.at(2).toLongLong();
return (frames * kNsecPerSec) / 75;

View File

@ -25,7 +25,8 @@
#include <QMap>
#include <QByteArray>
#include <QString>
#include <QRegExp>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QTextStream>
#include "core/timeconstants.h"
@ -48,7 +49,7 @@ SongList PLSParser::Load(QIODevice *device, const QString &playlist_path, const
Q_UNUSED(playlist_path);
QMap<int, Song> songs;
QRegExp n_re("\\d+$");
QRegularExpression n_re("\\d+$");
while (!device->atEnd()) {
QString line = QString::fromUtf8(device->readLine()).trimmed();
@ -56,8 +57,8 @@ SongList PLSParser::Load(QIODevice *device, const QString &playlist_path, const
QString key = line.left(equals).toLower();
QString value = line.mid(equals + 1);
n_re.indexIn(key);
int n = n_re.cap(0).toInt();
QRegularExpressionMatch re_match = n_re.match(key);
int n = re_match.captured(0).toInt();
if (key.startsWith("file")) {
Song song = LoadSong(value, 0, dir);

View File

@ -26,7 +26,8 @@
#include <QVariant>
#include <QString>
#include <QStringList>
#include <QRegExp>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QDir>
#include <QLocale>
#include <QSettings>
@ -74,13 +75,15 @@ BehaviourSettingsPage::BehaviourSettingsPage(SettingsDialog *dialog) : SettingsP
// Populate the language combo box. We do this by looking at all the compiled in translations.
QDir dir(":/translations/");
QStringList codes(dir.entryList(QStringList() << "*.qm"));
QRegExp lang_re("^strawberry_(.*).qm$");
QRegularExpression lang_re("^strawberry_(.*).qm$");
for (const QString &filename : codes) {
// The regex captures the "ru" from "strawberry_ru.qm"
if (!lang_re.exactMatch(filename)) continue;
QRegularExpressionMatch re_match = lang_re.match(filename);
QString code = lang_re.cap(1);
// The regex captures the "ru" from "strawberry_ru.qm"
if (!re_match.hasMatch()) continue;
QString code = re_match.captured(1);
QString lookup_code = QString(code)
.replace("@latin", "_Latn")
.replace("_CN", "_Hans_CN")

View File

@ -28,7 +28,6 @@
#include <QString>
#include <QUrl>
#include <QUrlQuery>
#include <QRegExp>
#include <QImage>
#include <QImageReader>
#include <QNetworkAccessManager>

View File

@ -25,10 +25,6 @@
#include <QFile>
#include <QByteArray>
#include <QString>
#include <QStringList>
#include <QTemporaryFile>
#include <QTextCodec>
#include <QRegExp>
#include <QCryptographicHash>
#include "core/song.h"