refactor: clean up Section struct

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2023-01-19 21:59:33 -03:00
parent 81848e05f1
commit bcebb1920f
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469

View File

@ -36,15 +36,14 @@
#pragma once #pragma once
#include <QDebug> #include <QDebug>
#include <QList>
#include <QString> #include <QString>
#include <QStringView> #include <QStringView>
#include <QList>
class QUrl; class QUrl;
class Version class Version {
{ public:
public:
Version(QString str); Version(QString str);
Version() = default; Version() = default;
@ -55,125 +54,104 @@ public:
bool operator==(const Version &other) const; bool operator==(const Version &other) const;
bool operator!=(const Version &other) const; bool operator!=(const Version &other) const;
QString toString() const QString toString() const { return m_string; }
{
return m_string;
}
friend QDebug operator<<(QDebug debug, const Version& v); friend QDebug operator<<(QDebug debug, const Version& v);
private: private:
QString m_string; struct Section {
struct Section explicit Section(QString fullString) : m_isNull(true), m_fullString(std::move(fullString))
{
explicit Section(const QString &fullString)
{ {
m_fullString = fullString;
m_isNull = true;
int cutoff = m_fullString.size(); int cutoff = m_fullString.size();
for(int i = 0; i < m_fullString.size(); i++) for (int i = 0; i < m_fullString.size(); i++) {
{ if (!m_fullString[i].isDigit()) {
if(!m_fullString[i].isDigit())
{
cutoff = i; cutoff = i;
break; break;
} }
} }
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
auto numPart = QStringView{m_fullString}.left(cutoff); auto numPart = QStringView{m_fullString}.left(cutoff);
#else #else
auto numPart = m_fullString.leftRef(cutoff); auto numPart = m_fullString.leftRef(cutoff);
#endif #endif
if(numPart.size())
{ if (!numPart.isEmpty()) {
numValid = true;
m_isNull = false; m_isNull = false;
m_numPart = numPart.toInt(); m_numPart = numPart.toInt();
} }
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
auto stringPart = QStringView{m_fullString}.mid(cutoff); auto stringPart = QStringView{m_fullString}.mid(cutoff);
#else #else
auto stringPart = m_fullString.midRef(cutoff); auto stringPart = m_fullString.midRef(cutoff);
#endif #endif
if(stringPart.size())
{ if (!stringPart.isEmpty()) {
m_isNull = false; m_isNull = false;
m_stringPart = stringPart.toString(); m_stringPart = stringPart.toString();
} }
} }
explicit Section() {}
bool numValid = false; explicit Section() = default;
bool m_isNull = false;
int m_numPart = 0; int m_numPart = 0;
QString m_stringPart; QString m_stringPart;
QString m_fullString; QString m_fullString;
bool m_isNull;
inline bool operator!=(const Section &other) const inline bool operator==(const Section& other) const
{ {
if (m_isNull && other.numValid) { if (m_isNull && !other.m_isNull)
return 0 != other.m_numPart; return other.m_numPart == 0;
} else if (numValid && other.m_isNull) {
return m_numPart != 0; if (!m_isNull && other.m_isNull)
} else if (m_isNull || other.m_isNull) { return m_numPart == 0;
if ((m_stringPart == ".") || (other.m_stringPart == ".")) return false;
return true; if (m_isNull || other.m_isNull)
} return (m_stringPart == ".") || (other.m_stringPart == ".");
if(numValid && other.numValid)
{ if (!m_isNull && !other.m_isNull)
return m_numPart != other.m_numPart || m_stringPart != other.m_stringPart; return (m_numPart == other.m_numPart) && (m_stringPart == other.m_stringPart);
}
else return m_fullString == other.m_fullString;
{
return m_fullString != other.m_fullString;
}
} }
inline bool operator<(const Section &other) const inline bool operator<(const Section &other) const
{ {
if (m_isNull && other.numValid) { if (m_isNull && !other.m_isNull)
return 0 < other.m_numPart; return other.m_numPart > 0;
} else if (numValid && other.m_isNull) {
if (!m_isNull && other.m_isNull)
return m_numPart < 0; return m_numPart < 0;
} else if (m_isNull || other.m_isNull) {
if (m_isNull || other.m_isNull)
return true; return true;
}
if(numValid && other.numValid) if (!m_isNull && !other.m_isNull) {
{
if(m_numPart < other.m_numPart) if(m_numPart < other.m_numPart)
return true; return true;
if(m_numPart == other.m_numPart && m_stringPart < other.m_stringPart) if(m_numPart == other.m_numPart && m_stringPart < other.m_stringPart)
return true; return true;
return false; return false;
} }
else
{ return m_fullString < other.m_fullString;
return m_fullString < other.m_fullString; }
}
inline bool operator!=(const Section& other) const
{
return !(*this == other);
} }
inline bool operator>(const Section &other) const inline bool operator>(const Section &other) const
{ {
if (m_isNull && other.numValid) { return !(*this < other || *this == other);
return 0 > other.m_numPart;
} else if (numValid && other.m_isNull) {
return m_numPart > 0;
} else if (m_isNull || other.m_isNull) {
return false;
}
if(numValid && other.numValid)
{
if(m_numPart > other.m_numPart)
return true;
if(m_numPart == other.m_numPart && m_stringPart > other.m_stringPart)
return true;
return false;
}
else
{
return m_fullString > other.m_fullString;
}
} }
}; };
private:
QString m_string;
QList<Section> m_sections; QList<Section> m_sections;
void parse(); void parse();