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