210 lines
6.4 KiB
C++
210 lines
6.4 KiB
C++
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "Version.hpp"
|
|
#include <fmt/format.h>
|
|
#include <charconv>
|
|
#include <sstream>
|
|
#include <regex>
|
|
|
|
namespace OpenVulkano
|
|
{
|
|
namespace
|
|
{
|
|
struct ComponentDecodeHolder
|
|
{
|
|
std::vector<uint32_t> versionComponents;
|
|
std::vector<std::string> tagComponents;
|
|
std::vector<uint32_t> appleBuildComponents;
|
|
|
|
ComponentDecodeHolder(const std::string_view& versionStr)
|
|
{
|
|
if (versionStr.empty()) return;
|
|
size_t offset = 0;
|
|
if (versionStr[0] == 'v' || versionStr[0] == 'V')
|
|
{
|
|
offset++;
|
|
if (versionStr.size() == 1) return;
|
|
}
|
|
ReadVersionComponents(versionComponents, versionStr, offset);
|
|
if (offset < versionStr.size() && versionStr[offset] == '-')
|
|
{
|
|
offset++;
|
|
ReadTagComponents(tagComponents, versionStr, offset);
|
|
}
|
|
if (versionStr.size() > 3 && versionStr.size() - 3 > offset && versionStr[offset] == ' ' && versionStr[offset + 1] == '(' && versionStr.back() == ')')
|
|
{
|
|
ReadVersionComponents(appleBuildComponents, versionStr, offset);
|
|
if (versionStr[offset] != ')') appleBuildComponents.clear();
|
|
}
|
|
}
|
|
|
|
private:
|
|
static int ToNumber(const std::string_view numberStr)
|
|
{
|
|
if (numberStr.empty()) return 0;
|
|
int tmp = 0;
|
|
std::from_chars(numberStr.data(), numberStr.data() + numberStr.size(), tmp);
|
|
return tmp;
|
|
}
|
|
|
|
static void ReadVersionComponents(std::vector<uint32_t>& comps, const std::string_view& versionStr, size_t& offset)
|
|
{
|
|
do
|
|
{
|
|
const size_t blockStart = offset;
|
|
while (versionStr.size() > offset && versionStr[offset] >= '0' && versionStr[offset] <= '9') offset++;
|
|
comps.push_back(ToNumber(versionStr.substr(blockStart, offset - blockStart)));
|
|
if (offset < versionStr.size() && versionStr[offset] != '.') break;
|
|
offset++;
|
|
} while(versionStr.size() > offset);
|
|
}
|
|
|
|
static void ReadTagComponents(std::vector<std::string>& tags, const std::string_view& versionStr, size_t& offset)
|
|
{
|
|
do
|
|
{
|
|
const size_t blockStart = offset;
|
|
while (versionStr.size() > offset && versionStr[offset] != '-' && versionStr[offset] != ' ' && versionStr[offset] != '\0') offset++;
|
|
tags.emplace_back(versionStr.data() + blockStart, offset - blockStart);
|
|
if (offset >= versionStr.size() || versionStr[offset] == ' ' || versionStr[offset] == '\0') break;
|
|
offset++;
|
|
} while (versionStr.size() > offset);
|
|
}
|
|
};
|
|
|
|
std::pair<char, uint64_t> ReadTagValue(const std::string& str)
|
|
{
|
|
std::pair<char, uint64_t> p;
|
|
std::istringstream iss(str);
|
|
iss >> p.first >> p.second;
|
|
return p;
|
|
}
|
|
|
|
constexpr std::string ToString(uint32_t major, uint32_t minor, uint32_t patch, const uint32_t build)
|
|
{
|
|
if (build)
|
|
return fmt::format("v{}.{}.{}.{}", major, minor, patch, build);
|
|
return fmt::format("v{}.{}.{}", major, minor, patch);
|
|
}
|
|
}
|
|
|
|
VersionDataExtendedData::VersionDataExtendedData(const std::string_view& versionString, const bool ignoreTags)
|
|
{
|
|
ComponentDecodeHolder helper(versionString);
|
|
if (!helper.appleBuildComponents.empty())
|
|
while (helper.versionComponents.size() < 3) helper.versionComponents.push_back(0);
|
|
if (helper.versionComponents.size() == 4) m_buildNumber = helper.versionComponents[3];
|
|
m_versionComponents = std::move(helper.versionComponents);
|
|
for (auto val : helper.appleBuildComponents) { m_versionComponents.push_back(val); }
|
|
if (!ignoreTags)
|
|
{
|
|
m_tagComponents = std::move(helper.tagComponents);
|
|
const auto tagRegex = std::regex("[bBtT][0-9]+");
|
|
for (const std::string& tag : m_tagComponents)
|
|
{
|
|
if (std::regex_match(tag, tagRegex))
|
|
{
|
|
const auto [fst, snd] = ReadTagValue(tag);
|
|
if (fst == 't' || fst == 'T')
|
|
m_timestamp = snd;
|
|
else
|
|
m_buildNumber = snd;
|
|
}
|
|
}
|
|
}
|
|
if (m_tagComponents.empty() && !helper.appleBuildComponents.empty())
|
|
{
|
|
if (helper.appleBuildComponents.size() == 3)
|
|
{
|
|
m_timestamp = helper.appleBuildComponents[1] * 1000000uLL + helper.appleBuildComponents[2];
|
|
}
|
|
m_buildNumber = helper.appleBuildComponents[0];
|
|
}
|
|
}
|
|
|
|
Version::Version(uint32_t major, uint32_t minor, uint32_t patch, const uint32_t build)
|
|
: m_data(VersionDataCompact(major, minor, patch, build))
|
|
, m_versionString(ToString(major, minor, patch, build))
|
|
{}
|
|
|
|
Version::Version(const std::string_view versionString, const bool ignoreTags)
|
|
: m_versionString(versionString)
|
|
{
|
|
VersionDataExtendedData data(versionString, ignoreTags);
|
|
auto compact = data.GetCompactVersion();
|
|
if (compact)
|
|
m_data = compact.value();
|
|
else
|
|
m_data = std::move(data);
|
|
}
|
|
|
|
//region Compare functions
|
|
int Version::CompareBuildNr(const Version& other) const
|
|
{
|
|
const uint64_t build = Build(), otherBuild = other.Build();
|
|
if (build == 0 || otherBuild == 0) return 0;
|
|
if (build > otherBuild) return 1;
|
|
if (build < otherBuild) return -1;
|
|
return 0;
|
|
}
|
|
|
|
int Version::CompareTimestamp(const Version& other) const
|
|
{
|
|
const uint64_t ts = Timestamp(), oTs = other.Timestamp();
|
|
if (ts == 0 || oTs == 0) return 0;
|
|
if (ts > oTs) return 1;
|
|
if (ts < oTs) return -1;
|
|
return 0;
|
|
}
|
|
|
|
int Version::CompareComponents(const Version& other) const
|
|
{
|
|
const auto comp = GetVersionComponents();
|
|
const auto otherComp = other.GetVersionComponents();
|
|
const size_t minCount = std::min(comp.size(), otherComp.size());
|
|
for(size_t i = 0; i < minCount; i++)
|
|
{
|
|
if (comp[i] > otherComp[i]) return 1;
|
|
if (comp[i] < otherComp[i]) return -1;
|
|
}
|
|
if (comp.size() != otherComp.size())
|
|
{
|
|
for(size_t i = minCount; i < std::max(comp.size(), otherComp.size()); i++)
|
|
{
|
|
if (GetComponent(i) > other.GetComponent(i)) return 1;
|
|
if (GetComponent(i) < other.GetComponent(i)) return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int Version::Compare(const Version& other) const
|
|
{
|
|
int comp;
|
|
if ((comp = CompareComponents(other)) != 0) return comp;
|
|
if ((comp = CompareBuildNr(other)) != 0) return comp;
|
|
return CompareTimestamp(other);
|
|
}
|
|
//endregion
|
|
|
|
namespace
|
|
{
|
|
uint32_t GetDigits(const double val)
|
|
{
|
|
if (val == 0) return 0;
|
|
std::string fracStr = std::to_string(val);
|
|
fracStr.erase(fracStr.find_last_not_of('0') + 1, std::string::npos);
|
|
fracStr.erase(0, fracStr.find_first_of('.') + 1);
|
|
return std::stoul(fracStr);
|
|
}
|
|
}
|
|
|
|
Version::Version(const double version)
|
|
: Version(static_cast<uint32_t>(version), GetDigits(version))
|
|
{}
|
|
}
|