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