Skip to content

Commit 242641e

Browse files
authored
Merge pull request #101 from codingseb/dev
Dev
2 parents 49d4129 + 5fabd69 commit 242641e

File tree

5 files changed

+98
-18
lines changed

5 files changed

+98
-18
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

+70
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,15 @@ ExpressionEvaluator evaluatorForMethodArgs()
24742474
.Returns(typeof(List<Regex>))
24752475
.SetCategory("Bug resolution");
24762476

2477+
yield return new TestCaseData(new ExpressionEvaluator()
2478+
{
2479+
OptionCaseSensitiveEvaluationActive = false
2480+
}
2481+
, "Int32.Parse(\"2\")"
2482+
, null)
2483+
.Returns(2)
2484+
.SetCategory("Bug resolution");
2485+
24772486
#region For bug #65
24782487
var Persons = new List<Person2>() { new Person2() { Code = "QT00010", Name = "Pedrito", Number = 11.11m },
24792488
new Person2() { Code = "QT00011", Name = "Pablito", Number = 12.11m }};
@@ -2631,6 +2640,67 @@ ExpressionEvaluator evaluatorForMethodArgs()
26312640

26322641
#endregion
26332642

2643+
#region for issue #100 Array types in cast or typeof generate an exception
2644+
2645+
yield return new TestCaseData(new ExpressionEvaluator()
2646+
, "typeof(double[])"
2647+
, null)
2648+
.Returns(typeof(double[]))
2649+
.SetCategory("Bug resolution");
2650+
2651+
yield return new TestCaseData(new ExpressionEvaluator()
2652+
, "typeof(double[ ])"
2653+
, null)
2654+
.Returns(typeof(double[]))
2655+
.SetCategory("Bug resolution");
2656+
2657+
yield return new TestCaseData(new ExpressionEvaluator()
2658+
, "typeof(double[][])"
2659+
, null)
2660+
.Returns(typeof(double[][]))
2661+
.SetCategory("Bug resolution");
2662+
2663+
yield return new TestCaseData(new ExpressionEvaluator()
2664+
, "typeof(double[,])"
2665+
, null)
2666+
.Returns(typeof(double[,]))
2667+
.SetCategory("Bug resolution");
2668+
2669+
yield return new TestCaseData(new ExpressionEvaluator()
2670+
, "typeof(int[])"
2671+
, null)
2672+
.Returns(typeof(int[]))
2673+
.SetCategory("Bug resolution");
2674+
2675+
yield return new TestCaseData(new ExpressionEvaluator()
2676+
, "typeof(Int32[])"
2677+
, null)
2678+
.Returns(typeof(Int32[]))
2679+
.SetCategory("Bug resolution");
2680+
2681+
yield return new TestCaseData(new ExpressionEvaluator()
2682+
, "typeof(string[])"
2683+
, null)
2684+
.Returns(typeof(string[]))
2685+
.SetCategory("Bug resolution");
2686+
2687+
yield return new TestCaseData(new ExpressionEvaluator()
2688+
, "typeof(Regex[])"
2689+
, null)
2690+
.Returns(typeof(Regex[]))
2691+
.SetCategory("Bug resolution");
2692+
2693+
yield return new TestCaseData(new ExpressionEvaluator(new ObjectContainer()
2694+
{
2695+
AnObjectProperty = new double[] {1.1, 2.3, 4.3}
2696+
})
2697+
, "(double[])AnObjectProperty"
2698+
, null)
2699+
.Returns(new double[] { 1.1, 2.3, 4.3 })
2700+
.SetCategory("Bug resolution");
2701+
2702+
#endregion
2703+
26342704
#endregion
26352705
}
26362706
}

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

