-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
FXVersion.cs
330 lines (286 loc) · 10.8 KB
/
FXVersion.cs
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// dotnet doesn't have nullable enabled
#pragma warning disable IDE0240 // Remove redundant nullable directive
#nullable disable
#pragma warning restore IDE0240 // Remove redundant nullable directive
namespace Microsoft.DotNet.MSBuildSdkResolver
{
// Note: This is SemVer 2.0.0 https://semver.org/spec/v2.0.0.html
// See the original version of this code here: https://github.com/dotnet/core-setup/blob/master/src/corehost/cli/fxr/fx_ver.cpp
internal sealed class FXVersion
{
public int Major { get; }
public int Minor { get; }
public int Patch { get; }
public string Pre { get; }
public string Build { get; }
public FXVersion(int major, int minor, int patch, string pre = "", string build = "")
{
Major = major;
Minor = minor;
Patch = patch;
Pre = pre;
Build = build;
}
private static string GetId(string ids, int idStart)
{
int next = ids.IndexOf('.', idStart);
return next == -1 ? ids.Substring(idStart) : ids.Substring(idStart, next - idStart);
}
public static int Compare(FXVersion s1, FXVersion s2)
{
// compare(u.v.w-p+b, x.y.z-q+c)
if (s1.Major != s2.Major)
{
return s1.Major > s2.Major ? 1 : -1;
}
if (s1.Minor != s2.Minor)
{
return s1.Minor > s2.Minor ? 1 : -1;
}
if (s1.Patch != s2.Patch)
{
return s1.Patch > s2.Patch ? 1 : -1;
}
if (string.IsNullOrEmpty(s1.Pre) || string.IsNullOrEmpty(s2.Pre))
{
// Empty (release) is higher precedence than prerelease
return string.IsNullOrEmpty(s1.Pre) ? (string.IsNullOrEmpty(s2.Pre) ? 0 : 1) : -1;
}
// Both are non-empty (may be equal)
// First character of pre is '-' when it is not empty
// First idenitifier starts at position 1
int idStart = 1;
for (int i = idStart; true; ++i)
{
// C# strings are not null terminated. Pretend to make code similar to fx_ver.cpp
char s1char = (s1.Pre.Length == i) ? '\0' : s1.Pre[i];
char s2char = (s2.Pre.Length == i) ? '\0' : s2.Pre[i];
if (s1char != s2char)
{
// Found first character with a difference
if (s1char == '\0' && s2char == '.')
{
// identifiers both complete, b has an additional identifier
return -1;
}
if (s2char == '\0' && s1char == '.')
{
// identifiers both complete, a has an additional identifier
return 1;
}
// identifiers must not be empty
string id1 = GetId(s1.Pre, idStart);
string id2 = GetId(s2.Pre, idStart);
int id1num = 0;
bool id1IsNum = int.TryParse(id1, out id1num);
int id2num = 0;
bool id2IsNum = int.TryParse(id2, out id2num);
if (id1IsNum && id2IsNum)
{
// Numeric comparison
return (id1num > id2num) ? 1 : -1;
}
else if (id1IsNum || id2IsNum)
{
// Mixed compare. Spec: Number < Text
return id2IsNum ? 1 : -1;
}
// Ascii compare
// Since we are using only ascii characters, unicode ordinal sort == ascii sort
return (s1char > s2char) ? 1 : -1;
}
else
{
// s1char == s2char
if (s1char == '\0')
{
break;
}
if (s1char == '.')
{
idStart = i + 1;
}
}
}
return 0;
}
private static bool ValidIdentifierCharSet(string id)
{
// ids must be of the set [0-9a-zA-Z-]
// ASCII and Unicode ordering
for (int i = 0; i < id.Length; ++i)
{
if (id[i] >= 'A')
{
if ((id[i] > 'Z' && id[i] < 'a') || id[i] > 'z')
{
return false;
}
}
else
{
if ((id[i] < '0' && id[i] != '-') || id[i] > '9')
{
return false;
}
}
}
return true;
}
private static bool ValidIdentifier(string id, bool buildMeta)
{
if (string.IsNullOrEmpty(id))
{
// Identifier must not be empty
return false;
}
if (!ValidIdentifierCharSet(id))
{
// ids must be of the set [0-9a-zA-Z-]
return false;
}
int ignored;
if (!buildMeta && id[0] == '0' && id.Length > 1 && int.TryParse(id, out ignored))
{
// numeric identifiers must not be padded with 0s
return false;
}
return true;
}
private static bool ValidIdentifiers(string ids)
{
if (string.IsNullOrEmpty(ids))
{
return true;
}
bool prerelease = ids[0] == '-';
bool buildMeta = ids[0] == '+';
if (!(prerelease || buildMeta))
{
// ids must start with '-' or '+' for prerelease & build respectively
return false;
}
int idStart = 1;
int nextId;
while ((nextId = ids.IndexOf('.', idStart)) != -1)
{
if (!ValidIdentifier(ids.Substring(idStart, nextId - idStart), buildMeta))
{
return false;
}
idStart = nextId + 1;
}
if (!ValidIdentifier(ids.Substring(idStart), buildMeta))
{
return false;
}
return true;
}
private static int IndexOfNonNumeric(string s, int startIndex)
{
for (int i = startIndex; i < s.Length; ++i)
{
if ((s[i] < '0') || (s[i] > '9'))
{
return i;
}
}
return -1;
}
public static bool TryParse(string fxVersionString, out FXVersion FXVersion)
{
FXVersion = null;
if (string.IsNullOrEmpty(fxVersionString))
{
return false;
}
int majorSeparator = fxVersionString.IndexOf(".", StringComparison.Ordinal);
if (majorSeparator == -1)
{
return false;
}
int major = 0;
if (!int.TryParse(fxVersionString.Substring(0, majorSeparator), out major))
{
return false;
}
if (majorSeparator > 1 && fxVersionString[0] == '0')
{
// if leading character is 0, and strlen > 1
// then the numeric substring has leading zeroes which is prohibited by the specification.
return false;
}
int minorStart = majorSeparator + 1;
int minorSeparator = fxVersionString.IndexOf(".", minorStart, StringComparison.Ordinal);
if (minorSeparator == -1)
{
return false;
}
int minor = 0;
if (!int.TryParse(fxVersionString.Substring(minorStart, minorSeparator - minorStart), out minor))
{
return false;
}
if (minorSeparator - minorStart > 1 && fxVersionString[minorStart] == '0')
{
// if leading character is 0, and strlen > 1
// then the numeric substring has leading zeroes which is prohibited by the specification.
return false;
}
int patch = 0;
int patchStart = minorSeparator + 1;
int patchSeparator = IndexOfNonNumeric(fxVersionString, patchStart);
if (patchSeparator == -1)
{
if (!int.TryParse(fxVersionString.Substring(patchStart), out patch))
{
return false;
}
if (patchStart + 1 < fxVersionString.Length && fxVersionString[patchStart] == '0')
{
// if leading character is 0, and strlen != 1
// then the numeric substring has leading zeroes which is prohibited by the specification.
return false;
}
FXVersion = new FXVersion(major, minor, patch);
return true;
}
if (!int.TryParse(fxVersionString.Substring(patchStart, patchSeparator - patchStart), out patch))
{
return false;
}
if (patchSeparator - patchStart > 1 && fxVersionString[patchStart] == '0')
{
return false;
}
int preStart = patchSeparator;
int preSeparator = fxVersionString.IndexOf("+", preStart, StringComparison.Ordinal);
string pre = (preSeparator == -1) ? fxVersionString.Substring(preStart) : fxVersionString.Substring(preStart, preSeparator - preStart);
if (!ValidIdentifiers(pre))
{
return false;
}
string build = "";
if (preSeparator != -1)
{
build = fxVersionString.Substring(preSeparator);
if (!ValidIdentifiers(build))
{
return false;
}
}
FXVersion = new FXVersion(major, minor, patch, pre, build);
return true;
}
public override string ToString()
=> (!string.IsNullOrEmpty(Pre), !string.IsNullOrEmpty(Build)) switch
{
(false, false) => $"{Major}.{Minor}.{Patch}",
(true, false) => $"{Major}.{Minor}.{Patch}{Pre}",
(false, true) => $"{Major}.{Minor}.{Patch}{Build}",
(true, true) => $"{Major}.{Minor}.{Patch}{Pre}{Build}",
};
}
}