forked from ncalc/ncalc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
16 changed files
with
190 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
namespace NCalc.Exceptions; | ||
|
||
public sealed class NCalcEvaluationException : NCalcException | ||
{ | ||
public NCalcEvaluationException(string message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
public NCalcEvaluationException(string message, Exception innerException) | ||
: base(message, innerException) | ||
{ | ||
} | ||
} | ||
public class NCalcEvaluationException(string message) : NCalcException(message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace NCalc.Exceptions; | ||
|
||
public sealed class NCalcFunctionNotFoundException(string message, string functionName) | ||
: NCalcEvaluationException(message) | ||
{ | ||
public string FunctionName { get;} = functionName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace NCalc.Exceptions; | ||
|
||
public sealed class NCalcParameterNotDefinedException(string parameterName) | ||
: NCalcEvaluationException($"Parameter {parameterName} not defined.") | ||
{ | ||
public string ParameterName { get; } = parameterName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace NCalc.Helpers; | ||
|
||
public static class TypeHelper | ||
{ | ||
private static readonly Type[] BuiltInTypes = | ||
[ | ||
typeof(decimal), | ||
typeof(double), | ||
typeof(float), | ||
typeof(long), | ||
typeof(ulong), | ||
typeof(int), | ||
typeof(uint), | ||
typeof(short), | ||
typeof(ushort), | ||
typeof(byte), | ||
typeof(sbyte), | ||
typeof(char), | ||
typeof(bool), | ||
typeof(string), | ||
typeof(object) | ||
]; | ||
|
||
/// <summary> | ||
/// Gets the the most precise type. | ||
/// </summary> | ||
/// <param name="a">Type a.</param> | ||
/// <param name="b">Type b.</param> | ||
/// <returns></returns> | ||
private static Type GetMostPreciseType(Type? a, Type? b) | ||
{ | ||
foreach (var t in BuiltInTypes) | ||
{ | ||
if (a == t || b == t) | ||
{ | ||
return t; | ||
} | ||
} | ||
|
||
return a ?? typeof(object); | ||
} | ||
|
||
public static bool IsReal(object? value) => value is decimal or double or float; | ||
|
||
public record struct ComparasionOptions(CultureInfo CultureInfo, bool IsCaseSensitive); | ||
public static int CompareUsingMostPreciseType(object? a, object? b, ComparasionOptions options) | ||
{ | ||
var (cultureInfo, isCaseInsensitiveComparer) = options; | ||
|
||
var mpt = GetMostPreciseType(a?.GetType(), b?.GetType()); | ||
|
||
var aValue = a != null ? Convert.ChangeType(a, mpt, cultureInfo) : null; | ||
var bValue = b != null ? Convert.ChangeType(b, mpt, cultureInfo) : null; | ||
|
||
if (isCaseInsensitiveComparer) | ||
return CaseInsensitiveComparer.Default.Compare(aValue, bValue); | ||
|
||
return Comparer.Default.Compare(aValue, bValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.