refactor: simplify Version operators

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2023-01-19 21:31:55 -03:00
parent 5ae69c079a
commit 81848e05f1
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 28 additions and 42 deletions

View File

@ -1,67 +1,41 @@
#include "Version.h" #include "Version.h"
#include <QDebug> #include <QDebug>
#include <QUrl>
#include <QRegularExpression> #include <QRegularExpression>
#include <QRegularExpressionMatch> #include <QRegularExpressionMatch>
#include <QUrl>
Version::Version(const QString &str) : m_string(str) Version::Version(QString str) : m_string(std::move(str))
{ {
parse(); parse();
} }
bool Version::operator<(const Version &other) const bool Version::operator<(const Version& other) const
{ {
const int size = qMax(m_sections.size(), other.m_sections.size()); const auto size = qMax(m_sections.size(), other.m_sections.size());
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i) {
{ const Section sec1 =
const Section sec1 = (i >= m_sections.size()) ? Section("") : m_sections.at(i); (i >= m_sections.size()) ? Section("") : m_sections.at(i);
const Section sec2 = const Section sec2 =
(i >= other.m_sections.size()) ? Section("") : other.m_sections.at(i); (i >= other.m_sections.size()) ? Section("") : other.m_sections.at(i);
if (sec1 != sec2) if (sec1 != sec2)
{
return sec1 < sec2; return sec1 < sec2;
}
} }
return false; return false;
} }
bool Version::operator<=(const Version &other) const bool Version::operator==(const Version& other) const
{ {
return *this < other || *this == other; const auto size = qMax(m_sections.size(), other.m_sections.size());
} for (int i = 0; i < size; ++i) {
bool Version::operator>(const Version &other) const const Section sec1 =
{ (i >= m_sections.size()) ? Section("") : m_sections.at(i);
const int size = qMax(m_sections.size(), other.m_sections.size());
for (int i = 0; i < size; ++i)
{
const Section sec1 = (i >= m_sections.size()) ? Section("") : m_sections.at(i);
const Section sec2 = const Section sec2 =
(i >= other.m_sections.size()) ? Section("") : other.m_sections.at(i); (i >= other.m_sections.size()) ? Section("") : other.m_sections.at(i);
if (sec1 != sec2)
{
return sec1 > sec2;
}
}
return false;
}
bool Version::operator>=(const Version &other) const
{
return *this > other || *this == other;
}
bool Version::operator==(const Version &other) const
{
const int size = qMax(m_sections.size(), other.m_sections.size());
for (int i = 0; i < size; ++i)
{
const Section sec1 = (i >= m_sections.size()) ? Section("") : m_sections.at(i);
const Section sec2 =
(i >= other.m_sections.size()) ? Section("") : other.m_sections.at(i);
if (sec1 != sec2) if (sec1 != sec2)
{
return false; return false;
}
} }
return true; return true;
@ -70,6 +44,18 @@ bool Version::operator!=(const Version &other) const
{ {
return !operator==(other); return !operator==(other);
} }
bool Version::operator<=(const Version &other) const
{
return *this < other || *this == other;
}
bool Version::operator>(const Version &other) const
{
return !(*this <= other);
}
bool Version::operator>=(const Version &other) const
{
return !(*this < other);
}
void Version::parse() void Version::parse()
{ {
@ -96,7 +82,7 @@ void Version::parse()
} }
/// qDebug print support for the BlockedMod struct /// qDebug print support for the Version class
QDebug operator<<(QDebug debug, const Version& v) QDebug operator<<(QDebug debug, const Version& v)
{ {
QDebugStateSaver saver(debug); QDebugStateSaver saver(debug);

View File

@ -45,8 +45,8 @@ class QUrl;
class Version class Version
{ {
public: public:
Version(const QString &str); Version(QString str);
Version() {} Version() = default;
bool operator<(const Version &other) const; bool operator<(const Version &other) const;
bool operator<=(const Version &other) const; bool operator<=(const Version &other) const;