Commit ea9276f3 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

Checksum Controller: port to QRegularExpression

parent aff083f4
......@@ -9,53 +9,52 @@
#pragma once
#include <QFile>
#include <Libkleo/ChecksumDefinition>
#include "kleopatra_debug.h"
#include <QFile>
#include <QRegularExpression>
#include <QTextStream>
#ifdef Q_OS_UNIX
// can we use QAbstractFileEngine::caseSensitive()?
// Can we use QAbstractFileEngine::caseSensitive()?
static const Qt::CaseSensitivity fs_cs = Qt::CaseSensitive;
static const QRegularExpression::PatternOption s_regex_cs = QRegularExpression::NoPatternOption;
#else
static const Qt::CaseSensitivity fs_cs = Qt::CaseInsensitive;
static const QRegularExpression::PatternOption s_regex_cs = QRegularExpression::CaseInsensitiveOption;
#endif
static QList<QRegExp> get_patterns(const std::vector< std::shared_ptr<Kleo::ChecksumDefinition> > &checksumDefinitions)
static QList<QRegularExpression> get_patterns(const std::vector< std::shared_ptr<Kleo::ChecksumDefinition> > &checksumDefinitions)
{
QList<QRegExp> result;
QList<QRegularExpression> result;
for (const std::shared_ptr<Kleo::ChecksumDefinition> &cd : checksumDefinitions)
if (cd) {
const auto patterns = cd->patterns();
for (const QString &pattern : patterns) {
result.push_back(QRegExp(pattern, fs_cs));
result.push_back(QRegularExpression(QRegularExpression::anchoredPattern(pattern), s_regex_cs));
}
}
return result;
}
namespace
{
struct matches_any : std::unary_function<QString, bool> {
const QList<QRegExp> m_regexps;
explicit matches_any(const QList<QRegExp> &regexps) : m_regexps(regexps) {}
const QList<QRegularExpression> m_regexps;
explicit matches_any(const QList<QRegularExpression> &regexps) : m_regexps(regexps) {}
bool operator()(const QString &s) const
{
return std::any_of(m_regexps.cbegin(), m_regexps.cend(),
[s](const QRegExp &rx) { return rx.exactMatch(s); });
[s](const QRegularExpression &rx) { return rx.match(s).hasMatch(); });
}
};
}
namespace
{
struct File {
QString name;
QByteArray checksum;
bool binary;
};
}
static QString decode(const QString &encoded)
{
......@@ -89,15 +88,16 @@ static std::vector<File> parse_sum_file(const QString &fileName)
QFile f(fileName);
if (f.open(QIODevice::ReadOnly)) {
QTextStream s(&f);
QRegExp rx(QLatin1String("(\\?)([a-f0-9A-F]+) ([ *])([^\n]+)\n*"));
static const QRegularExpression rx(QRegularExpression::anchoredPattern(uR"((\\?)([a-f0-9A-F]+) ([ *])([^\\n]+)\\n*)"));
while (!s.atEnd()) {
const QString line = s.readLine();
if (rx.exactMatch(line)) {
Q_ASSERT(!rx.cap(4).endsWith(QLatin1Char('\n')));
QRegularExpressionMatch match = rx.match(line);
if (match.hasMatch()) {
Q_ASSERT(!match.capturedView(4).endsWith(QLatin1Char('\n')));
const File file = {
rx.cap(1) == QLatin1String("\\") ? decode(rx.cap(4)) : rx.cap(4),
rx.cap(2).toLatin1(),
rx.cap(3) == QLatin1String("*"),
match.capturedView(1) == QLatin1String("\\") ? decode(match.captured(4)) : match.captured(4),
match.capturedView(2).toLatin1(),
match.capturedView(3) == QLatin1String("*"),
};
files.push_back(file);
}
......@@ -106,7 +106,6 @@ static std::vector<File> parse_sum_file(const QString &fileName)
return files;
}
static std::shared_ptr<Kleo::ChecksumDefinition> filename2definition(const QString &fileName,
const std::vector< std::shared_ptr<Kleo::ChecksumDefinition> > &checksumDefinitions)
{
......@@ -114,7 +113,8 @@ static std::shared_ptr<Kleo::ChecksumDefinition> filename2definition(const QStri
if (cd) {
const auto patterns = cd->patterns();
for (const QString &pattern : patterns) {
if (QRegExp(pattern, fs_cs).exactMatch(fileName)) {
const QRegularExpression re(QRegularExpression::anchoredPattern(pattern), s_regex_cs);
if (re.match(fileName).hasMatch()) {
return cd;
}
}
......
......@@ -260,7 +260,7 @@ void CreateChecksumsController::setFiles(const QStringList &files)
{
kleo_assert(!d->isRunning());
kleo_assert(!files.empty());
const QList<QRegExp> patterns = get_patterns(d->checksumDefinitions);
const QList<QRegularExpression> patterns = get_patterns(d->checksumDefinitions);
if (!std::all_of(files.cbegin(), files.cend(), matches_any(patterns)) &&
!std::none_of(files.cbegin(), files.cend(), matches_any(patterns))) {
throw Exception(gpg_error(GPG_ERR_INV_ARG), i18n("Create Checksums: input files must be either all checksum files or all files to be checksummed, not a mixture of both."));
......@@ -326,13 +326,13 @@ struct Dir {
}
static QStringList remove_checksum_files(QStringList l, const QList<QRegExp> &rxs)
static QStringList remove_checksum_files(QStringList l, const QList<QRegularExpression> &rxs)
{
QStringList::iterator end = l.end();
for (const QRegExp &rx : rxs) {
for (const QRegularExpression &rx : rxs) {
end = std::remove_if(l.begin(), end,
[rx](const QString &str) {
return rx.exactMatch(str);
return rx.match(str).hasMatch();
});
}
l.erase(end, l.end());
......@@ -353,7 +353,7 @@ static std::vector<Dir> find_dirs_by_sum_files(const QStringList &files, bool al
const std::vector< std::shared_ptr<ChecksumDefinition> > &checksumDefinitions)
{
const QList<QRegExp> patterns = get_patterns(checksumDefinitions);
const QList<QRegularExpression> patterns = get_patterns(checksumDefinitions);
std::vector<Dir> dirs;
dirs.reserve(files.size());
......@@ -415,7 +415,7 @@ static std::vector<Dir> find_dirs_by_input_files(const QStringList &files, const
return std::vector<Dir>();
}
const QList<QRegExp> patterns = get_patterns(checksumDefinitions);
const QList<QRegularExpression> patterns = get_patterns(checksumDefinitions);
std::map<QDir, QStringList, less_dir> dirs2files;
......
......@@ -72,16 +72,14 @@ static QStringList fs_intersect(QStringList l1, QStringList l2)
}
#endif
namespace
{
namespace {
struct matches_none_of : std::unary_function<QString, bool> {
const QList<QRegExp> m_regexps;
explicit matches_none_of(const QList<QRegExp> &regexps) : m_regexps(regexps) {}
const QList<QRegularExpression> m_regexps;
explicit matches_none_of(const QList<QRegularExpression> &regexps) : m_regexps(regexps) {}
bool operator()(const QString &s) const
{
return std::none_of(m_regexps.cbegin(), m_regexps.cend(),
[&s](const QRegExp &rx) { return rx.exactMatch(s); });
[&s](const QRegularExpression &rx) { return rx.match(s).hasMatch(); });
}
};
}
......@@ -219,7 +217,7 @@ struct SumFile {
}
static QStringList filter_checksum_files(QStringList l, const QList<QRegExp> &rxs)
static QStringList filter_checksum_files(QStringList l, const QList<QRegularExpression> &rxs)
{
l.erase(std::remove_if(l.begin(), l.end(),
matches_none_of(rxs)),
......@@ -325,7 +323,7 @@ static std::vector<SumFile> find_sums_by_input_files(const QStringList &files, Q
const std::function<void(int)> &progress,
const std::vector< std::shared_ptr<ChecksumDefinition> > &checksumDefinitions)
{
const QList<QRegExp> patterns = get_patterns(checksumDefinitions);
const QList<QRegularExpression> patterns = get_patterns(checksumDefinitions);
const matches_any is_sum_file(patterns);
......@@ -419,10 +417,11 @@ static std::vector<SumFile> find_sums_by_input_files(const QStringList &files, Q
static QStringList c_lang_environment()
{
static const QRegularExpression re(QRegularExpression::anchoredPattern(u"LANG=.*"), s_regex_cs);
QStringList env = QProcess::systemEnvironment();
env.erase(std::remove_if(env.begin(), env.end(),
[](const QString &str) {
return QRegExp(QLatin1String("^LANG=.*"), fs_cs).exactMatch(str);
return re.match(str).hasMatch();
}),
env.end());
env.push_back(QStringLiteral("LANG=C"));
......
Supports Markdown
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