Skip to content

Commit 30487c1

Browse files
Solve exception when building case insensitive lookup.
1 parent d68f753 commit 30487c1

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

Source/EnumValue.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,18 @@ internal static Func<string, ValueLookupResult> ValueLookup
165165
private static IReadOnlyDictionary<string, TEnum>? _ignoreCaseLookup;
166166
internal static IReadOnlyDictionary<string, TEnum> IgnoreCaseLookup
167167
=> LazyInitializer.EnsureInitialized(ref _ignoreCaseLookup,
168-
() => Values.ToDictionary(e => Enum.GetName(typeof(TEnum), e)!, e => e, StringComparer.OrdinalIgnoreCase))!;
168+
() =>
169+
{
170+
// If the enum has duplicate values, we want to use the first one.
171+
var result = new Dictionary<string, TEnum>(StringComparer.OrdinalIgnoreCase);
172+
foreach (var e in Values)
173+
{
174+
var v = Enum.GetName(typeof(TEnum), e);
175+
if (result.ContainsKey(v)) continue;
176+
result[v] = e;
177+
}
178+
return result;
179+
})!;
169180

170181
static Entry[]?[] CreateLookup()
171182
{

Source/Open.Text.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Text</RepositoryUrl>
2020
<RepositoryType>git</RepositoryType>
2121
<PackageTags>string, span, enum, readonlyspan, text, format, split, trim, equals, trimmed equals, first, last, preceding, following, stringbuilder, extensions, stringcomparable, spancomparable, stringsegment, splitassegment</PackageTags>
22-
<Version>6.6.3</Version>
22+
<Version>6.6.4</Version>
2323
<PackageReleaseNotes></PackageReleaseNotes>
2424
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2525
<PublishRepositoryUrl>true</PublishRepositoryUrl>

Tests/EnumValueTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using FluentAssertions;
2+
using System;
23
using System.Linq;
34
using Xunit;
45

@@ -43,6 +44,15 @@ public enum Greek
4344
None
4445
}
4546

47+
public enum MultiCase
48+
{
49+
Goodbye,
50+
Hello,
51+
HELLO,
52+
hello,
53+
goodbye
54+
}
55+
4656
public enum LongEnum
4757
{
4858
In_the_glowing_moonlight_the_winds_caress_and_serenade_the_silent_ocean_waves,
@@ -122,6 +132,19 @@ public enum LargeEnum
122132

123133
public static class EnumValueTests
124134
{
135+
[Fact]
136+
public static void EnsureMultiCase()
137+
{
138+
EnumValue.TryParse<MultiCase>("HELLO", out var value).Should().Be(true);
139+
value.Should().Be(MultiCase.HELLO);
140+
141+
EnumValue.TryParse("HELLO", true, out value).Should().Be(true);
142+
value.Should().Be(MultiCase.Hello);
143+
144+
EnumValue.TryParse("goodbye", true, out value).Should().Be(true);
145+
value.Should().Be(MultiCase.Goodbye);
146+
}
147+
125148
[Fact]
126149
public static void IsIntType()
127150
{

0 commit comments

Comments
 (0)