/
NuGetVersionFactory.cs
315 lines (275 loc) · 11.3 KB
/
NuGetVersionFactory.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
namespace NuGet.Versioning
{
public partial class NuGetVersion
{
/// <summary>
/// Creates a NuGetVersion from a string representing the semantic version.
/// </summary>
public new static NuGetVersion Parse(string value)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Argument_Cannot_Be_Null_Or_Empty, value), nameof(value));
}
if (!TryParse(value, out NuGetVersion? ver))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Invalidvalue, value), nameof(value));
}
return ver;
}
/// <summary>
/// Parses a version string using loose semantic versioning rules that allows 2-4 version components followed
/// by an optional special version.
/// </summary>
public static bool TryParse(string? value, [NotNullWhen(true)] out NuGetVersion? version)
{
version = null;
if (value != null)
{
Version? systemVersion;
// trim the value before passing it in since we not strict here
ParseSections(value.Trim(), out string? versionString, out string[]? releaseLabels, out string? buildMetadata);
// null indicates the string did not meet the rules
if (!string.IsNullOrEmpty(versionString))
{
string versionPart = versionString!;
if (TryGetNormalizedVersion(versionPart, out systemVersion))
{
// labels
if (releaseLabels != null)
{
for (int i = 0; i < releaseLabels.Length; i++)
{
if (!IsValidPart(releaseLabels[i], allowLeadingZeros: false))
{
return false;
}
}
}
// build metadata
if (buildMetadata != null
&& !IsValid(buildMetadata, allowLeadingZeros: true))
{
return false;
}
var originalVersion = value;
if (IndexOf(originalVersion, ' ') > -1)
{
originalVersion =
#if NETCOREAPP2_0_OR_GREATER
value.Replace(" ", string.Empty, StringComparison.Ordinal);
#else
value.Replace(" ", string.Empty);
#endif
}
version = new NuGetVersion(version: systemVersion,
releaseLabels: releaseLabels,
metadata: buildMetadata ?? string.Empty,
originalVersion: originalVersion);
return true;
}
}
}
return false;
int IndexOf(string str, char c)
{
#if NETCOREAPP2_1_OR_GREATER
return str.IndexOf(c, StringComparison.Ordinal);
#else
return str.IndexOf(c);
#endif
}
}
private static bool TryGetNormalizedVersion(string str, [NotNullWhen(true)] out Version? version)
{
if (string.IsNullOrWhiteSpace(str))
{
version = null;
return false;
}
int minor = 0;
int build = 0;
int revision = 0;
// Check for all the possible parts of the version string. If lastParsedPosition is less than the end of
// the string, the input string was invalid (e.g. "1.2.3.4.5").
bool success = ParseSection(str, 0, out int lastParsedPosition, out int major) &&
ParseSection(str, lastParsedPosition, out lastParsedPosition, out minor) &&
ParseSection(str, lastParsedPosition, out lastParsedPosition, out build) &&
ParseSection(str, lastParsedPosition, out lastParsedPosition, out revision) &&
lastParsedPosition == str.Length;
if (success)
{
version = new Version(major, minor, build, revision);
return true;
}
else
{
version = null;
return false;
}
// Returns false if an invalid section was found while processing the string.
static bool ParseSection(string str, int start, out int end, out int versionNumber)
{
// Section is empty.
if (start == str.Length)
{
end = start;
versionNumber = 0;
return true;
}
// Trim off leading whitespace
for (end = start; end < str.Length; ++end)
{
char currentChar = str[end];
if (!char.IsWhiteSpace(currentChar))
{
if (IsDigit(currentChar))
{
break;
}
else
{
// Found a non-whitespace non-digit character. Invalid string.
versionNumber = 0;
return false;
}
}
}
bool done = false;
bool digitFound = false;
long intermediateVersionNumber = 0;
// Handle number portion.
for (; end < str.Length; ++end)
{
// Negative numbers are invalid for version strings so we only need to check for digits.
char currentChar = str[end];
if (IsDigit(currentChar))
{
// Parse the values digit by digit and multiplies by 10 to make space for the next digit.
// When parsing "123456", this method becomes 1 -> 10 + 2 -> 120 + 3 -> 1230 + 4 -> 12340 + 5 -> 123450 + 6 -> 123456
// We subtract off ASCII value of '0' from our current character to get the digit's value
// e.g. '3' - '0' == 51 - 48 == 3
digitFound = true;
intermediateVersionNumber = intermediateVersionNumber * 10 + currentChar - '0';
// Check for overflow. We can't get outside the bounds of intermediateVersionNumber, a long, before exceeding int.MaxValue
// Intentionally avoid usage of 'checked' statement to avoid exception
if (intermediateVersionNumber > int.MaxValue)
{
versionNumber = 0;
return false;
}
}
else if (currentChar == '.')
{
++end;
// version string ended with '.'
if (end == str.Length)
{
versionNumber = 0;
return false;
}
done = true;
break;
}
else if (char.IsWhiteSpace(currentChar))
{
break;
}
else
{
versionNumber = 0;
return false;
}
}
// We failed to find a number in the section, so the string is invalid.
if (!digitFound)
{
versionNumber = 0;
return false;
}
if (end == str.Length)
{
done = true;
}
if (!done)
{
// trailing whitespace
for (; end < str.Length; ++end)
{
char currentChar = str[end];
if (!char.IsWhiteSpace(currentChar))
{
if (currentChar == '.')
{
++end;
// version string ended with '.'
if (end == str.Length)
{
versionNumber = 0;
return false;
}
break;
}
else
{
versionNumber = 0;
return false;
}
}
}
}
// Previous checks guarantee returnValue <= int.MaxValue
versionNumber = (int)intermediateVersionNumber;
return true;
}
static bool IsDigit(char c)
{
return c >= '0' && c <= '9';
}
}
/// <summary>
/// Parses a version string using strict SemVer rules.
/// </summary>
public static bool TryParseStrict(string value, [NotNullWhen(true)] out NuGetVersion? version)
{
version = null;
if (TryParse(value, out SemanticVersion? semVer))
{
version = new NuGetVersion(semVer.Major, semVer.Minor, semVer.Patch, 0, semVer.ReleaseLabels, semVer.Metadata);
return true;
}
return false;
}
/// <summary>
/// Creates a legacy version string using System.Version
/// </summary>
private static string GetLegacyString(Version version, IEnumerable<string>? releaseLabels, string? metadata)
{
var sb = new StringBuilder(version.ToString());
if (releaseLabels != null)
{
sb.AppendFormat(CultureInfo.InvariantCulture, "-{0}", string.Join(".", releaseLabels));
}
if (!string.IsNullOrEmpty(metadata))
{
sb.AppendFormat(CultureInfo.InvariantCulture, "+{0}", metadata);
}
return sb.ToString();
}
private static IEnumerable<string>? ParseReleaseLabels(string? releaseLabels)
{
if (!string.IsNullOrEmpty(releaseLabels))
{
return releaseLabels!.Split('.');
}
return null;
}
}
}