Skip to content

Commit 6482f4c

Browse files
author
Sébastien Geiser
committed
Binary numbers and _ separators for all numbers
1 parent a1db064 commit 6482f4c

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ public void TypeTesting(string expression, Type type)
9191

9292
#region Test cases for DirectExpressionEvaluation
9393

94-
#region HexNumber
94+
#region Other bases numbers
9595

9696
[TestCase("0xab", ExpectedResult = 0xab, Category = "HexNumber")]
9797
[TestCase("0xAB", ExpectedResult = 0xab, Category = "HexNumber")]
9898
[TestCase("0x1", ExpectedResult = 0x1, Category = "HexNumber")]
9999
[TestCase("0xf", ExpectedResult = 0xf, Category = "HexNumber")]
100100
[TestCase("-0xf", ExpectedResult = -0xf, Category = "HexNumber")]
101+
[TestCase("0xff_2a", ExpectedResult = 0xff_2a, Category = "HexNumber")]
102+
103+
[TestCase("0b01100111", ExpectedResult = 0b01100111, Category = "BinaryNumber")]
104+
[TestCase("0b0100", ExpectedResult = 0b0100, Category = "BinaryNumber")]
105+
[TestCase("0b1010", ExpectedResult = 0b1010, Category = "BinaryNumber")]
106+
[TestCase("0b10_10", ExpectedResult = 0b10_10, Category = "BinaryNumber")]
107+
[TestCase("-0b10_10", ExpectedResult = -0b10_10, Category = "BinaryNumber")]
101108

102109
#endregion
103110

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 18 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.3.1
3+
Version : 1.3.3.2
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,8 +31,8 @@ public class ExpressionEvaluator
3131
private static readonly string diactiticsKeywordsRegexPattern = "a-zA-Z_" + diactitics;
3232

3333
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 numberRegex = new Regex(@"^(?<sign>[+-])?\d+(?<hasdecimal>\.?\d+(e[+-]?\d+)?)?(?<type>ul|[fdulm])?", RegexOptions.IgnoreCase);
35-
private static readonly Regex hexNumberRegex = new Regex(@"^(?<sign>[+-])?(?<hexValue>0x[0-9a-f]+)", RegexOptions.IgnoreCase);
34+
private static readonly Regex numberRegex = new Regex(@"^(?<sign>[+-])?([0-9][0-9_]*[0-9]|\d)(?<hasdecimal>\.?([0-9][0-9_]*[0-9]|\d)(e[+-]?([0-9][0-9_]*[0-9]|\d))?)?(?<type>ul|[fdulm])?", RegexOptions.IgnoreCase);
35+
private static readonly Regex otherBasesNumberRegex = new Regex(@"^(?<sign>[+-])?(?<value>0(?<type>x)([0-9a-f][0-9a-f_]*[0-9a-f]|[0-9a-f])|0(?<type>b)([01][01_]*[01]|[01]))", RegexOptions.IgnoreCase);
3636
private static readonly Regex stringBeginningRegex = new Regex("^(?<interpolated>[$])?(?<escaped>[@])?[\"]");
3737
private static readonly Regex internalCharRegex = new Regex(@"^['](\\[']|[^'])*[']");
3838
private static readonly Regex indexingBeginningRegex = new Regex(@"^[?]?\[");
@@ -1371,20 +1371,25 @@ private bool EvaluateCast(string restOfExpression, Stack<object> stack, ref int
13711371
private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref int i)
13721372
{
13731373
Match numberMatch = numberRegex.Match(restOfExpression);
1374-
Match hexMatch = hexNumberRegex.Match(restOfExpression);
1374+
Match otherBaseMatch = otherBasesNumberRegex.Match(restOfExpression);
13751375

1376-
if (hexMatch.Success
1377-
&& (!hexMatch.Groups["sign"].Success
1376+
if (otherBaseMatch.Success
1377+
&& (!otherBaseMatch.Groups["sign"].Success
13781378
|| stack.Count == 0
13791379
|| stack.Peek() is ExpressionOperator))
13801380
{
1381-
i += hexMatch.Length;
1381+
i += otherBaseMatch.Length;
13821382
i--;
13831383

1384-
if (hexMatch.Groups["sign"].Success)
1385-
stack.Push(hexMatch.Groups["sign"].Value.Equals("-") ? -Convert.ToInt32(hexMatch.Groups["hexValue"].Value, 16) : Convert.ToInt32(hexMatch.Groups["hexValue"].Value, 16));
1384+
int baseValue = otherBaseMatch.Groups["type"].Value.Equals("b") ? 2 : 16;
1385+
1386+
if (otherBaseMatch.Groups["sign"].Success)
1387+
{
1388+
string value = otherBaseMatch.Groups["value"].Value.Replace("_", "").Substring(2);
1389+
stack.Push(otherBaseMatch.Groups["sign"].Value.Equals("-") ? -Convert.ToInt32(value, baseValue) : Convert.ToInt32(value, baseValue));
1390+
}
13861391
else
1387-
stack.Push(Convert.ToInt32(hexMatch.Value, 16));
1392+
stack.Push(Convert.ToInt32(otherBaseMatch.Value.Replace("_", "").Substring(2), baseValue));
13881393

13891394
return true;
13901395
}
@@ -1399,7 +1404,7 @@ private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref in
13991404
if (numberMatch.Groups["type"].Success)
14001405
{
14011406
string type = numberMatch.Groups["type"].Value;
1402-
string numberNoType = numberMatch.Value.Replace(type, string.Empty);
1407+
string numberNoType = numberMatch.Value.Replace(type, string.Empty).Replace("_", "");
14031408

14041409
if (numberSuffixToParse.TryGetValue(type, out Func<string, object> parseFunc))
14051410
{
@@ -1410,11 +1415,11 @@ private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref in
14101415
{
14111416
if (numberMatch.Groups["hasdecimal"].Success)
14121417
{
1413-
stack.Push(double.Parse(numberMatch.Value, NumberStyles.Any, CultureInfo.InvariantCulture));
1418+
stack.Push(double.Parse(numberMatch.Value.Replace("_",""), NumberStyles.Any, CultureInfo.InvariantCulture));
14141419
}
14151420
else
14161421
{
1417-
stack.Push(int.Parse(numberMatch.Value, NumberStyles.Any, CultureInfo.InvariantCulture));
1422+
stack.Push(int.Parse(numberMatch.Value.Replace("_", ""), NumberStyles.Any, CultureInfo.InvariantCulture));
14181423
}
14191424
}
14201425

0 commit comments

Comments
 (0)