pollymc/depends/util/src/modutils.cpp

151 lines
3.3 KiB
C++
Raw Normal View History

#include "include/modutils.h"
#include <QStringList>
#include <QUrl>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
Util::Version::Version(const QString &str) : m_string(str)
{
2014-09-06 21:46:56 +05:30
parse();
}
bool Util::Version::operator<(const Version &other) const
{
2014-09-06 21:46:56 +05:30
const int size = qMax(m_sections.size(), other.m_sections.size());
for (int i = 0; i < size; ++i)
{
2014-09-06 21:46:56 +05:30
const Section sec1 = (i >= m_sections.size()) ? Section("0", 0) : m_sections.at(i);
const Section sec2 =
(i >= other.m_sections.size()) ? Section("0", 0) : other.m_sections.at(i);
if (sec1 != sec2)
{
2014-09-06 21:46:56 +05:30
return sec1 < sec2;
}
}
return false;
}
bool Util::Version::operator<=(const Util::Version &other) const
{
return *this < other || *this == other;
}
bool Util::Version::operator>(const Version &other) const
{
2014-09-06 21:46:56 +05:30
const int size = qMax(m_sections.size(), other.m_sections.size());
for (int i = 0; i < size; ++i)
{
2014-09-06 21:46:56 +05:30
const Section sec1 = (i >= m_sections.size()) ? Section("0", 0) : m_sections.at(i);
const Section sec2 =
(i >= other.m_sections.size()) ? Section("0", 0) : other.m_sections.at(i);
if (sec1 != sec2)
{
2014-09-06 21:46:56 +05:30
return sec1 > sec2;
}
}
return false;
}
2014-09-06 21:46:56 +05:30
bool Util::Version::operator>=(const Version &other) const
{
return *this > other || *this == other;
}
bool Util::Version::operator==(const Version &other) const
{
2014-09-06 21:46:56 +05:30
const int size = qMax(m_sections.size(), other.m_sections.size());
for (int i = 0; i < size; ++i)
{
2014-09-06 21:46:56 +05:30
const Section sec1 = (i >= m_sections.size()) ? Section("0", 0) : m_sections.at(i);
const Section sec2 =
(i >= other.m_sections.size()) ? Section("0", 0) : other.m_sections.at(i);
if (sec1 != sec2)
{
2014-09-06 21:46:56 +05:30
return false;
}
}
return true;
}
bool Util::Version::operator!=(const Version &other) const
{
return !operator==(other);
}
2014-09-06 21:46:56 +05:30
void Util::Version::parse()
{
2014-09-06 21:46:56 +05:30
m_sections.clear();
QStringList parts = m_string.split('.');
for (const auto part : parts)
{
2014-09-06 21:46:56 +05:30
bool ok = false;
int num = part.toInt(&ok);
if (ok)
{
m_sections.append(Section(part, num));
}
else
{
m_sections.append(Section(part));
}
}
}
bool Util::versionIsInInterval(const QString &version, const QString &interval)
{
2014-09-06 21:46:56 +05:30
return versionIsInInterval(Util::Version(version), interval);
}
bool Util::versionIsInInterval(const Version &version, const QString &interval)
{
if (interval.isEmpty() || version.toString() == interval)
{
return true;
}
// Interval notation is used
QRegularExpression exp(
2014-09-06 21:46:56 +05:30
"(?<start>[\\[\\]\\(\\)])(?<bottom>.*?)(,(?<top>.*?))?(?<end>[\\[\\]\\(\\)]),?");
QRegularExpressionMatch match = exp.match(interval);
if (match.hasMatch())
{
const QChar start = match.captured("start").at(0);
const QChar end = match.captured("end").at(0);
const QString bottom = match.captured("bottom");
const QString top = match.captured("top");
// check if in range (bottom)
if (!bottom.isEmpty())
{
2014-09-06 21:46:56 +05:30
const auto bottomVersion = Util::Version(bottom);
if ((start == '[') && !(version >= bottomVersion))
{
return false;
}
2014-09-06 21:46:56 +05:30
else if ((start == '(') && !(version > bottomVersion))
{
return false;
}
}
// check if in range (top)
if (!top.isEmpty())
{
2014-09-06 21:46:56 +05:30
const auto topVersion = Util::Version(top);
if ((end == ']') && !(version <= topVersion))
{
return false;
}
2014-09-06 21:46:56 +05:30
else if ((end == ')') && !(version < topVersion))
{
return false;
}
}
return true;
}
return false;
}