示例#1
0
 /// True if str is a number
 public static bool IsNumber(string str)
 {
     if (str == null)
     {
         throw new ArgumentNullException("str");
     }
     using (var sr = new ParsingReader(str))
     {
         if (sr.ReadNumber() == null)
         {
             return(false);
         }
         sr.SkipWhiteSpaceAndComments();
         if (sr.Peek() != -1)
         {
             return(false);
         }
     }
     return(true);
 }
示例#2
0
 /// Returns null if str is not a number, or its value otherwise
 public static ValueType TryParseNumber(string stringToParse)
 {
     if (stringToParse == null)
     {
         return(null);
     }
     using (var sr = new ParsingReader(stringToParse))
     {
         sr.SkipWhiteSpaceAndComments();
         ValueType o = sr.ReadNumber();
         if (o == null)
         {
             return(null);
         }
         sr.SkipWhiteSpaceAndComments();
         if (sr.Peek() != -1)
         {
             return(null);
         }
         return(o);
     }
 }
示例#3
0
 /// Parse string to a number. 10 and hex numbers (like 0x222) are allowed. Suffixes like 20.3f are also allowed.
 public static ValueType ParseNumber(string str)
 {
     if (str == null)
     {
         throw new ArgumentNullException("str");
     }
     using (var sr = new ParsingReader(str))
     {
         sr.SkipWhiteSpaceAndComments();
         ValueType o = sr.ReadNumber();
         if (o == null)
         {
             throw new ParsingException("Invalid numeric expression at " + sr.ReadLine());
         }
         sr.SkipWhiteSpaceAndComments();
         if (sr.Peek() != -1)
         {
             throw new ParsingException("Invalid numeric expression, unexpected characters at " + sr.ReadLine());
         }
         return(o);
     }
 }