-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathVersion.cs
More file actions
430 lines (354 loc) · 16 KB
/
Version.cs
File metadata and controls
430 lines (354 loc) · 16 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers.Text;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System
{
// A Version object contains four hierarchical numeric components: major, minor,
// build and revision. Build and revision may be unspecified, which is represented
// internally as a -1. By definition, an unspecified component matches anything
// (both unspecified and specified), and an unspecified component is "less than" any
// specified component.
[Serializable]
[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public sealed class Version : ICloneable, IComparable, IComparable<Version?>, IEquatable<Version?>, ISpanFormattable, IUtf8SpanFormattable
{
// AssemblyName depends on the order staying the same
private readonly int _Major; // Do not rename (binary serialization)
private readonly int _Minor; // Do not rename (binary serialization)
private readonly int _Build; // Do not rename (binary serialization)
private readonly int _Revision; // Do not rename (binary serialization)
public Version(int major, int minor, int build, int revision)
{
ArgumentOutOfRangeException.ThrowIfNegative(major);
ArgumentOutOfRangeException.ThrowIfNegative(minor);
ArgumentOutOfRangeException.ThrowIfNegative(build);
ArgumentOutOfRangeException.ThrowIfNegative(revision);
_Major = major;
_Minor = minor;
_Build = build;
_Revision = revision;
}
public Version(int major, int minor, int build)
{
ArgumentOutOfRangeException.ThrowIfNegative(major);
ArgumentOutOfRangeException.ThrowIfNegative(minor);
ArgumentOutOfRangeException.ThrowIfNegative(build);
_Major = major;
_Minor = minor;
_Build = build;
_Revision = -1;
}
public Version(int major, int minor)
{
ArgumentOutOfRangeException.ThrowIfNegative(major);
ArgumentOutOfRangeException.ThrowIfNegative(minor);
_Major = major;
_Minor = minor;
_Build = -1;
_Revision = -1;
}
public Version(string version)
{
Version v = Parse(version);
_Major = v.Major;
_Minor = v.Minor;
_Build = v.Build;
_Revision = v.Revision;
}
public Version()
{
//_Major = 0;
//_Minor = 0;
_Build = -1;
_Revision = -1;
}
private Version(Version version)
{
Debug.Assert(version != null);
_Major = version._Major;
_Minor = version._Minor;
_Build = version._Build;
_Revision = version._Revision;
}
public object Clone()
{
return new Version(this);
}
// Properties for setting and getting version numbers
public int Major => _Major;
public int Minor => _Minor;
public int Build => _Build;
public int Revision => _Revision;
public short MajorRevision => (short)(_Revision >> 16);
public short MinorRevision => (short)(_Revision & 0xFFFF);
public int CompareTo(object? version)
{
if (version == null)
{
return 1;
}
if (version is Version v)
{
return CompareTo(v);
}
throw new ArgumentException(SR.Arg_MustBeVersion, nameof(version));
}
public int CompareTo(Version? value)
{
return
ReferenceEquals(value, this) ? 0 :
value is null ? 1 :
_Major != value._Major ? (_Major > value._Major ? 1 : -1) :
_Minor != value._Minor ? (_Minor > value._Minor ? 1 : -1) :
_Build != value._Build ? (_Build > value._Build ? 1 : -1) :
_Revision != value._Revision ? (_Revision > value._Revision ? 1 : -1) :
0;
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
return Equals(obj as Version);
}
public bool Equals([NotNullWhen(true)] Version? obj)
{
return ReferenceEquals(obj, this) ||
(!(obj is null) &&
_Major == obj._Major &&
_Minor == obj._Minor &&
_Build == obj._Build &&
_Revision == obj._Revision);
}
public override int GetHashCode()
{
// Let's assume that most version numbers will be pretty small and just
// OR some lower order bits together.
int accumulator = 0;
accumulator |= (_Major & 0x0000000F) << 28;
accumulator |= (_Minor & 0x000000FF) << 20;
accumulator |= (_Build & 0x000000FF) << 12;
accumulator |= (_Revision & 0x00000FFF);
return accumulator;
}
public override string ToString() =>
ToString(DefaultFormatFieldCount);
public string ToString(int fieldCount)
{
Span<char> dest = stackalloc char[(4 * Number.Int32NumberBufferLength) + 3]; // at most 4 Int32s and 3 periods
bool success = TryFormat(dest, fieldCount, out int charsWritten);
Debug.Assert(success);
return dest.Slice(0, charsWritten).ToString();
}
string IFormattable.ToString(string? format, IFormatProvider? formatProvider) =>
ToString();
public bool TryFormat(Span<char> destination, out int charsWritten) =>
TryFormatCore(destination, DefaultFormatFieldCount, out charsWritten);
public bool TryFormat(Span<char> destination, int fieldCount, out int charsWritten) =>
TryFormatCore(destination, fieldCount, out charsWritten);
/// <summary>Tries to format this version instance into a span of bytes.</summary>
/// <param name="utf8Destination">The span in which to write this instance's value formatted as a span of UTF-8 bytes.</param>
/// <param name="bytesWritten">When this method returns, contains the number of bytes that were written in <paramref name="utf8Destination"/>.</param>
/// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</returns>
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten) =>
TryFormatCore(utf8Destination, DefaultFormatFieldCount, out bytesWritten);
/// <summary>Tries to format this version instance into a span of bytes.</summary>
/// <param name="utf8Destination">The span in which to write this instance's value formatted as a span of UTF-8 bytes.</param>
/// <param name="fieldCount">The number of components to return. This value ranges from 0 to 4.</param>
/// <param name="bytesWritten">When this method returns, contains the number of bytes that were written in <paramref name="utf8Destination"/>.</param>
/// <returns><see langword="true"/> if the formatting was successful; otherwise, <see langword="false"/>.</returns>
public bool TryFormat(Span<byte> utf8Destination, int fieldCount, out int bytesWritten) =>
TryFormatCore(utf8Destination, fieldCount, out bytesWritten);
private bool TryFormatCore<TChar>(Span<TChar> destination, int fieldCount, out int charsWritten) where TChar : unmanaged, IUtfChar<TChar>
{
Debug.Assert(typeof(TChar) == typeof(char) || typeof(TChar) == typeof(byte));
switch ((uint)fieldCount)
{
case > 4:
ThrowArgumentException("4");
break;
case >= 3 when _Build == -1:
ThrowArgumentException("2");
break;
case 4 when _Revision == -1:
ThrowArgumentException("3");
break;
static void ThrowArgumentException(string failureUpperBound) =>
throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, "0", failureUpperBound), nameof(fieldCount));
}
int totalCharsWritten = 0;
for (int i = 0; i < fieldCount; i++)
{
if (i != 0)
{
if (destination.IsEmpty)
{
charsWritten = 0;
return false;
}
destination[0] = TChar.CastFrom('.');
destination = destination.Slice(1);
totalCharsWritten++;
}
int value = i switch
{
0 => _Major,
1 => _Minor,
2 => _Build,
_ => _Revision
};
int valueCharsWritten;
bool formatted = typeof(TChar) == typeof(char) ?
((uint)value).TryFormat(MemoryMarshal.Cast<TChar, char>(destination), out valueCharsWritten) :
((uint)value).TryFormat(MemoryMarshal.Cast<TChar, byte>(destination), out valueCharsWritten, default, CultureInfo.InvariantCulture);
if (!formatted)
{
charsWritten = 0;
return false;
}
totalCharsWritten += valueCharsWritten;
destination = destination.Slice(valueCharsWritten);
}
charsWritten = totalCharsWritten;
return true;
}
bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
// format and provider are ignored.
TryFormatCore(destination, DefaultFormatFieldCount, out charsWritten);
/// <inheritdoc cref="IUtf8SpanFormattable.TryFormat" />
bool IUtf8SpanFormattable.TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider) =>
// format and provider are ignored.
TryFormatCore(utf8Destination, DefaultFormatFieldCount, out bytesWritten);
private int DefaultFormatFieldCount =>
_Build == -1 ? 2 :
_Revision == -1 ? 3 :
4;
public static Version Parse(string input)
{
ArgumentNullException.ThrowIfNull(input);
return ParseVersion(input.AsSpan(), throwOnFailure: true)!;
}
public static Version Parse(ReadOnlySpan<char> input) =>
ParseVersion(input, throwOnFailure: true)!;
public static bool TryParse([NotNullWhen(true)] string? input, [NotNullWhen(true)] out Version? result)
{
if (input == null)
{
result = null;
return false;
}
return (result = ParseVersion(input.AsSpan(), throwOnFailure: false)) != null;
}
public static bool TryParse(ReadOnlySpan<char> input, [NotNullWhen(true)] out Version? result) =>
(result = ParseVersion(input, throwOnFailure: false)) != null;
private static Version? ParseVersion(ReadOnlySpan<char> input, bool throwOnFailure)
{
// Find the separator between major and minor. It must exist.
int majorEnd = input.IndexOf('.');
if (majorEnd < 0)
{
if (throwOnFailure) throw new ArgumentException(SR.Arg_VersionString, nameof(input));
return null;
}
// Find the ends of the optional minor and build portions.
// We musn't have any separators after build.
int buildEnd = -1;
int minorEnd = input.Slice(majorEnd + 1).IndexOf('.');
if (minorEnd >= 0)
{
minorEnd += (majorEnd + 1);
buildEnd = input.Slice(minorEnd + 1).IndexOf('.');
if (buildEnd >= 0)
{
buildEnd += (minorEnd + 1);
if (input.Slice(buildEnd + 1).Contains('.'))
{
if (throwOnFailure) throw new ArgumentException(SR.Arg_VersionString, nameof(input));
return null;
}
}
}
int minor, build, revision;
// Parse the major version
if (!TryParseComponent(input.Slice(0, majorEnd), nameof(input), throwOnFailure, out int major))
{
return null;
}
if (minorEnd != -1)
{
// If there's more than a major and minor, parse the minor, too.
if (!TryParseComponent(input.Slice(majorEnd + 1, minorEnd - majorEnd - 1), nameof(input), throwOnFailure, out minor))
{
return null;
}
if (buildEnd != -1)
{
// major.minor.build.revision
return
TryParseComponent(input.Slice(minorEnd + 1, buildEnd - minorEnd - 1), nameof(build), throwOnFailure, out build) &&
TryParseComponent(input.Slice(buildEnd + 1), nameof(revision), throwOnFailure, out revision) ?
new Version(major, minor, build, revision) :
null;
}
else
{
// major.minor.build
return TryParseComponent(input.Slice(minorEnd + 1), nameof(build), throwOnFailure, out build) ?
new Version(major, minor, build) :
null;
}
}
else
{
// major.minor
return TryParseComponent(input.Slice(majorEnd + 1), nameof(input), throwOnFailure, out minor) ?
new Version(major, minor) :
null;
}
}
private static bool TryParseComponent(ReadOnlySpan<char> component, string componentName, bool throwOnFailure, out int parsedComponent)
{
if (throwOnFailure)
{
parsedComponent = int.Parse(component, NumberStyles.Integer, CultureInfo.InvariantCulture);
ArgumentOutOfRangeException.ThrowIfNegative(parsedComponent, componentName);
return true;
}
return int.TryParse(component, NumberStyles.Integer, CultureInfo.InvariantCulture, out parsedComponent) && parsedComponent >= 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Version? v1, Version? v2)
{
// Test "right" first to allow branch elimination when inlined for null checks (== null)
// so it can become a simple test
if (v2 is null)
{
return v1 is null;
}
// Quick reference equality test prior to calling the virtual Equality
return ReferenceEquals(v2, v1) ? true : v2.Equals(v1);
}
public static bool operator !=(Version? v1, Version? v2) => !(v1 == v2);
public static bool operator <(Version? v1, Version? v2)
{
if (v1 is null)
{
return !(v2 is null);
}
return v1.CompareTo(v2) < 0;
}
public static bool operator <=(Version? v1, Version? v2)
{
if (v1 is null)
{
return true;
}
return v1.CompareTo(v2) <= 0;
}
public static bool operator >(Version? v1, Version? v2) => v2 < v1;
public static bool operator >=(Version? v1, Version? v2) => v2 <= v1;
}
}