@@ -16,10 +16,6 @@ namespace ToolGood.Algorithm.Internals
1616 class MathVisitor : AbstractParseTreeVisitor < Operand > , ImathVisitor < Operand >
1717 {
1818 private static readonly Regex sumifRegex = new Regex ( @"(<|<=|>|>=|=|==|===|!=|!==|<>) *([-+]?\d+(\.(\d+)?)?)" , RegexOptions . Compiled ) ;
19- private static readonly Regex bit_2 = new Regex ( "^[01]+$" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
20- private static readonly Regex bit_8 = new Regex ( "^[0-8]+$" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
21- private static readonly Regex bit_16 = new Regex ( "^[0-9a-f]+$" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
22- private static readonly Regex clearRegex = new Regex ( @"[\f\n\r\t\v]" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
2319 public event Func < string , Operand > GetParameter ;
2420 public event Func < string , List < Operand > , Operand > DiyFunction ;
2521 public int excelIndex ;
@@ -841,7 +837,7 @@ public virtual Operand VisitBIN2OCT_fun(mathParser.BIN2OCT_funContext context)
841837 var firstValue = args [ 0 ] . ToText ( "Function BIN2OCT parameter 1 is error!" ) ;
842838 if ( firstValue . IsError ) { return firstValue ; }
843839
844- if ( bit_2 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function BIN2OCT parameter 1 is error!" ) ; }
840+ if ( Regex . IsMatch ( firstValue . TextValue , "^[01]+$" ) == false ) { return Operand . Error ( "Function BIN2OCT parameter 1 is error!" ) ; }
845841 var num = Convert . ToString ( Convert . ToInt32 ( firstValue . TextValue , 2 ) , 8 ) ;
846842 if ( args . Count == 2 ) {
847843 var secondValue = args [ 1 ] . ToNumber ( "Function BIN2OCT parameter 2 is error!" ) ;
@@ -858,7 +854,7 @@ public virtual Operand VisitBIN2DEC_fun(mathParser.BIN2DEC_funContext context)
858854 var firstValue = context . expr ( ) . Accept ( this ) . ToText ( "Function BIN2DEC parameter is error!" ) ;
859855 if ( firstValue . IsError ) { return firstValue ; }
860856
861- if ( bit_2 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function BIN2DEC parameter is error!" ) ; }
857+ if ( Regex . IsMatch ( firstValue . TextValue , "^[01]+$" ) == false ) { return Operand . Error ( "Function BIN2DEC parameter is error!" ) ; }
862858 var num = Convert . ToInt32 ( firstValue . TextValue , 2 ) ;
863859 return Operand . Create ( num ) ;
864860 }
@@ -870,7 +866,7 @@ public virtual Operand VisitBIN2HEX_fun(mathParser.BIN2HEX_funContext context)
870866 var firstValue = args [ 0 ] . ToText ( "Function BIN2HEX parameter 1 is error!" ) ;
871867 if ( firstValue . IsError ) { return firstValue ; }
872868
873- if ( bit_2 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function BIN2HEX parameter 1 is error!" ) ; }
869+ if ( Regex . IsMatch ( firstValue . TextValue , "^[01]+$" ) == false ) { return Operand . Error ( "Function BIN2HEX parameter 1 is error!" ) ; }
874870 var num = Convert . ToString ( Convert . ToInt32 ( firstValue . TextValue , 2 ) , 16 ) . ToUpper ( ) ;
875871 if ( args . Count == 2 ) {
876872 var secondValue = args [ 1 ] . ToNumber ( "Function BIN2HEX parameter 2 is error!" ) ;
@@ -891,7 +887,7 @@ public virtual Operand VisitOCT2BIN_fun(mathParser.OCT2BIN_funContext context)
891887 var firstValue = args [ 0 ] . ToText ( "Function OCT2BIN parameter 1 is error!" ) ;
892888 if ( firstValue . IsError ) { return firstValue ; }
893889
894- if ( bit_8 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function OCT2BIN parameter 1 is error!" ) ; }
890+ if ( Regex . IsMatch ( firstValue . TextValue , "^[0-8]+$" ) == false ) { return Operand . Error ( "Function OCT2BIN parameter 1 is error!" ) ; }
895891 var num = Convert . ToString ( Convert . ToInt32 ( firstValue . TextValue , 8 ) , 2 ) ;
896892 if ( args . Count == 2 ) {
897893 var secondValue = args [ 1 ] . ToNumber ( "Function OCT2BIN parameter 2 is error!" ) ;
@@ -908,7 +904,7 @@ public virtual Operand VisitOCT2DEC_fun(mathParser.OCT2DEC_funContext context)
908904 var firstValue = context . expr ( ) . Accept ( this ) . ToText ( "Function OCT2DEC parameter is error!" ) ;
909905 if ( firstValue . IsError ) { return firstValue ; }
910906
911- if ( bit_8 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function OCT2DEC parameter is error!" ) ; }
907+ if ( Regex . IsMatch ( firstValue . TextValue , "^[0-8]+$" ) == false ) { return Operand . Error ( "Function OCT2DEC parameter is error!" ) ; }
912908 var num = Convert . ToInt32 ( firstValue . TextValue , 8 ) ;
913909 return Operand . Create ( num ) ;
914910 }
@@ -919,7 +915,7 @@ public virtual Operand VisitOCT2HEX_fun(mathParser.OCT2HEX_funContext context)
919915
920916 var firstValue = args [ 0 ] . ToText ( "Function OCT2HEX parameter 1 is error!" ) ;
921917 if ( firstValue . IsError ) { return firstValue ; }
922- if ( bit_8 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function OCT2HEX parameter 1 is error!" ) ; }
918+ if ( Regex . IsMatch ( firstValue . TextValue , "^[0-8]+$" ) == false ) { return Operand . Error ( "Function OCT2HEX parameter 1 is error!" ) ; }
923919 var num = Convert . ToString ( Convert . ToInt32 ( firstValue . TextValue , 8 ) , 16 ) . ToUpper ( ) ;
924920 if ( args . Count == 2 ) {
925921 var secondValue = args [ 1 ] . ToNumber ( "Function OCT2HEX parameter 2 is error!" ) ;
@@ -993,7 +989,7 @@ public virtual Operand VisitHEX2BIN_fun(mathParser.HEX2BIN_funContext context)
993989
994990 var firstValue = args [ 0 ] . ToText ( "Function HEX2BIN parameter 1 is error!" ) ;
995991 if ( firstValue . IsError ) { return firstValue ; }
996- if ( bit_16 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function HEX2BIN parameter 1 is error!" ) ; }
992+ if ( Regex . IsMatch ( firstValue . TextValue , "^[0-9a-fA-F]+$" ) == false ) { return Operand . Error ( "Function HEX2BIN parameter 1 is error!" ) ; }
997993
998994 var num = Convert . ToString ( Convert . ToInt32 ( firstValue . TextValue , 16 ) , 2 ) ;
999995 if ( args . Count == 2 ) {
@@ -1013,7 +1009,7 @@ public virtual Operand VisitHEX2OCT_fun(mathParser.HEX2OCT_funContext context)
10131009
10141010 var firstValue = args [ 0 ] . ToText ( "Function HEX2OCT parameter 1 is error!" ) ;
10151011 if ( firstValue . IsError ) { return firstValue ; }
1016- if ( bit_16 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function HEX2OCT parameter 1 is error!" ) ; }
1012+ if ( Regex . IsMatch ( firstValue . TextValue , "^[0-9a-fA-F]+$" ) == false ) { return Operand . Error ( "Function HEX2OCT parameter 1 is error!" ) ; }
10171013 var num = Convert . ToString ( Convert . ToInt32 ( firstValue . TextValue , 16 ) , 8 ) ;
10181014 if ( args . Count == 2 ) {
10191015 var secondValue = args [ 1 ] . ToNumber ( "Function HEX2OCT parameter 2 is error!" ) ;
@@ -1030,7 +1026,7 @@ public virtual Operand VisitHEX2DEC_fun(mathParser.HEX2DEC_funContext context)
10301026 var firstValue = context . expr ( ) . Accept ( this ) . ToText ( "Function HEX2DEC parameter is error!" ) ;
10311027 if ( firstValue . IsError ) { return firstValue ; }
10321028
1033- if ( bit_16 . IsMatch ( firstValue . TextValue ) == false ) { return Operand . Error ( "Function HEX2DEC parameter is error!" ) ; }
1029+ if ( Regex . IsMatch ( firstValue . TextValue , "^[0-9a-fA-F]+$" ) == false ) { return Operand . Error ( "Function HEX2DEC parameter is error!" ) ; }
10341030 var num = Convert . ToInt32 ( firstValue . TextValue , 16 ) ;
10351031 return Operand . Create ( num ) ;
10361032 }
@@ -1379,7 +1375,7 @@ public virtual Operand VisitCLEAN_fun(mathParser.CLEAN_funContext context)
13791375 if ( firstValue . IsError ) { return firstValue ; }
13801376
13811377 var t = firstValue . TextValue ;
1382- t = clearRegex . Replace ( t , "" ) ;
1378+ t = Regex . Replace ( t , @"[\f\n\r\t\v]" , "" ) ;
13831379 return Operand . Create ( t ) ;
13841380 }
13851381 public virtual Operand VisitCODE_fun ( mathParser . CODE_funContext context )
0 commit comments