forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemVersion.cs
More file actions
282 lines (234 loc) · 8.07 KB
/
Copy pathSemVersion.cs
File metadata and controls
282 lines (234 loc) · 8.07 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
namespace UnityEditor.Scripting.ScriptCompilation
{
internal struct SemVersion : IComparable<SemVersion>, IComparable
{
public bool IsInitialized { get; }
public int Major { get; }
public int Minor { get; }
public int Patch { get; }
public string Prerelease { get; }
public string Build { get; }
public SemVersion(int major, int minor = 0, int patch = 0, string prerelease = "", string build = "")
{
Major = major;
Minor = minor;
Patch = patch;
Prerelease = prerelease ?? "";
Build = build ?? "";
IsInitialized = true;
}
public static int Compare(SemVersion versionA, SemVersion versionB)
{
return versionA.CompareTo(versionB);
}
public static bool operator==(SemVersion left, SemVersion right)
{
return Equals(left, right);
}
public static bool operator!=(SemVersion left, SemVersion right)
{
return !Equals(left, right);
}
public static bool operator>(SemVersion left, SemVersion right)
{
return Compare(left, right) > 0;
}
public static bool operator>=(SemVersion left, SemVersion right)
{
return left == right || left > right;
}
public static bool operator<(SemVersion left, SemVersion right)
{
return Compare(left, right) < 0;
}
public static bool operator<=(SemVersion left, SemVersion right)
{
return left == right || left < right;
}
public int CompareTo(object obj)
{
return CompareTo((SemVersion)obj);
}
public int CompareTo(SemVersion other)
{
var result = Major.CompareTo(other.Major);
if (result != 0)
{
return result;
}
result = Minor.CompareTo(other.Minor);
if (result != 0)
{
return result;
}
result = Patch.CompareTo(other.Patch);
if (result != 0)
{
return result;
}
result = CompareExtension(Prerelease, other.Prerelease, true);
if (result != 0)
{
return result;
}
return 0;
}
private static int CompareExtension(string current, string other, bool lower = false)
{
var currentIsEmpty = string.IsNullOrEmpty(current);
var otherIsEmpty = string.IsNullOrEmpty(other);
if (currentIsEmpty && otherIsEmpty)
return 0;
if (currentIsEmpty)
return lower ? 1 : -1;
if (otherIsEmpty)
return lower ? -1 : 1;
var currentParts = current.Split('.');
var otherParts = other.Split('.');
for (var i = 0; i < Math.Min(currentParts.Length, otherParts.Length); i++)
{
var currentPart = currentParts[i];
var otherPart = otherParts[i];
int currentNumber;
int otherNumber;
var currentPartIsNumber = int.TryParse(currentPart, out currentNumber);
var otherPartIsNumber = int.TryParse(otherPart, out otherNumber);
int result;
if (currentPartIsNumber && otherPartIsNumber)
{
result = currentNumber.CompareTo(otherNumber);
if (result != 0)
{
return result;
}
}
else
{
if (currentPartIsNumber)
{
return -1;
}
if (otherPartIsNumber)
{
return 1;
}
result = string.CompareOrdinal(currentPart, otherPart);
if (result != 0)
{
return result;
}
}
}
return currentParts.Length.CompareTo(otherParts.Length);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(obj, null))
return false;
var other = (SemVersion)obj;
return Major == other.Major &&
Minor == other.Minor &&
Patch == other.Patch &&
string.Equals(Prerelease, other.Prerelease, StringComparison.Ordinal);
}
public override string ToString()
{
var version = $"{Major}.{Minor}.{Patch}";
if (!string.IsNullOrEmpty(Prerelease))
{
version += $"-{Prerelease}";
}
if (!string.IsNullOrEmpty(Build))
{
version += $"+{Build}";
}
return version;
}
public override int GetHashCode()
{
unchecked
{
int result = Major.GetHashCode();
result = result * 31 + Minor.GetHashCode();
result = result * 31 + Patch.GetHashCode();
result = result * 31 + Prerelease.GetHashCode();
result = result * 31 + Build.GetHashCode();
return result;
}
}
}
internal static class SemVersionParser
{
private static string GetStringUntil(string value, ref int cursor, Func<char, bool> isEnd)
{
int length = 0;
for (int i = cursor; i < value.Length; i++)
{
if (isEnd(value[i]))
{
break;
}
length++;
}
int newIndex = cursor;
cursor += length;
return value.Substring(newIndex, length);
}
public static SemVersion Parse(string version, bool strict = false)
{
int cursor = 0;
int major = 0;
int minor = 0;
int patch = 0;
string prerelease = null;
string build = null;
//Doing this instead because RegEx is impressively slow
try
{
major = int.Parse(GetStringUntil(version, ref cursor, x => !char.IsDigit(x)));
if (cursor < version.Length && version[cursor] == '.')
{
cursor++;
minor = int.Parse(GetStringUntil(version, ref cursor, x => !char.IsDigit(x)) ?? "0");
}
if (cursor < version.Length && version[cursor] == '.')
{
cursor++;
patch = int.Parse(GetStringUntil(version, ref cursor, x => !char.IsDigit(x)) ?? "0");
}
if (cursor < version.Length && version[cursor] == '-')
{
cursor++;
prerelease = GetStringUntil(version, ref cursor, x => x == '+');
}
if (cursor < version.Length && version[cursor] == '+')
{
cursor++;
build = GetStringUntil(version, ref cursor, x => x == '\0');
}
}
catch (Exception)
{
throw new ArgumentException($"{version} is not valid Semantic Version");
}
return new SemVersion(major, minor, patch, prerelease, build);
}
public static bool TryParse(string versionString, out SemVersion? result)
{
try
{
result = SemVersionParser.Parse(versionString);
return true;
}
catch (Exception)
{
result = null;
return false;
}
}
}
}