+5-7
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
<Product>CodingSeb.ExpressionEvaluator</Product>
66
<Description>A Simple Math and Pseudo C# Expression Evaluator in One C# File. Can also execute small C# like scripts</Description>
77
<Copyright>Copyright © Coding Seb 2017</Copyright>
8-
<Version>1.4.27.0</Version>
9-
<AssemblyVersion>1.4.27.0</AssemblyVersion>
10-
<FileVersion>1.4.27.0</FileVersion>
8+
<Version>1.4.28.0</Version>
9+
<AssemblyVersion>1.4.28.0</AssemblyVersion>
10+
<FileVersion>1.4.28.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -19,10 +19,8 @@
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageIcon>Icon.png</PackageIcon>
2121
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
22-
<PackageReleaseNotes>* Manage Nested Types Part 2 (instanciate with new and casting)
23-
* Cast int to enum now work
24-
* Use methods name as method group and try to select the good overide when used as method parameter
25-
* Not perfect but better type inference of generic methods parameters</PackageReleaseNotes>
22+
<PackageReleaseNotes>* Correction of a bug : Int32, Int16 and Int64 were not evaluated when OptionCaseSensitiveEvaluationActive = false
23+
* Array types are now correctly evaluate in typeof and cast ex: typeof(double[]) or (string[])myArray</PackageReleaseNotes>
2624
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2725
<RepositoryUrl>https://github.com/codingseb/ExpressionEvaluator</RepositoryUrl>
2826
</PropertyGroup>

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/******************************************************************************************************
22
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
Version : 1.4.27.0
3+
Version : 1.4.28.0
44
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66
Author : Coding Seb
@@ -29,8 +29,6 @@ public partial class ExpressionEvaluator
2929
{
3030
#region Regex declarations
3131

32-
protected const string primaryTypesRegexPattern = @"(?<=^|[^\p{L}_])(?<primaryType>object|string|bool[?]?|byte[?]?|char[?]?|decimal[?]?|double[?]?|short[?]?|int[?]?|long[?]?|sbyte[?]?|float[?]?|ushort[?]?|uint[?]?|ulong[?]?|void)(?=[^a-zA-Z_]|$)";
33-
3432
protected static readonly Regex varOrFunctionRegEx = new Regex(@"^((?<sign>[+-])|(?<prefixOperator>[+][+]|--)|(?<varKeyword>var)\s+|(?<dynamicKeyword>dynamic)\s+|((?<nullConditional>[?])?(?<inObject>\.))?)(?<name>[\p{L}_](?>[\p{L}_0-9]*))(?>\s*)((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>|\?\?)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![\p{L}_0-9]))|((?<isgeneric>[<](?>([\p{L}_](?>[\p{L}_0-9]*)|(?>\s+)|[,\.])+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
3533

3634
protected const string numberRegexOrigPattern = @"^(?<sign>[+-])?([0-9][0-9_{1}]*[0-9]|\d)(?<hasdecimal>{0}?([0-9][0-9_]*[0-9]|\d)(e[+-]?([0-9][0-9_]*[0-9]|\d))?)?(?<type>ul|[fdulm])?";
@@ -40,6 +38,7 @@ public partial class ExpressionEvaluator
4038
protected static readonly Regex stringBeginningRegex = new Regex("^(?<interpolated>[$])?(?<escaped>[@])?[\"]", RegexOptions.Compiled);
4139
protected static readonly Regex internalCharRegex = new Regex(@"^['](\\[\\'0abfnrtv]|[^'])[']", RegexOptions.Compiled);
4240
protected static readonly Regex indexingBeginningRegex = new Regex(@"^[?]?\[", RegexOptions.Compiled);
41+
protected static readonly Regex arrayTypeDetectionRegex = new Regex(@"^(\s*(\[(?>(?>\s+)|[,])*)\])+", RegexOptions.Compiled);
4342
protected static readonly Regex assignationOrPostFixOperatorRegex = new Regex(@"^(?>\s*)((?<assignmentPrefix>[+\-*/%&|^]|<<|>>|\?\?)?=(?![=>])|(?<postfixOperator>([+][+]|--)(?![\p{L}_0-9])))");
4443
protected static readonly Regex genericsDecodeRegex = new Regex("(?<name>[^,<>]+)(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?", RegexOptions.Compiled);
4544
protected static readonly Regex genericsEndOnlyOneTrim = new Regex(@"(?>\s*)[>](?>\s*)$", RegexOptions.Compiled);
@@ -2583,6 +2582,18 @@ protected virtual Type EvaluateType(string expression,ref int i, string currentN
25832582
}
25842583
}
25852584

