forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersions.cpp
More file actions
187 lines (161 loc) · 4.6 KB
/
Copy pathVersions.cpp
File metadata and controls
187 lines (161 loc) · 4.6 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerVersions.h"
namespace AppInstaller::Utility
{
Version::Version(std::string&& version, std::string_view splitChars) :
m_version(std::move(version))
{
size_t pos = 0;
while (pos < m_version.length())
{
size_t newPos = m_version.find_first_of(splitChars, pos);
size_t length = (newPos == std::string::npos ? m_version.length() : newPos) - pos;
m_parts.emplace_back(m_version.substr(pos, length));
pos += length + 1;
}
// Remove trailing empty versions (0 or empty)
while (!m_parts.empty())
{
const Part& part = m_parts.back();
if (part.Integer == 0 && part.Other.empty())
{
m_parts.pop_back();
}
else
{
break;
}
}
}
bool Version::operator<(const Version& other) const
{
for (size_t i = 0; i < m_parts.size(); ++i)
{
if (i >= other.m_parts.size())
{
// All parts equal to this point
break;
}
const Part& partA = m_parts[i];
const Part& partB = other.m_parts[i];
if (partA < partB)
{
return true;
}
else if (partB < partA)
{
return false;
}
// else parts are equal, so continue to next part
}
// All parts tested were equal, so this is only less if there are more parts in other.
return m_parts.size() < other.m_parts.size();
}
bool Version::operator>(const Version& other) const
{
return other < *this;
}
bool Version::operator<=(const Version& other) const
{
return !(*this > other);
}
bool Version::operator>=(const Version& other) const
{
return !(*this < other);
}
bool Version::operator==(const Version& other) const
{
if (m_parts.size() != other.m_parts.size())
{
return false;
}
for (size_t i = 0; i < m_parts.size(); ++i)
{
if (m_parts[i] != other.m_parts[i])
{
return false;
}
}
return true;
}
bool Version::operator!=(const Version& other) const
{
return !(*this == other);
}
Version::Part::Part(const std::string& part)
{
size_t end = 0;
try
{
Integer = std::stoull(part, &end);
}
CATCH_LOG();
if (end != part.length())
{
Other = part.substr(end);
}
}
bool Version::Part::operator<(const Part& other) const
{
if (Integer < other.Integer)
{
return true;
}
else if (Integer > other.Integer)
{
return false;
}
else if (Other < other.Other)
{
return true;
}
// else Other >= other.Other
return false;
}
bool Version::Part::operator==(const Part& other) const
{
return Integer == other.Integer && Other == other.Other;
}
bool Version::Part::operator!=(const Part& other) const
{
return !(*this == other);
}
bool Channel::operator<(const Channel& other) const
{
return m_channel < other.m_channel;
}
VersionAndChannel::VersionAndChannel(Version&& version, Channel&& channel) :
m_version(std::move(version)), m_channel(std::move(channel)) {}
std::string VersionAndChannel::ToString() const
{
std::string result;
result = m_version.ToString();
if (!m_channel.ToString().empty())
{
result += '[';
result += m_channel.ToString();
result += ']';
}
return result;
}
bool VersionAndChannel::operator<(const VersionAndChannel& other) const
{
if (m_channel < other.m_channel)
{
return true;
}
else if (other.m_channel < m_channel)
{
return false;
}
// We intentionally invert the order for version here.
else if (other.m_version < m_version)
{
return true;
}
// else m_verson >= other.m_version
return false;
}
}