Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case insensitive name comparison bug #532

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions src/SmartEnum/SmartFlagEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public abstract class SmartFlagEnum<TEnum, TValue> :
static readonly Lazy<Dictionary<string, TEnum>> _fromName =
new Lazy<Dictionary<string, TEnum>>(() => GetAllOptions().ToDictionary(item => item.Name));

static readonly Lazy<Dictionary<string, TEnum>> _fromNameIgnoreCase =
new Lazy<Dictionary<string, TEnum>>(() => GetAllOptions().ToDictionary(item => item.Name, StringComparer.OrdinalIgnoreCase));

private static IEnumerable<TEnum> GetAllOptions()
{
Type baseType = typeof(TEnum);
Expand Down Expand Up @@ -123,19 +120,12 @@ public static IEnumerable<TEnum> FromName(string names, bool ignoreCase = false,
if (String.IsNullOrEmpty(names))
ThrowHelper.ThrowArgumentNullOrEmptyException(nameof(names));

if (ignoreCase)
return FromName(_fromNameIgnoreCase.Value);
else
return FromName(_fromName.Value);

IEnumerable<TEnum> FromName(Dictionary<string, TEnum> dictionary)
if (!_fromName.Value.TryGetFlagEnumValuesByName<TEnum, TValue>(names, ignoreCase, out var result))
{
if (!dictionary.TryGetFlagEnumValuesByName<TEnum, TValue>(names, out var result))
{
ThrowHelper.ThrowNameNotFoundException<TEnum, TValue>(names);
}
return result;
ThrowHelper.ThrowNameNotFoundException<TEnum, TValue>(names);
}

return result;
}

/// <summary>
Expand Down Expand Up @@ -170,10 +160,7 @@ public static bool TryFromName(string names, bool ignoreCase, out IEnumerable<TE
if (String.IsNullOrEmpty(names))
ThrowHelper.ThrowArgumentNullOrEmptyException(nameof(names));

if (ignoreCase)
return _fromNameIgnoreCase.Value.TryGetFlagEnumValuesByName<TEnum, TValue>(names, out result);
else
return _fromName.Value.TryGetFlagEnumValuesByName<TEnum, TValue>(names, out result);
return _fromName.Value.TryGetFlagEnumValuesByName<TEnum, TValue>(names, ignoreCase, out result);
}

/// <summary>
Expand Down
7 changes: 4 additions & 3 deletions src/SmartEnum/SmartFlagEnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@
/// <param name="names"></param>
/// <param name="outputEnums"></param>
/// <returns></returns>
public static bool TryGetFlagEnumValuesByName<TEnum, TValue>(this Dictionary<string, TEnum> dictionary, string names, out IEnumerable<TEnum> outputEnums)
public static bool TryGetFlagEnumValuesByName<TEnum, TValue>(this Dictionary<string, TEnum> dictionary, string names, bool ignoreCase, out IEnumerable<TEnum> outputEnums)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)

Check failure on line 60 in src/SmartEnum/SmartFlagEnumExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'ignoreCase' has no matching param tag in the XML comment for 'SmartFlagEnumExtensions.TryGetFlagEnumValuesByName<TEnum, TValue>(Dictionary<string, TEnum>, string, bool, out IEnumerable<TEnum>)' (but other parameters do)
where TEnum : SmartFlagEnum<TEnum, TValue>
where TValue : IEquatable<TValue>, IComparable<TValue>
{
var outputList = new List<TEnum>(dictionary.Count);

var commaSplitNameList = names.Replace(" ", "").Trim().Split(',');
Array.Sort(commaSplitNameList);
var nameComparer = ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
Array.Sort(commaSplitNameList, nameComparer);

foreach (var enumValue in dictionary.Values)
{
var result = Array.BinarySearch(commaSplitNameList, enumValue.Name);
var result = Array.BinarySearch(commaSplitNameList, enumValue.Name, nameComparer);
if (result >= 0)
{
outputList.Add(enumValue);
Expand Down
8 changes: 8 additions & 0 deletions test/SmartEnum.UnitTests/SmartEnumFromName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public void ReturnsEnumGivenMatchingName()
result.Should().BeSameAs(TestEnum.One);
}

[Fact]
public void ReturnsEnumGivenMatchingNameIgnoreCase()
{
var result = TestEnum.FromName("ONE", true);

result.Should().BeSameAs(TestEnum.One);
}

[Fact]
public void ReturnsEnumGivenDerivedClass()
{
Expand Down
4 changes: 2 additions & 2 deletions test/SmartFlagEnum.UnitTests/SmartFlagEnumFromName.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -14,7 +14,7 @@ public class SmartFlagEnumFromName
[Fact]
public void IgnoreCaseReturnsIEnumerableWithSameValues()
{
var result = SmartFlagTestEnum.FromName("One, Two", true).ToList();
var result = SmartFlagTestEnum.FromName("ONE, TWO", true).ToList();

Assert.Equal("One", result[0].Name);
Assert.Equal("Two", result[1].Name);
Expand Down
Loading