Skip to content

Commit 0b4cb7b

Browse files
authored
Merge pull request #28 from codingseb/dev
Dev
2 parents d107857 + e4e747f commit 0b4cb7b

File tree

7 files changed

+98
-31
lines changed

7 files changed

+98
-31
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,17 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
12731273
.Returns("[6,4,10,6,10,4]");
12741274

12751275
#endregion
1276+
1277+
#region For Bug correction (no regression)
1278+
1279+
yield return new TestCaseData(Resources.Script0049, null, null, null)
1280+
.SetCategory("Script")
1281+
.SetCategory("variable assignation")
1282+
.SetCategory("Bug")
1283+
.SetCategory("#26")
1284+
.Returns(false);
1285+
1286+
#endregion
12761287
}
12771288
}
12781289

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ public void TypeTesting(string expression, Type type)
384384
[TestCase("5 + 10 * 2", TestOf = typeof(int), ExpectedResult = 25, Category = "DivAndMultiplyPriorityOverSubAndAdd")]
385385
#endregion
386386

387-
#region ParenthesisPriority
387+
#region Parenthesis Priority
388388
[TestCase("(5d - 10) / 2", TestOf = typeof(double), ExpectedResult = -2.5, Category = "ParenthesisPriority")]
389389
[TestCase("(5d + 10) / 2", TestOf = typeof(double), ExpectedResult = 7.5, Category = "ParenthesisPriority")]
390390
[TestCase("(5 - 10) * 2", TestOf = typeof(double), ExpectedResult = -10, Category = "ParenthesisPriority")]
@@ -971,6 +971,7 @@ public void TypeTesting(string expression, Type type)
971971
[TestCase("$\"https://www.google.com/search?q={System.Net.WebUtility.UrlEncode(\"test of request with url encode() ?\")}\"", ExpectedResult = "https://www.google.com/search?q=test+of+request+with+url+encode()+%3F", Category = "Complex expression,Inline namespace")]
972972
[TestCase("new System.Xml.XmlDocument().FluidLoadXml(\"<root><element id='MyElement'>Xml Content</element></root>\").SelectSingleNode(\"//element[@id='MyElement']\").InnerXml", ExpectedResult = "Xml Content", Category = "Complex expression,Inline namespace,Fluid")]
973973
[TestCase("new System.Xml.XmlDocument().FluidLoadXml(\"<root><element id='MyElement'>Xml Content</element></root>\").ChildNodes[0].Name", ExpectedResult = "root", Category = "Complex expression,Inline namespace,Fluid,Custom Indexer")]
974+
[TestCase("string.Join(\" - \", new string[]{\"Hello\", \"Bye\", \"Other\"})", ExpectedResult = "Hello - Bye - Other", Category = "Complex expression, Different brackets imbrication")]
974975

975976
#endregion
976977

@@ -1473,11 +1474,40 @@ public static IEnumerable<TestCaseData> TestCasesEvaluateWithSpecificEvaluator
14731474
.SetCategory("Numbers Culture");
14741475

14751476
#endregion
1477+
1478+
#region Force Integer numbers default type
1479+
1480+
yield return new TestCaseData(new ExpressionEvaluator()
1481+
, "(130-120)/(2*250)")
1482+
.Returns(0)
1483+
.SetCategory("Options")
1484+
.SetCategory("Integer Numbers default types");
1485+
1486+
yield return new TestCaseData(new ExpressionEvaluator
1487+
{
1488+
OptionForceIntegerNumbersEvaluationsAsDoubleByDefault = false
1489+
}
1490+
, "(130-120)/(2*250)")
1491+
.Returns(0)
1492+
.SetCategory("Options")
1493+
.SetCategory("Integer Numbers default types");
1494+
1495+
yield return new TestCaseData(new ExpressionEvaluator
1496+
{
1497+
OptionForceIntegerNumbersEvaluationsAsDoubleByDefault = true
1498+
}
1499+
, "(130-120)/(2*250)")
1500+
.Returns(0.02)
1501+
.SetCategory("Options")
1502+
.SetCategory("Integer Numbers default types");
1503+
1504+
#endregion
14761505
}
14771506
}
14781507