2585+
Match arrayTypeMatch;
2586+
2587+
if(i < expression.Length && (arrayTypeMatch = arrayTypeDetectionRegex.Match(expression.Substring(i))).Success)
2588+
{
2589+
Type arrayType = GetTypeByFriendlyName(staticType + arrayTypeMatch.Value);
2590+
if(arrayType != null)
2591+
{
2592+
i += arrayTypeMatch.Length;
2593+
staticType = arrayType;
2594+
}
2595+
}
2596+
25862597
return staticType;
25872598
}
25882599

@@ -3929,6 +3940,11 @@ protected virtual Type GetTypeByFriendlyName(string typeName, string genericType
39293940
typeName = typeName.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", "");
39303941
genericTypes = genericTypes.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", "");
39313942

3943+
if (primaryTypesDict.ContainsKey(OptionCaseSensitiveEvaluationActive ? typeName : typeName.ToLower()))
3944+
{
3945+
result = primaryTypesDict[OptionCaseSensitiveEvaluationActive ? typeName : typeName.ToLower()];
3946+
}
3947+
39323948
if (CacheTypesResolutions && (TypesResolutionCaching?.ContainsKey(typeName + genericTypes) ?? false))
39333949
{
39343950
result = TypesResolutionCaching[typeName + genericTypes];
@@ -3946,14 +3962,6 @@ protected virtual Type GetTypeByFriendlyName(string typeName, string genericType
39463962
result = Type.GetType(typeName + formatedGenericTypes, false, !OptionCaseSensitiveEvaluationActive);
39473963
}
39483964

3949-
if (result == null)
3950-
{
3951-
typeName = Regex.Replace(typeName, primaryTypesRegexPattern,
3952-
(Match match) => primaryTypesDict[OptionCaseSensitiveEvaluationActive ? match.Value : match.Value.ToLower()].ToString(), optionCaseSensitiveEvaluationActive ? RegexOptions.None : RegexOptions.IgnoreCase);
3953-
3954-
result = Type.GetType(typeName, false, !OptionCaseSensitiveEvaluationActive);
3955-
}
3956-
39573965
if (result == null)
39583966
{
39593967
result = Types.ToList().Find(type => type.Name.Equals(typeName, StringComparisonForCasing) || type.FullName.StartsWith(typeName + ","));

TryWindow/MainWindow.xaml

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
<WrapPanel>
2929
<CheckBox x:Name="NeedSemicolonAtTheEndCheckBox"
3030
Content="Need semicolon [;] At the end" />
31+
<CheckBox x:Name="CaseSensitiveCheckBox"
32+
Content="Case sensitive"
33+
IsChecked="True"/>
3134
</WrapPanel>
3235

3336
<Button x:Name="CalculateButton" Content="_Execute" IsDefault="True" Click="CalculateButton_Click" />

TryWindow/MainWindow.xaml.cs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
3939
ExpressionEvaluator evaluator = new ExpressionEvaluator()
4040
{
4141
OptionScriptNeedSemicolonAtTheEndOfLastExpression = NeedSemicolonAtTheEndCheckBox.IsChecked.GetValueOrDefault(),
42+
OptionCaseSensitiveEvaluationActive = CaseSensitiveCheckBox.IsChecked.GetValueOrDefault()
4243
};
4344

4445
if (UseCachesCheckbox.IsChecked ?? false)

0 commit comments

Comments
 (0)