diff options
author | flow <flowlnlnln@gmail.com> | 2023-01-20 11:11:35 -0300 |
---|---|---|
committer | flow <flowlnlnln@gmail.com> | 2023-01-21 18:47:47 -0300 |
commit | 445f9e5f717bf1ad9b764704b320bbec237a7682 (patch) | |
tree | 2f18d4410fb7b2b7227f55047435f43f18e53a95 /launcher/Version.h | |
parent | bcebb1920ff5df4f2a311984b296bfd8d5969997 (diff) | |
download | PrismLauncher-445f9e5f717bf1ad9b764704b320bbec237a7682.tar.gz PrismLauncher-445f9e5f717bf1ad9b764704b320bbec237a7682.tar.bz2 PrismLauncher-445f9e5f717bf1ad9b764704b320bbec237a7682.zip |
feat+fix(Version): make comparsion FlexVer-compatible
... and fixes a minor issue in the parsing.
This changes the expected behavior of Versions in one significant way:
Now, Versions like 1.2 or 1.5 evaluate to LESS THAN 1.2.0 and 1.5.0
respectively. This makes sense for sorting versions, since one expects
the versions without patch release to 'contain' the ones with, so the
ones without should be evaluated uniformily with the ones with the
patch.
Signed-off-by: flow <flowlnlnln@gmail.com>
Diffstat (limited to 'launcher/Version.h')
-rw-r--r-- | launcher/Version.h | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/launcher/Version.h b/launcher/Version.h index 23481c29..659f8e54 100644 --- a/launcher/Version.h +++ b/launcher/Version.h @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only /* * PolyMC - Minecraft Launcher + * Copyright (C) 2023 flowln <flowlnlnln@gmail.com> * Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net> * * This program is free software: you can redistribute it and/or modify @@ -60,7 +61,7 @@ class Version { private: struct Section { - explicit Section(QString fullString) : m_isNull(true), m_fullString(std::move(fullString)) + explicit Section(QString fullString) : m_fullString(std::move(fullString)) { int cutoff = m_fullString.size(); for (int i = 0; i < m_fullString.size(); i++) { @@ -95,45 +96,54 @@ class Version { explicit Section() = default; - bool m_isNull = false; - int m_numPart = 0; + bool m_isNull = true; + int m_numPart = 0; QString m_stringPart; + QString m_fullString; + [[nodiscard]] inline bool isAppendix() const { return m_stringPart.startsWith('+'); } + [[nodiscard]] inline bool isPreRelease() const { return m_stringPart.startsWith('-') && m_stringPart.length() > 1; } + inline bool operator==(const Section& other) const { if (m_isNull && !other.m_isNull) - return other.m_numPart == 0; - + return false; if (!m_isNull && other.m_isNull) - return m_numPart == 0; - - if (m_isNull || other.m_isNull) - return (m_stringPart == ".") || (other.m_stringPart == "."); + return false; - if (!m_isNull && !other.m_isNull) + if (!m_isNull && !other.m_isNull) { return (m_numPart == other.m_numPart) && (m_stringPart == other.m_stringPart); + } - return m_fullString == other.m_fullString; + return true; } - inline bool operator<(const Section &other) const + inline bool operator<(const Section& other) const { - if (m_isNull && !other.m_isNull) - return other.m_numPart > 0; + static auto unequal_is_less = [](Section const& non_null) -> bool { + if (non_null.m_stringPart.isEmpty()) + return non_null.m_numPart == 0; + return (non_null.m_stringPart != QLatin1Char('.')) && non_null.isPreRelease(); + }; if (!m_isNull && other.m_isNull) - return m_numPart < 0; - - if (m_isNull || other.m_isNull) - return true; + return unequal_is_less(*this); + if (m_isNull && !other.m_isNull) + return !unequal_is_less(other); if (!m_isNull && !other.m_isNull) { - if(m_numPart < other.m_numPart) + if (m_numPart < other.m_numPart) + return true; + if (m_numPart == other.m_numPart && m_stringPart < other.m_stringPart) return true; - if(m_numPart == other.m_numPart && m_stringPart < other.m_stringPart) + + if (!m_stringPart.isEmpty() && other.m_stringPart.isEmpty()) + return false; + if (m_stringPart.isEmpty() && !other.m_stringPart.isEmpty()) return true; + return false; } |