Skip to content

Commit f2f6462

Browse files
Updated with Get Name.
1 parent 93eed33 commit f2f6462

5 files changed

Lines changed: 79 additions & 8 deletions

File tree

Benchmarks/EnumParseTests.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33

44
namespace Open.Text.Benchmarks
55
{
6-
7-
public enum Greek
8-
{
9-
Alpha, Beta, Cappa, Delta, Epsilon, Gamma, Omega, Phi, Theta, None
10-
}
11-
126
[MemoryDiagnoser]
137
public class EnumParseTests
148
{

Benchmarks/EnumToStringTests.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using BenchmarkDotNet.Attributes;
2+
using System.Collections.Immutable;
3+
4+
namespace Open.Text.Benchmarks
5+
{
6+
public class EnumToStringTests
7+
{
8+
static readonly ImmutableArray<Greek> Values = Enum.GetValues(typeof(Greek)).Cast<Greek>().ToImmutableArray();
9+
10+
[Benchmark(Baseline = true)]
11+
public void EnumToString()
12+
{
13+
foreach (var g in Values)
14+
_ = g.ToString();
15+
}
16+
17+
static string Switch(Greek value) => value switch
18+
{
19+
Greek.Alpha => nameof(Greek.Alpha),
20+
Greek.Beta => nameof(Greek.Beta),
21+
Greek.Cappa => nameof(Greek.Cappa),
22+
Greek.Delta => nameof(Greek.Delta),
23+
Greek.Epsilon => nameof(Greek.Epsilon),
24+
Greek.Gamma => nameof(Greek.Gamma),
25+
Greek.Omega => nameof(Greek.Omega),
26+
Greek.Phi => nameof(Greek.Phi),
27+
Greek.Theta => nameof(Greek.Theta),
28+
Greek.None => nameof(Greek.None),
29+
_ => throw new Exception(),
30+
};
31+
32+
[Benchmark]
33+
public void CompiledSwitch()
34+
{
35+
foreach (var g in Values)
36+
_ = Switch(g);
37+
}
38+
39+
[Benchmark]
40+
public void EnumValueGetName()
41+
{
42+
foreach (var g in Values)
43+
_ = EnumValue.GetName(g);
44+
}
45+
}
46+
}

Benchmarks/Greek.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Open.Text.Benchmarks
8+
{
9+
public enum Greek
10+
{
11+
Alpha, Beta, Cappa, Delta, Epsilon, Gamma, Omega, Phi, Theta, None
12+
}
13+
}

Benchmarks/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Open.Text.Benchmarks;
22
using BenchmarkDotNet.Running;
33

4-
BenchmarkRunner.Run<EnumParseTests>();
4+
//BenchmarkRunner.Run<EnumParseTests>();
5+
BenchmarkRunner.Run<EnumToStringTests>();

Source/EnumValue.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Immutable;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Linq.Expressions;
67

78
namespace Open.Text
89
{
@@ -39,10 +40,12 @@ public EnumValue(string value)
3940
/// <summary>
4041
/// Returns the string representation of the enum value.
4142
/// </summary>
42-
public override string ToString() => Value.ToString();
43+
public override string ToString() => NameLookup[Value];
4344

4445
internal static readonly (string Name, TEnum Value)[]?[] Lookup = CreateLookup();
4546

47+
internal static readonly ImmutableSortedDictionary<TEnum, string> NameLookup = CreateNameLookup();
48+
4649
static (string Name, TEnum Value)[]?[] CreateLookup()
4750
{
4851
var longest = 0;
@@ -67,6 +70,11 @@ public EnumValue(string value)
6770
return result;
6871
}
6972

73+
static ImmutableSortedDictionary<TEnum, string> CreateNameLookup()
74+
=> Enum.GetValues(typeof(TEnum))
75+
.Cast<TEnum>()
76+
.ToImmutableSortedDictionary(v => v, v => v.ToString());
77+
7078
/// <summary>
7179
/// Indicates whether this instance matches the enum value of <paramref name="other"/>.
7280
/// </summary>
@@ -251,5 +259,14 @@ public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum e)
251259
e = default!;
252260
return false;
253261
}
262+
263+
/// <summary>
264+
/// Uses a dictionary to lookup the name of the enum value.
265+
/// </summary>
266+
/// <typeparam name="TEnum">The enum type.</typeparam>
267+
/// <param name="value">The enum value to get the name for.</param>
268+
/// <returns>The name of the enum.</returns>
269+
public static string GetName<TEnum>(TEnum value) where TEnum : Enum
270+
=> EnumValue<TEnum>.NameLookup[value];
254271
}
255272
}

0 commit comments

Comments
 (0)