Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Added EvaluateOptions.CaseInsensitiveComparer (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
gumbarros authored Nov 12, 2023
1 parent c0b88a6 commit 62989a7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/NCalcAsync/Domain/EvaluationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace NCalcAsync.Domain
{
public class EvaluationVisitor : LogicalExpressionVisitor
{
private delegate T Func<T>();
private delegate T Func<out T>();

private readonly EvaluateOptions _options = EvaluateOptions.None;
private readonly EvaluateOptions _options;
private readonly EvaluateParameterAsyncHandler _evaluateParameterAsync;
private readonly EvaluateFunctionAsyncHandler _evaluateFunctionAsync;
private readonly NumberConversionTypePreference _numberConversionTypePreference;

private bool IgnoreCase { get { return (_options & EvaluateOptions.IgnoreCase) == EvaluateOptions.IgnoreCase; } }
private bool IgnoreCase => (_options & EvaluateOptions.IgnoreCase) == EvaluateOptions.IgnoreCase;

public EvaluationVisitor(EvaluateOptions options, EvaluateParameterAsyncHandler evaluateParameterAsync, EvaluateFunctionAsyncHandler evaluateFunctionAsync, NumberConversionTypePreference numberConversionTypePreference = NumberConversionTypePreference.Decimal)
{
Expand All @@ -38,7 +38,7 @@ public override Task VisitAsync(LogicalExpression expression)
return Task.FromException(new Exception("The method or operation is not implemented."));
}

private static Type[] CommonTypes = new[] { typeof(Int64), typeof(Double), typeof(Boolean), typeof(String), typeof(Decimal) };
private static readonly Type[] CommonTypes = { typeof(Int64), typeof(Double), typeof(Boolean), typeof(String), typeof(Decimal) };

/// <summary>
/// Gets the the most precise type.
Expand Down Expand Up @@ -73,7 +73,11 @@ public int CompareUsingMostPreciseType(object a, object b)
mpt = GetMostPreciseType(a.GetType(), b?.GetType());
}

return Comparer.Default.Compare(Convert.ChangeType(a, mpt), Convert.ChangeType(b, mpt));
bool isCaseSensitiveComparer = (_options & EvaluateOptions.CaseInsensitiveComparer) == 0;

var comparer = isCaseSensitiveComparer ? (IComparer)Comparer.Default : CaseInsensitiveComparer.Default;

return comparer.Compare(Convert.ChangeType(a, mpt), Convert.ChangeType(b, mpt));
}

public override async Task VisitAsync(TernaryExpression expression)
Expand Down
7 changes: 6 additions & 1 deletion src/NCalcAsync/EvaluationOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public enum EvaluateOptions
//
// Summary:
// When using Round(), if a number is halfway between two others, it is rounded toward the nearest number that is away from zero.
RoundAwayFromZero = 16
RoundAwayFromZero = 16,

//
// Summary:
// Specifies the use of CaseInsensitiveComparer for comparasions.
CaseInsensitiveComparer = 32
}
}
10 changes: 10 additions & 0 deletions test/NCalcAsync.Tests/Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,16 @@ public async Task Should_Do_Number_Conversion()
Assert.AreEqual(calculatedValue, await expression.EvaluateAsync());
}
}


[TestMethod]
public async Task Should_Use_Case_Insensitive_Comparer_Issue_24()
{
var eif = new Expression("PageState == 'LIST'", EvaluateOptions.CaseInsensitiveComparer);
eif.Parameters["PageState"] = "List";

Assert.AreEqual(true, await eif.EvaluateAsync());
}
}
}

0 comments on commit 62989a7

Please sign in to comment.