14791508
#endregion
14801509

1510+
14811511
[TestCaseSource(nameof(TestCasesEvaluateWithSpecificEvaluator))]
14821512
public object EvaluateWithSpecificEvaluator(ExpressionEvaluator evaluator, string expression)
14831513
{

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodingSeb.ExpressionEvaluator.Tests/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,7 @@
262262
<data name="Script0048" type="System.Resources.ResXFileRef, System.Windows.Forms">
263263
<value>resources\script0048.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
264264
</data>
265+
<data name="Script0049" type="System.Resources.ResXFileRef, System.Windows.Forms">
266+
<value>resources\script0049.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
267+
</data>
265268
</root>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Script0049 */
2+
P1 = 5;
3+
P2 = 6;
4+
P1 < 1 && P2 > 3;

CodingSeb.ExpressionEvaluator/CodingSeb.ExpressionEvaluator.csproj

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
<AssemblyTitle>CodingSeb.ExpressionEvaluator</AssemblyTitle>
44
<Company>Coding Seb</Company>
55
<Product>CodingSeb.ExpressionEvaluator</Product>
6-
<Description>A Simple Math and Pseudo C# Expression Evaluator in One C# File. And from version 1.2.0 can execute small C# like scripts</Description>
6+
<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.3.4.0</Version>
9-
<AssemblyVersion>1.3.4.0</AssemblyVersion>
10-
<FileVersion>1.3.4.0</FileVersion>
8+
<Version>1.3.5.0</Version>
9+
<AssemblyVersion>1.3.5.0</AssemblyVersion>
10+
<FileVersion>1.3.5.0</FileVersion>
1111
<OutputPath>bin\$(Configuration)\</OutputPath>
1212
<Authors>Coding Seb</Authors>
1313
<PackageId>CodingSeb.ExpressionEvaluator</PackageId>
@@ -18,19 +18,9 @@
1818
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1919
<PackageIconUrl>https://github.com/codingseb/ExpressionEvaluator/blob/master/Icon.png?raw=true</PackageIconUrl>
2020
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
21-
<PackageReleaseNotes>* Support ++ and -- as prefix operators
22-
* Support ~ as bitwise complement operator
23-
* Support of the sizeof keyword
24-
* Add hexadecimal and binary notations for int values with 0x and 0b prefixes
25-
* Add the _ (underscore) as digit separator in numbers (all notations)
26-
* Add the following options to have a culture dependant way to evaluate numbers
27-
* CultureInfoForNumberParsing
28-
* OptionNumberParsingDecimalSeparator
29-
* OptionNumberParsingThousandSeparator
30-
* OptionFunctionArgumentsSeparator
31-
* OptionInitializersSeparator
32-
* Some memory management and performance improvement
33-
* Some bugs corrections</PackageReleaseNotes>
21+
<PackageReleaseNotes>* Add OptionForceIntegerNumbersEvaluationsAsDoubleByDefault (if true 1/2 = 0.5 otherwise 1/2 = 0)
22+
* Correction of a conflict between &lt;&gt; operators and &lt;&gt; used for generics
23+
* Correction of a bug when imbricate different brackets with comma separators</PackageReleaseNotes>
3424
</PropertyGroup>
3525
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
3626
<DebugType>full</DebugType>

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/******************************************************************************************************
22
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
Version : 1.3.4.0
3+
Version : 1.3.5.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
@@ -31,7 +31,7 @@ public class ExpressionEvaluator
3131
private const string diactitics = "áàâãåǎăāąæéèêëěēĕėęěìíîïīĭįijóôõöōŏőøðœùúûüǔũūŭůűųýþÿŷıćĉċčçďđĝğġģĥħĵķĺļľŀłńņňŋñŕŗřśŝşšţťŧŵźżžÁÀÂÃÅǍĂĀĄÆÉÈÊËĚĒĔĖĘĚÌÍÎÏĪĬĮIJÓÔÕÖŌŎŐØÐŒÙÚÛÜǓŨŪŬŮŰŲÝÞŸŶIĆĈĊČÇĎĐĜĞĠĢĤĦĴĶĹĻĽĿŁŃŅŇŊÑŔŖŘŚŜŞŠŢŤŦŴŹŻŽß";
3232
private const string diactiticsKeywordsRegexPattern = "a-zA-Z_" + diactitics;
3333

34-
private static readonly Regex varOrFunctionRegEx = new Regex($@"^((?<sign>[+-])|(?<prefixOperator>[+][+]|--)|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*))(?>\s*)((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![{ diactiticsKeywordsRegexPattern}0-9]))|((?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
34+
private static readonly Regex varOrFunctionRegEx = new Regex($@"^((?<sign>[+-])|(?<prefixOperator>[+][+]|--)|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*))(?>\s*)((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![{ diactiticsKeywordsRegexPattern}0-9]))|((?<isgeneric>[<](?>([{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*)|(?>\s+)|[,\.])+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
3535

3636
private 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])?";
3737
private string numberRegexPattern = null;
@@ -527,6 +527,13 @@ private StringComparer StringComparerForCasing
527527
}
528528
}
529529

530+
/// <summary>
531+
/// If <c>true</c> all numbers without decimal and suffixes evaluations will be done as double
532+
/// If <c>false</c> Integers values without decimal and suffixes will be evaluate as int as in C# (Warning some operation can round values)
533+
/// By default = false
534+
/// </summary>
535+
public bool OptionForceIntegerNumbersEvaluationsAsDoubleByDefault { get; set; } = false;
536+
530537
private CultureInfo cultureInfoForNumberParsing = CultureInfo.InvariantCulture.Clone() as CultureInfo;
531538

532539
/// <summary>
@@ -625,21 +632,12 @@ public string OptionNumberParsingThousandSeparator
625632
/// </summary>
626633
public bool OptionFluidPrefixingActive { get; set; } = true;
627634

628-
private bool optionInlineNamespacesEvaluationActive = true;
629-
630635
/// <summary>
631636
/// if <c>true</c> allow the use of inline namespace (Can be slow, and is less secure).
632637
/// if <c>false</c> unactive inline namespace (only namespaces in Namespaces list are available).
633638
/// By default : true
634639
/// </summary>
635-
public bool OptionInlineNamespacesEvaluationActive
636-
{
637-
get { return optionInlineNamespacesEvaluationActive; }
638-
set
639-
{
640-
optionInlineNamespacesEvaluationActive = value;
641-
}
642-
}
640+
public bool OptionInlineNamespacesEvaluationActive { get; set; } = true;
643641

644642
private Func<ExpressionEvaluator, List<string>, object> newMethodMem;
645643

@@ -1514,12 +1512,13 @@ private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref in
15141512
}
15151513
else
15161514
{
1517-
if (numberMatch.Groups["hasdecimal"].Success)
1515+
if (OptionForceIntegerNumbersEvaluationsAsDoubleByDefault || numberMatch.Groups["hasdecimal"].Success)
15181516
{
15191517
stack.Push(double.Parse(numberMatch.Value.Replace("_",""), NumberStyles.Any, CultureInfoForNumberParsing));
15201518
}
15211519
else
15221520
{
1521+
15231522
stack.Push(int.Parse(numberMatch.Value.Replace("_", ""), NumberStyles.Any, CultureInfoForNumberParsing));
15241523
}
15251524
}
@@ -2804,6 +2803,24 @@ private List<string> GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(s
28042803
s = expr.Substring(i, 1);
28052804

28062805
if (s.Equals(startChar)) bracketCount++;
2806+
else if (s.Equals("("))
2807+
{
2808+
i++;
2809+
currentExpression += "(" + GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, false, ",", "(", ")").SingleOrDefault() + ")";
2810+
continue;
2811+
}
2812+
else if (s.Equals("{"))
2813+
{
2814+
i++;
2815+
currentExpression += "{" + GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, false, ",", "{", "}").SingleOrDefault() + "}";
2816+
continue;
2817+
}
2818+
else if (s.Equals("["))
2819+
{
2820+
i++;
2821+
currentExpression += "[" + GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, false, ",", "[", "]").SingleOrDefault() + "]";
2822+
continue;
2823+
}
28072824

28082825
if (s.Equals(endChar))
28092826
{

0 commit comments

Comments
 (0)