/
FrameworkName.cs
213 lines (182 loc) · 7.2 KB
/
FrameworkName.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Runtime.Versioning
{
public sealed class FrameworkName : IEquatable<FrameworkName?>
{
private readonly string _identifier;
private readonly Version _version = null!;
private readonly string _profile;
private string? _fullName;
private const char ComponentSeparator = ',';
private const char KeyValueSeparator = '=';
private const char VersionValuePrefix = 'v';
private const string VersionKey = "Version";
private const string ProfileKey = "Profile";
public string Identifier
{
get
{
Debug.Assert(_identifier != null);
return _identifier;
}
}
public Version Version
{
get
{
Debug.Assert(_version != null);
return _version;
}
}
public string Profile
{
get
{
Debug.Assert(_profile != null);
return _profile;
}
}
public string FullName
{
get
{
_fullName ??= string.IsNullOrEmpty(Profile) ?
$"{Identifier}{ComponentSeparator + VersionKey + KeyValueSeparator + VersionValuePrefix}{Version}" :
$"{Identifier}{ComponentSeparator + VersionKey + KeyValueSeparator + VersionValuePrefix}{Version}{ComponentSeparator + ProfileKey + KeyValueSeparator}{Profile}";
Debug.Assert(_fullName != null);
return _fullName;
}
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
return Equals(obj as FrameworkName);
}
public bool Equals([NotNullWhen(true)] FrameworkName? other)
{
if (other is null)
{
return false;
}
return Identifier == other.Identifier &&
Version == other.Version &&
Profile == other.Profile;
}
public override int GetHashCode()
{
return Identifier.GetHashCode() ^ Version.GetHashCode() ^ Profile.GetHashCode();
}
public override string ToString()
{
return FullName;
}
public FrameworkName(string identifier, Version version)
: this(identifier, version, null)
{
}
public FrameworkName(string identifier, Version version, string? profile)
{
identifier = identifier?.Trim()!;
ArgumentException.ThrowIfNullOrEmpty(identifier);
ArgumentNullException.ThrowIfNull(version);
_identifier = identifier;
_version = version;
_profile = (profile == null) ? string.Empty : profile.Trim();
}
// Parses strings in the following format: "<identifier>, Version=[v|V]<version>, Profile=<profile>"
// - The identifier and version is required, profile is optional
// - Only three components are allowed.
// - The version string must be in the System.Version format; an optional "v" or "V" prefix is allowed
public FrameworkName(string frameworkName)
{
ArgumentException.ThrowIfNullOrEmpty(frameworkName);
ReadOnlySpan<char> frameworkNameSpan = frameworkName;
Span<Range> components = stackalloc Range[4];
int numComponents = frameworkNameSpan.Split(components, ComponentSeparator);
// Identifier and Version are required, Profile is optional.
if (numComponents is not (2 or 3))
{
throw new ArgumentException(SR.Argument_FrameworkNameTooShort, nameof(frameworkName));
}
components = components.Slice(0, numComponents);
//
// 1) Parse the "Identifier", which must come first. Trim any whitespace
//
_identifier = frameworkNameSpan[components[0]].Trim().ToString();
if (_identifier.Length == 0)
{
throw new ArgumentException(SR.Argument_FrameworkNameInvalid, nameof(frameworkName));
}
bool versionFound = false;
_profile = string.Empty;
//
// The required "Version" and optional "Profile" component can be in any order
//
for (int i = 1; i < components.Length; i++)
{
// Get the key/value pair separated by '='
ReadOnlySpan<char> component = frameworkNameSpan[components[i]];
int separatorIndex = component.IndexOf(KeyValueSeparator);
if (separatorIndex < 0 || separatorIndex != component.LastIndexOf(KeyValueSeparator))
{
throw new ArgumentException(SR.Argument_FrameworkNameInvalid, nameof(frameworkName));
}
// Get the key and value, trimming any whitespace
ReadOnlySpan<char> key = component.Slice(0, separatorIndex).Trim();
ReadOnlySpan<char> value = component.Slice(separatorIndex + 1).Trim();
//
// 2) Parse the required "Version" key value
//
if (key.Equals(VersionKey, StringComparison.OrdinalIgnoreCase))
{
versionFound = true;
// Allow the version to include a 'v' or 'V' prefix...
if (value.Length > 0 && (value[0] == VersionValuePrefix || value[0] == 'V'))
{
value = value.Slice(1);
}
try
{
_version = Version.Parse(value);
}
catch (Exception e)
{
throw new ArgumentException(SR.Argument_FrameworkNameInvalidVersion, nameof(frameworkName), e);
}
}
//
// 3) Parse the optional "Profile" key value
//
else if (key.Equals(ProfileKey, StringComparison.OrdinalIgnoreCase))
{
if (value.Length > 0)
{
_profile = value.ToString();
}
}
else
{
throw new ArgumentException(SR.Argument_FrameworkNameInvalid, nameof(frameworkName));
}
}
if (!versionFound)
{
throw new ArgumentException(SR.Argument_FrameworkNameMissingVersion, nameof(frameworkName));
}
}
public static bool operator ==(FrameworkName? left, FrameworkName? right)
{
if (left is null)
{
return right is null;
}
return left.Equals(right);
}
public static bool operator !=(FrameworkName? left, FrameworkName? right)
{
return !(left == right);
}
}
}