1
1
/******************************************************************************************************
2
2
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3
- Version : 1.3.3.1
3
+ Version : 1.3.3.2
4
4
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
5
5
6
6
Author : Coding Seb
@@ -31,8 +31,8 @@ public class ExpressionEvaluator
31
31
private static readonly string diactiticsKeywordsRegexPattern = "a-zA-Z_" + diactitics ;
32
32
33
33
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 ) ;
36
36
private static readonly Regex stringBeginningRegex = new Regex ( "^(?<interpolated>[$])?(?<escaped>[@])?[\" ]" ) ;
37
37
private static readonly Regex internalCharRegex = new Regex ( @"^['](\\[']|[^'])*[']" ) ;
38
38
private static readonly Regex indexingBeginningRegex = new Regex ( @"^[?]?\[" ) ;
@@ -1371,20 +1371,25 @@ private bool EvaluateCast(string restOfExpression, Stack<object> stack, ref int
1371
1371
private bool EvaluateNumber ( string restOfExpression , Stack < object > stack , ref int i )
1372
1372
{
1373
1373
Match numberMatch = numberRegex . Match ( restOfExpression ) ;
1374
- Match hexMatch = hexNumberRegex . Match ( restOfExpression ) ;
1374
+ Match otherBaseMatch = otherBasesNumberRegex . Match ( restOfExpression ) ;
1375
1375
1376
- if ( hexMatch . Success
1377
- && ( ! hexMatch . Groups [ "sign" ] . Success
1376
+ if ( otherBaseMatch . Success
1377
+ && ( ! otherBaseMatch . Groups [ "sign" ] . Success
1378
1378
|| stack . Count == 0
1379
1379
|| stack . Peek ( ) is ExpressionOperator ) )
1380
1380
{
1381
- i += hexMatch . Length ;
1381
+ i += otherBaseMatch . Length ;
1382
1382
i -- ;
1383
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 ) ) ;
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
+ }
1386
1391
else
1387
- stack . Push ( Convert . ToInt32 ( hexMatch . Value , 16 ) ) ;
1392
+ stack . Push ( Convert . ToInt32 ( otherBaseMatch . Value . Replace ( "_" , "" ) . Substring ( 2 ) , baseValue ) ) ;
1388
1393
1389
1394
return true ;
1390
1395
}
@@ -1399,7 +1404,7 @@ private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref in
1399
1404
if ( numberMatch . Groups [ "type" ] . Success )
1400
1405
{
1401
1406
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 ( "_" , "" ) ;
1403
1408
1404
1409
if ( numberSuffixToParse . TryGetValue ( type , out Func < string , object > parseFunc ) )
1405
1410
{
@@ -1410,11 +1415,11 @@ private bool EvaluateNumber(string restOfExpression, Stack<object> stack, ref in
1410
1415
{
1411
1416
if ( numberMatch . Groups [ "hasdecimal" ] . Success )
1412
1417
{
1413
- stack . Push ( double . Parse ( numberMatch . Value , NumberStyles . Any , CultureInfo . InvariantCulture ) ) ;
1418
+ stack . Push ( double . Parse ( numberMatch . Value . Replace ( "_" , "" ) , NumberStyles . Any , CultureInfo . InvariantCulture ) ) ;
1414
1419
}
1415
1420
else
1416
1421
{
1417
- stack . Push ( int . Parse ( numberMatch . Value , NumberStyles . Any , CultureInfo . InvariantCulture ) ) ;
1422
+ stack . Push ( int . Parse ( numberMatch . Value . Replace ( "_" , "" ) , NumberStyles . Any , CultureInfo . InvariantCulture ) ) ;
1418
1423
}
1419
1424
}
1420
1425
0 commit comments