Skip to content

Commit 9aa43c9

Browse files
author
Sébastien Geiser
committed
sizeof keyword + Correction of ulong missing in regex
1 parent 6482f4c commit 9aa43c9

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,24 @@ public void TypeTesting(string expression, Type type)
517517
[TestCase("typeof(string) == 12.GetType()", ExpectedResult = false, Category = "typeof keyword")]
518518
#endregion
519519

520+
#region sizeof keyword
521+
522+
[TestCase("sizeof(sbyte)", ExpectedResult = sizeof(sbyte), Category = "sizeof keyword")]
523+
[TestCase("sizeof(byte)", ExpectedResult = sizeof(byte), Category = "sizeof keyword")]
524+
[TestCase("sizeof(short)", ExpectedResult = sizeof(short), Category = "sizeof keyword")]
525+
[TestCase("sizeof(ushort)", ExpectedResult = sizeof(ushort), Category = "sizeof keyword")]
526+
[TestCase("sizeof(int)", ExpectedResult = sizeof(int), Category = "sizeof keyword")]
527+
[TestCase("sizeof(uint)", ExpectedResult = sizeof(uint), Category = "sizeof keyword")]
528+
[TestCase("sizeof(long)", ExpectedResult = sizeof(long), Category = "sizeof keyword")]
529+
[TestCase("sizeof(ulong)", ExpectedResult = sizeof(ulong), Category = "sizeof keyword")]
530+
[TestCase("sizeof(char)", ExpectedResult = sizeof(char), Category = "sizeof keyword")]
531+
[TestCase("sizeof(float)", ExpectedResult = sizeof(float), Category = "sizeof keyword")]
532+
[TestCase("sizeof(double)", ExpectedResult = sizeof(double), Category = "sizeof keyword")]
533+
[TestCase("sizeof(decimal)", ExpectedResult = sizeof(decimal), Category = "sizeof keyword")]
534+
[TestCase("sizeof(bool)", ExpectedResult = sizeof(bool), Category = "sizeof keyword")]
535+
536+
#endregion
537+
520538
#region Create instance with new Keyword
521539
[TestCase("new ClassForTest1().GetType()", ExpectedResult = typeof(ClassForTest1), Category = "Create instance with new Keyword")]
522540
[TestCase("new ClassForTest2(15).GetType()", ExpectedResult = typeof(ClassForTest2), Category = "Create instance with new Keyword")]

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Globalization;
1616
using System.Linq;
1717
using System.Reflection;
18+
using System.Runtime.InteropServices;
1819
using System.Text;
1920
using System.Text.RegularExpressions;
2021

@@ -57,7 +58,7 @@ public class ExpressionEvaluator
5758
private string CastRegexPattern { get { return $@"^\(\s*(?<typeName>[{ diactiticsKeywordsRegexPattern }][{ diactiticsKeywordsRegexPattern }0-9{ (OptionInlineNamespacesEvaluationActive ? @"\." : string.Empty) }\[\]<>]*[?]?)\s*\)"; } }
5859
private Regex castRegex = null;
5960

60-
private static readonly string primaryTypesRegexPattern = @"(?<=^|[^" + diactiticsKeywordsRegexPattern + @"])(?<primaryType>object|string|bool[?]?|byte[?]?|char[?]?|decimal[?]?|double[?]?|short[?]?|int[?]?|long[?]?|sbyte[?]?|float[?]?|ushort[?]?|uint[?]?|void)(?=[^a-zA-Z_]|$)";
61+
private static readonly string primaryTypesRegexPattern = @"(?<=^|[^" + diactiticsKeywordsRegexPattern + @"])(?<primaryType>object|string|bool[?]?|byte[?]?|char[?]?|decimal[?]?|double[?]?|short[?]?|int[?]?|long[?]?|sbyte[?]?|float[?]?|ushort[?]?|uint[?]?|ulong[?]?|void)(?=[^a-zA-Z_]|$)";
6162
private Regex primaryTypesRegex = new Regex(primaryTypesRegexPattern);
6263

6364
// To remove comments in scripts based on https://stackoverflow.com/questions/3524317/regex-to-strip-line-comments-from-c-sharp/3524689#3524689
@@ -431,6 +432,18 @@ private enum TryBlockEvaluatedState
431432
}
432433
},
433434
{ "Sign", (self, args) => Math.Sign(Convert.ToDouble(self.Evaluate(args[0]))) },
435+
{ "sizeof", (self, args) =>
436+
{
437+
Type type = ((ClassOrTypeName)self.Evaluate(args[0])).Type;
438+
439+
if(type == typeof(bool))
440+
return 1;
441+
else if(type == typeof(char))
442+
return 2;
443+
else
444+
return Marshal.SizeOf(type);
445+
}
446+
},
434447
{ "typeof", (self, args) => ((ClassOrTypeName)self.Evaluate(args[0])).Type },
435448
};
436449

0 commit comments

Comments
 (0)