Skip to content

Commit 93eed33

Browse files
Updated to leverage optimized method.
1 parent 9f05a46 commit 93eed33

2 files changed

Lines changed: 48 additions & 73 deletions

File tree

Source/EnumValue.cs

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public EnumValue(TEnum value)
2828
/// <exception cref="ArgumentNullException">value is null.</exception>
2929
public EnumValue(string value)
3030
{
31-
Value = Parse(value);
31+
Value = EnumValue.Parse<TEnum>(value);
3232
}
3333

3434
/// <summary>
@@ -41,42 +41,7 @@ public EnumValue(string value)
4141
/// </summary>
4242
public override string ToString() => Value.ToString();
4343

44-
static readonly (string Name, TEnum Value)[]?[] Lookup = CreateLookup();
45-
46-
/// <summary>
47-
/// Uses a case-senstive dictionary lookup to get a matching enum value.
48-
/// </summary>
49-
/// <param name="value">The string represnting the enum to search for.</param>
50-
/// <returns>The enum that represents the string <paramref name="value"/> provided.</returns>
51-
/// <exception cref="ArgumentNullException">value is null</exception>
52-
/// <exception cref="ArgumentException">Requested <paramref name="value"/> was not found.</exception>
53-
public static TEnum Parse(string value)
54-
=> TryParse(value, out var e) ? e : throw new ArgumentException($"Requested value '{value}' was not found.", nameof(value));
55-
56-
/// <summary>
57-
/// Uses a case-senstive dictionary lookup to get a matching enum value.
58-
/// </summary>
59-
/// <returns>true if the value found; otherwise false.</returns>
60-
/// <exception cref="ArgumentNullException"/>
61-
public static bool TryParse(string? value, out TEnum e)
62-
{
63-
if (value is null) goto notFound;
64-
var len = value.Length;
65-
if (len > Lookup.Length) goto notFound;
66-
var r = Lookup[len];
67-
if (r is null) goto notFound;
68-
69-
foreach(var item in r)
70-
{
71-
if (item.Name != value) continue;
72-
e = item.Value;
73-
return true;
74-
}
75-
76-
notFound:
77-
e = default!;
78-
return false;
79-
}
44+
internal static readonly (string Name, TEnum Value)[]?[] Lookup = CreateLookup();
8045

8146
static (string Name, TEnum Value)[]?[] CreateLookup()
8247
{
@@ -95,7 +60,7 @@ public static bool TryParse(string? value, out TEnum e)
9560
v.Add((n, e));
9661
}
9762

98-
var result = new (string Name, TEnum Value)[longest][];
63+
var result = new (string Name, TEnum Value)[]?[longest + 1];
9964
foreach (var i in d.Keys)
10065
result[i] = d[i].ToArray();
10166

@@ -168,7 +133,7 @@ public EnumValueCaseIgnored(TEnum value)
168133
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
169134
public EnumValueCaseIgnored(string value)
170135
{
171-
Value = Parse(value);
136+
Value = EnumValue.Parse<TEnum>(value, true);
172137
}
173138

174139
public TEnum Value { get; }
@@ -178,24 +143,6 @@ public EnumValueCaseIgnored(string value)
178143

179144
internal static readonly ImmutableDictionary<string, TEnum> Lookup = CreateLookup();
180145

181-
/// <summary>
182-
/// Uses a case-insenstive dictionary lookup to get a matching enum value.
183-
/// </summary>
184-
/// <inheritdoc cref="EnumValue{TEnum}.Parse(string)" />
185-
public static TEnum Parse(string value)
186-
=> TryParse(value, out var e) ? e : throw new ArgumentException($"Requested value '{value}' was not found.", nameof(value));
187-
188-
/// <summary>
189-
/// Uses a case-insenstive dictionary lookup to get a matching enum value.
190-
/// </summary>
191-
/// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
192-
public static bool TryParse(string? value, out TEnum e)
193-
{
194-
if (Lookup.TryGetValue(value, out e!)) return true;
195-
e = default!;
196-
return false;
197-
}
198-
199146
static ImmutableDictionary<string, TEnum> CreateLookup()
200147
=> Enum
201148
.GetValues(typeof(TEnum))
@@ -243,38 +190,66 @@ private string GetDebuggerDisplay()
243190

