Skip to content

Commit 805fe03

Browse files
Cleanup.
1 parent 94cc93a commit 805fe03

File tree

5 files changed

+53
-73
lines changed

5 files changed

+53
-73
lines changed

Benchmarks/EnumParseTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Greek EnumParse()
4242

4343
private static bool TryParseBySwitch(string value, bool ignoreCase, out Greek e)
4444
{
45-
if(ignoreCase)
45+
if (ignoreCase)
4646
{
4747
if (value.Equals(nameof(Greek.Alpha), StringComparison.OrdinalIgnoreCase))
4848
{
@@ -149,7 +149,7 @@ private static bool TryParseBySwitch(string value, bool ignoreCase, out Greek e)
149149
private static bool TryParseByLengthSwitch(string value, bool ignoreCase, out Greek e)
150150
{
151151
var len = value.Length;
152-
switch(len)
152+
switch (len)
153153
{
154154
case 3:
155155
if (value.Equals(nameof(Greek.Phi), ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))

Benchmarks/Greek.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
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
1+
namespace Open.Text.Benchmarks
82
{
93
public enum Greek
104
{

Benchmarks/Program.cs

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

44
//BenchmarkRunner.Run<EnumParseTests>();
55
BenchmarkRunner.Run<EnumToStringTests>();

Source/EnumValue.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Collections.Immutable;
43
using System.Diagnostics;
54
using System.Linq;
65
using System.Linq.Expressions;
@@ -62,7 +61,7 @@ static Func<TEnum, string> GetEnumNameDelegate()
6261
),
6362
null,
6463
Enum.GetValues(tEnum).Cast<Object>().Select(v => Expression.SwitchCase(
65-
Expression.Constant(v.ToString()),
64+
Expression.Constant(string.Intern(v.ToString())),
6665
Expression.Constant(v)
6766
)).ToArray()
6867
)
@@ -80,7 +79,7 @@ static Func<TEnum, string> GetEnumNameDelegate()
8079

8180
foreach (var e in values)
8281
{
83-
var n = e.ToString();
82+
var n = string.Intern(e.ToString());
8483
var len = n.Length;
8584
if (len > longest) longest = len;
8685
if (!d.TryGetValue(len, out var v)) d.Add(len, v = new());
@@ -94,11 +93,6 @@ static Func<TEnum, string> GetEnumNameDelegate()
9493
return result;
9594
}
9695

97-
static ImmutableSortedDictionary<TEnum, string> CreateNameLookup()
98-
=> Enum.GetValues(typeof(TEnum))
99-
.Cast<TEnum>()
100-
.ToImmutableSortedDictionary(v => v, v => v.ToString());
101-
10296
/// <summary>
10397
/// Indicates whether this instance matches the enum value of <paramref name="other"/>.
10498
/// </summary>
@@ -171,15 +165,7 @@ public EnumValueCaseIgnored(string value)
171165
public TEnum Value { get; }
172166

173167
/// <inheritdoc cref="EnumValue{TEnum}.ToString"/>
174-
public override string ToString() => Value.ToString();
175-
176-
internal static readonly ImmutableDictionary<string, TEnum> Lookup = CreateLookup();
177-
178-
static ImmutableDictionary<string, TEnum> CreateLookup()
179-
=> Enum
180-
.GetValues(typeof(TEnum))
181-
.Cast<TEnum>()
182-
.ToImmutableDictionary(v => v.ToString(), v => v, StringComparer.OrdinalIgnoreCase);
168+
public override string ToString() => EnumValue<TEnum>.NameLookup(Value);
183169

184170
/// <inheritdoc cref="EnumValue{TEnum}.Equals(EnumValue{TEnum})"/>
185171
public bool Equals(EnumValue<TEnum> other) => Value.Equals(other.Value);
@@ -270,11 +256,11 @@ public static bool TryParse<TEnum>(string value, bool ignoreCase, out TEnum e)
270256
if (r is null) goto notFound;
271257

272258
var sc = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
273-
foreach (var item in r)
259+
foreach (var (Name, Value) in r)
274260
{
275-
if (item.Name.Equals(value, sc))
261+
if (Name.Equals(value, sc))
276262
{
277-
e = item.Value;
263+
e = Value;
278264
return true;
279265
}
280266
}

Source/Extensions.Split.cs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -371,29 +371,29 @@ public static IReadOnlyList<string> Split(this ReadOnlySpan<char> source,
371371
return ImmutableArray<string>.Empty;
372372

373373
case StringSplitOptions.RemoveEmptyEntries:
374-
{
375-
Debug.Assert(!source.IsEmpty);
376-
var list = new List<string>();
377-
378-
loop:
379-
var result = source.FirstSplit(splitCharacter, out var nextIndex);
380-
if (!result.IsEmpty) list.Add(result.ToString());
381-
if (nextIndex == -1) return list;
382-
source = source.Slice(nextIndex);
383-
goto loop;
384-
}
374+
{
375+
Debug.Assert(!source.IsEmpty);
376+
var list = new List<string>();
377+
378+
loop:
379+
var result = source.FirstSplit(splitCharacter, out var nextIndex);
380+
if (!result.IsEmpty) list.Add(result.ToString());
381+
if (nextIndex == -1) return list;
382+
source = source.Slice(nextIndex);
383+
goto loop;
384+
}
385385

386386
default:
387-
{
388-
Debug.Assert(!source.IsEmpty);
389-
var list = new List<string>();
390-
loop:
391-
var result = source.FirstSplit(splitCharacter, out var nextIndex);
392-
list.Add(result.IsEmpty ? string.Empty : result.ToString());
393-
if (nextIndex == -1) return list;
394-
source = source.Slice(nextIndex);
395-
goto loop;
396-
}
387+
{
388+
Debug.Assert(!source.IsEmpty);
389+
var list = new List<string>();
390+
loop:
391+
var result = source.FirstSplit(splitCharacter, out var nextIndex);
392+
list.Add(result.IsEmpty ? string.Empty : result.ToString());
393+
if (nextIndex == -1) return list;
394+
source = source.Slice(nextIndex);
395+
goto loop;
396+
}
397397
}
398398
}
399399

@@ -419,29 +419,29 @@ public static IReadOnlyList<string> Split(this ReadOnlySpan<char> source,
419419
return ImmutableArray<string>.Empty;
420420

421421
case StringSplitOptions.RemoveEmptyEntries:
422-
{
423-
Debug.Assert(!source.IsEmpty);
424-
var list = new List<string>();
425-
426-
loop:
427-
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
428-
if (!result.IsEmpty) list.Add(result.ToString());
429-
if (nextIndex == -1) return list;
430-
source = source.Slice(nextIndex);
431-
goto loop;
432-
}
422+
{
423+
Debug.Assert(!source.IsEmpty);
424+
var list = new List<string>();
425+
426+
loop:
427+
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
428+
if (!result.IsEmpty) list.Add(result.ToString());
429+
if (nextIndex == -1) return list;
430+
source = source.Slice(nextIndex);
431+
goto loop;
432+
}
433433

434434
default:
435-
{
436-
Debug.Assert(!source.IsEmpty);
437-
var list = new List<string>();
438-
loop:
439-
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
440-
list.Add(result.IsEmpty ? string.Empty : result.ToString());
441-
if (nextIndex == -1) return list;
442-
source = source.Slice(nextIndex);
443-
goto loop;
444-
}
435+
{
436+
Debug.Assert(!source.IsEmpty);
437+
var list = new List<string>();
438+
loop:
439+
var result = source.FirstSplit(splitSequence, out var nextIndex, comparisonType);
440+
list.Add(result.IsEmpty ? string.Empty : result.ToString());
441+
if (nextIndex == -1) return list;
442+
source = source.Slice(nextIndex);
443+
goto loop;
444+
}
445445
}
446446
}
447447
}

0 commit comments

Comments
 (0)