-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVersion.cpp
More file actions
89 lines (72 loc) · 1.87 KB
/
Copy pathVersion.cpp
File metadata and controls
89 lines (72 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
*
* @file Version.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*/
#include <vix/cli/util/Version.hpp>
#include <algorithm>
#include <cctype>
#include <stdexcept>
namespace vix::cli::util
{
std::vector<int> parseVersionParts(const std::string &version)
{
std::vector<int> parts;
std::string current;
for (char c : version)
{
if (c == '.')
{
if (!current.empty())
{
parts.push_back(std::stoi(current));
current.clear();
}
continue;
}
if (!std::isdigit(static_cast<unsigned char>(c)))
throw std::runtime_error("invalid version segment: " + version);
current += c;
}
if (!current.empty())
parts.push_back(std::stoi(current));
return parts;
}
int compareVersions(const std::string &lhs, const std::string &rhs)
{
const std::vector<int> left = parseVersionParts(lhs);
const std::vector<int> right = parseVersionParts(rhs);
const std::size_t maxSize = std::max(left.size(), right.size());
for (std::size_t i = 0; i < maxSize; ++i)
{
const int l = (i < left.size()) ? left[i] : 0;
const int r = (i < right.size()) ? right[i] : 0;
if (l < r)
return -1;
if (l > r)
return 1;
}
return 0;
}
bool isVersionGreater(const std::string &lhs, const std::string &rhs)
{
return compareVersions(lhs, rhs) > 0;
}
std::string findLatestVersionFromJsonObjectKeys(const std::vector<std::string> &versions)
{
std::string best;
for (const std::string &version : versions)
{
if (best.empty() || isVersionGreater(version, best))
best = version;
}
return best;
}
}