244191
public static class EnumValue
245192
{
246-
/// <inheritdoc cref="EnumValue{TEnum}.Parse(string)"/>
193+
/// <summary>
194+
/// Converts the string representation of the name of one or more enumerated constants to an equivalent enumerated object.
195+
/// A parameter specifies whether the operation is case-insensitive.
196+
/// </summary>
197+
/// <param name="value">The string representing the enum value to search for.</param>
198+
/// <returns>The enum that represents the string <paramref name="value"/> provided.</returns>
199+
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
200+
/// <exception cref="ArgumentException">Requested <paramref name="value"/> was not found.</exception>
247201
public static TEnum Parse<TEnum>(string value)
248202
where TEnum : Enum
249-
=> EnumValue<TEnum>.Parse(value);
203+
=> TryParse<TEnum>(value, false, out var e) ? e
204+
: throw new ArgumentException($"Requested value '{value}' was not found.", nameof(value));
250205

251206
/// <summary>
252207
/// Converts the string representation of the name of one or more enumerated constants to an equivalent enumerated object.
253208
/// A parameter specifies whether the operation is case-insensitive.
254209
/// </summary>
255210
/// <param name="ignoreCase">If true, will ignore case differences when looking for a match.</param>
256-
/// <inheritdoc cref="EnumValue{TEnum}.Parse(string)"/>
211+
/// <inheritdoc cref="Parse{TEnum}(string)"/>
257212
public static TEnum Parse<TEnum>(string value, bool ignoreCase)
258213
where TEnum : Enum
259-
=> ignoreCase
260-
? EnumValueCaseIgnored<TEnum>.Parse(value)
261-
: EnumValue<TEnum>.Parse(value);
214+
=> TryParse<TEnum>(value, ignoreCase, out var e) ? e
215+
: throw new ArgumentException($"Requested value '{value}' was not found.", nameof(value));
262216

263-
/// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
264-
public static bool TryParse<TEnum>(string value, out TEnum e)
265-
where TEnum : Enum
266-
=> EnumValue<TEnum>.TryParse(value, out e);
267-
268-
/// <inheritdoc cref="EnumValue{TEnum}.TryParse(string, out TEnum)"/>
269217
/// <summary>
270218
/// Converts the string representation of the name of one or more enumerated constants to an equivalent enumerated object.
271219
/// A parameter specifies whether the operation is case-insensitive.
272220
/// </summary>
221+
/// <param name="value">The string representing the enum value to search for.</param>
222+
/// <param name="e">The enum that represents the string <paramref name="value"/> provided.</param>
223+
/// <returns>true if the value was found; otherwise false.</returns>
224+
public static bool TryParse<TEnum>(string value, out TEnum e)
225+
where TEnum : Enum
226+
=> TryParse(value, false, out e);
227+
273228
/// <param name="ignoreCase">If true, will ignore case differences when looking for a match.</param>
229+
/// <inheritdoc cref="TryParse{TEnum}(string, out TEnum)"/>
274230
public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum e)
275231
where TEnum : Enum
276-
=> ignoreCase
277-
? EnumValueCaseIgnored<TEnum>.TryParse(value, out e)
278-
: EnumValue<TEnum>.TryParse(value, out e);
232+
{
233+
if (value is null) goto notFound;
234+
var len = value.Length;
235+
var lookup = EnumValue<TEnum>.Lookup;
236+
if (len >= lookup.Length) goto notFound;
237+
var r = lookup[len];
238+
if (r is null) goto notFound;
239+
240+
var sc = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
241+
foreach (var item in r)
242+
{
243+
if (item.Name.Equals(value, sc))
244+
{
245+
e = item.Value;
246+
return true;
247+
}
248+
}
249+
250+
notFound:
251+
e = default!;
252+
return false;
253+
}
279254
}
280255
}

Source/Open.Text.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Text</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
1919
<PackageTags>dotnet, dotnetcore, string, span, readonlyspan, text, format, split, trim, equals, trimmed equals, first, last, preceding, following, stringbuilder, extensions</PackageTags>
20-
<Version>3.3.3</Version>
20+
<Version>3.4.0</Version>
2121
<PackageReleaseNotes></PackageReleaseNotes>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2323
<PublishRepositoryUrl>true</PublishRepositoryUrl>

0 commit comments

Comments
 (0)