Source/NFX/CodeAnalysis/CSharp/CSNumbers.cs
/// <summary>
/// Tries to convert string to number, returns null if conversion could not be made.
/// Raises exception if operand is wider than specifier allows
/// </summary>
Tries to convert string to number, returns null if conversion could not be made.
Raises exception if operand is wider than specifier allows
Tries to convert string to number returns null if conversion could not be made . Raises exception if operand is wider than specifier allows
public static object Convert(string str, out CSTokenType type)
    {
      ulong inum = 0;
      double fnum = 0.0;
      decimal dnum = 0.0M;
      if (strToInt(str, out inum))
      {
        if (inum > UInt32.MaxValue)
          type = CSTokenType.tLongIntLiteral;
        else
          type = CSTokenType.tIntLiteral;
        return inum;
      }
      if (strToFloat(str, out fnum))
      {
        if (inum > Single.MaxValue)
          type = CSTokenType.tDoubleLiteral;
        else
          type = CSTokenType.tFloatLiteral;
        return fnum;
      }
      type = CSTokenType.tUnknown;
      str = str.ToUpperInvariant();
      if (str.EndsWith(S_ULONG1))
      {
        str = str.Replace(S_ULONG1, string.Empty);
        type = CSTokenType.tULongIntLiteral;
      }
      else if (str.EndsWith(S_ULONG2))
      {
        str = str.Replace(S_ULONG2, string.Empty);
        type = CSTokenType.tULongIntLiteral;
      }
      else if (str.EndsWith(S_UINT))
      {
        str = str.Replace(S_UINT, string.Empty);
        type = CSTokenType.tUIntLiteral;
      }
      else if (str.EndsWith(S_LONG))
      {
        str = str.Replace(S_LONG, string.Empty);
        type = CSTokenType.tLongIntLiteral;
      }
      else if (str.EndsWith(S_FLOAT))
      {
        str = str.Replace(S_FLOAT, string.Empty);
        type = CSTokenType.tFloatLiteral;
      }
       else if (str.EndsWith(S_DOUBLE))
      {
        str = str.Replace(S_DOUBLE, string.Empty);
        type = CSTokenType.tDoubleLiteral;
      }
      else if (str.EndsWith(S_DECIMAL))
      {
        str = str.Replace(S_DECIMAL, string.Empty);
        type = CSTokenType.tDecimalLiteral;
      }
      else
        return null;
      if ((type != CSTokenType.tDecimalLiteral) &&
          (type != CSTokenType.tFloatLiteral) &&
          (type != CSTokenType.tDoubleLiteral))
      {
        if (strToInt(str, out inum))
        {
          if (inum > UInt32.MaxValue)
            throw new ArgumentException(S_UINT + ": " + inum);
          return inum;
        }
      }
      else if (type != CSTokenType.tDecimalLiteral)
      {
        if (strToFloat(str, out fnum))
        {
          if (
             (type == CSTokenType.tFloatLiteral) &&
             (fnum > float.MaxValue)
             ) throw new ArgumentException(S_FLOAT + ": " + fnum);
          return fnum;
        }
      }
      else if (strToDecimal(str, out dnum))
      {
        return dnum;
      }
      return null;
    }
public static object Convert ( string str , out CSTokenType type ) { ulong inum = 0 ; double fnum = 0.0 ; decimal dnum = 0.0M ; if ( strToInt ( str , out inum ) ) { if ( inum > UInt32 . MaxValue ) type = CSTokenType . tLongIntLiteral ; else type = CSTokenType . tIntLiteral ; return inum ; } if ( strToFloat ( str , out fnum ) ) { if ( inum > Single . MaxValue ) type = CSTokenType . tDoubleLiteral ; else type = CSTokenType . tFloatLiteral ; return fnum ; } type = CSTokenType . tUnknown ; str = str . ToUpperInvariant ( ) ; if ( str . EndsWith ( S_ULONG1 ) ) { str = str . Replace ( S_ULONG1 , string . Empty ) ; type = CSTokenType . tULongIntLiteral ; } else if ( str . EndsWith ( S_ULONG2 ) ) { str = str . Replace ( S_ULONG2 , string . Empty ) ; type = CSTokenType . tULongIntLiteral ; } else if ( str . EndsWith ( S_UINT ) ) { str = str . Replace ( S_UINT , string . Empty ) ; type = CSTokenType . tUIntLiteral ; } else if ( str . EndsWith ( S_LONG ) ) { str = str . Replace ( S_LONG , string . Empty ) ; type = CSTokenType . tLongIntLiteral ; } else if ( str . EndsWith ( S_FLOAT ) ) { str = str . Replace ( S_FLOAT , string . Empty ) ; type = CSTokenType . tFloatLiteral ; } else if ( str . EndsWith ( S_DOUBLE ) ) { str = str . Replace ( S_DOUBLE , string . Empty ) ; type = CSTokenType . tDoubleLiteral ; } else if ( str . EndsWith ( S_DECIMAL ) ) { str = str . Replace ( S_DECIMAL , string . Empty ) ; type = CSTokenType . tDecimalLiteral ; } else return null ; if ( ( type != CSTokenType . tDecimalLiteral ) && ( type != CSTokenType . tFloatLiteral ) && ( type != CSTokenType . tDoubleLiteral ) ) { if ( strToInt ( str , out inum ) ) { if ( inum > UInt32 . MaxValue ) throw new ArgumentException ( S_UINT + " " + inum ) ; return inum ; } } else if ( type != CSTokenType . tDecimalLiteral ) { if ( strToFloat ( str , out fnum ) ) { if ( ( type == CSTokenType . tFloatLiteral ) && ( fnum > float . MaxValue ) ) throw new ArgumentException ( S_FLOAT + " " + fnum ) ; return fnum ; } } else if ( strToDecimal ( str , out dnum ) ) { return dnum ; } return null ; }
Tries to convert string to number, returns null if conversion could not be made.
Tries to convert string to number returns null if conversion could not be made .
//No XLAT Hashtable on purpose , speed and simplicity //order of IFs is important because some types have the same endings //safeguard - this will never happen unless system supports 128bit arithmetic //caught by lexer //caught by lexer
str
string
CSTokenType
out