static private ParseCore ( |
||
reader | The reader to read characters from. | |
firstChar | char | The first character of the number. Must be 0-9 or a period. |
status | ParseCoreStatus | Upon returning, contains the type of error if one occurred. |
decimalOnly | bool | |
allowES3Octal | bool | |
Результат | double |
/// <summary> /// Converts a string to a number (used by parseFloat). /// </summary> /// <param name="input"> The string to convert. </param> /// <returns> The result of parsing the string as a number. </returns> internal static double ParseFloat(string input) { var reader = new System.IO.StringReader(input); // Skip whitespace and line terminators. while (IsWhiteSpaceOrLineTerminator(reader.Peek())) { reader.Read(); } // The number can start with a plus or minus sign. bool negative = false; int firstChar = reader.Read(); switch (firstChar) { case '-': negative = true; firstChar = reader.Read(); break; case '+': firstChar = reader.Read(); break; } // Infinity or -Infinity are also valid. if (firstChar == 'I' && reader.ReadToEnd().StartsWith("nfinity", StringComparison.Ordinal) == true) { return(negative ? double.NegativeInfinity : double.PositiveInfinity); } // Empty strings return NaN. if ((firstChar < '0' || firstChar > '9') && firstChar != '.') { return(double.NaN); } // Parse the number. NumberParser.ParseCoreStatus status; double result = NumberParser.ParseCore(reader, (char)firstChar, out status, allowHex: false, allowOctal: false); // Handle various error cases. if (status == ParseCoreStatus.NoDigits) { return(double.NaN); } return(negative ? -result : result); }
/// <summary> /// Converts a string to a number (used in type coercion). /// </summary> /// <returns> The result of parsing the string as a number. </returns> internal static double CoerceToNumber(string input) { var reader = new System.IO.StringReader(input); // Skip whitespace and line terminators. while (IsWhiteSpaceOrLineTerminator(reader.Peek())) { reader.Read(); } // Empty strings return 0. int firstChar = reader.Read(); if (firstChar == -1) { return(0.0); } // The number can start with a plus or minus sign. bool negative = false; switch (firstChar) { case '-': negative = true; firstChar = reader.Read(); break; case '+': firstChar = reader.Read(); break; } // Infinity or -Infinity are also valid. if (firstChar == 'I') { string restOfString1 = reader.ReadToEnd(); if (restOfString1.StartsWith("nfinity", StringComparison.Ordinal) == true) { // Check the end of the string for junk. for (int i = 7; i < restOfString1.Length; i++) { if (IsWhiteSpaceOrLineTerminator(restOfString1[i]) == false) { return(double.NaN); } } return(negative ? double.NegativeInfinity : double.PositiveInfinity); } } // Return NaN if the first digit is not a number or a period. if ((firstChar < '0' || firstChar > '9') && firstChar != '.') { return(double.NaN); } // Parse the number. NumberParser.ParseCoreStatus status; double result = NumberParser.ParseCore(reader, (char)firstChar, out status, allowHex: true, allowOctal: false); // Handle various error cases. switch (status) { case ParseCoreStatus.NoDigits: case ParseCoreStatus.NoExponent: return(double.NaN); } // Check the end of the string for junk. string restOfString2 = reader.ReadToEnd(); for (int i = 0; i < restOfString2.Length; i++) { if (IsWhiteSpaceOrLineTerminator(restOfString2[i]) == false) { return(double.NaN); } } return(negative ? -result : result); }