Skip to content

Commit a1db064

Browse files
author
Sébastien Geiser
committed
Support for Hex numbers
1 parent 41bfc88 commit a1db064

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ public void TypeTesting(string expression, Type type)
9191

9292
#region Test cases for DirectExpressionEvaluation
9393

94+
#region HexNumber
95+
96+
[TestCase("0xab", ExpectedResult = 0xab, Category = "HexNumber")]
97+
[TestCase("0xAB", ExpectedResult = 0xab, Category = "HexNumber")]
98+
[TestCase("0x1", ExpectedResult = 0x1, Category = "HexNumber")]
99+
[TestCase("0xf", ExpectedResult = 0xf, Category = "HexNumber")]
100+
[TestCase("-0xf", ExpectedResult = -0xf, Category = "HexNumber")]
101+
102+
#endregion
103+
94104
#region Null Expression
95105
[TestCase("null", ExpectedResult = null, Category = "Null Expression")]
96106
#endregion

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ExpressionEvaluator
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);
3434
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);
3536
private static readonly Regex stringBeginningRegex = new Regex("^(?<interpolated>[$])?(?<escaped>[@])?[\"]");
3637
private static readonly Regex internalCharRegex = new Regex(@"^['](\\[']|[^'])*[']");
3738
private static readonly Regex indexingBeginningRegex = new Regex(@"^[?]?\[");
@@ -50,7 +51,6 @@ public class ExpressionEvaluator
5051
private static readonly Regex initInNewBeginningRegex = new Regex(@"^\s*{");
5152
private static readonly Regex OtherDimentionArrayInNewBeginningRegex = new Regex(@"^\s*\[");
5253

53-
5454
// Depending on OptionInlineNamespacesEvaluationActive. Initialized in constructor
5555
private string InstanceCreationWithNewKeywordRegexPattern { get { return $@"^new\s+(?<name>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern}0-9{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?\s*((?<isfunction>[(])|(?<isArray>\[)|(?<isInit>[{{]))?"; } }
5656
private Regex instanceCreationWithNewKeywordRegex = null;
@@ -1371,8 +1371,24 @@ 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);
1375+
1376+
if (hexMatch.Success
1377+
&& (!hexMatch.Groups["sign"].Success
1378+
|| stack.Count == 0
1379+
|| stack.Peek() is ExpressionOperator))
1380+
{
1381+
i += hexMatch.Length;
1382+
i--;
1383+
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));
1386+
else
1387+
stack.Push(Convert.ToInt32(hexMatch.Value, 16));
13741388

1375-
if (numberMatch.Success
1389+
return true;
1390+
}
1391+
else if (numberMatch.Success
13761392
&& (!numberMatch.Groups["sign"].Success
13771393
|| stack.Count == 0
13781394
|| stack.Peek() is ExpressionOperator))

0 commit comments

Comments
 (0)