/// <summary> /// Parses an integer from the given string. Leading blanks are ignored. /// Either '+' or '-' are accepted as sign characters and must appear after /// any leading blanks at the beginning of the string. The rest of the string /// must contain digits or blanks. Blanks are treated as either '0' or ignored /// depending on the BlanksAsZero flag in the record. /// </summary> /// <param name="intString">A string containing an integer number</param> /// <param name="record">A FormatRecord specifier</param> /// <returns>The integer value of the string</returns> public static int ParseInteger(string intString, FormatRecord record) { if (intString == null) { throw new ArgumentNullException("intString"); } int intValue = 0; int sign = 1; FormatParser parser = new FormatParser(intString, record); char ch = parser.Next(); if (ch == '+') { ch = parser.Next(); } else if (ch == '-') { sign = -1; ch = parser.Next(); } while (ch != '\0') { if (!Char.IsDigit(ch)) { throw new JComRuntimeException(string.Format("Illegal character {0} in number", ch)); } intValue = (intValue * 10) + (ch - '0'); ch = parser.Next(); } return intValue * sign; }
/// <summary> /// Parses a double from the given string. Leading blanks are ignored. /// Either '+' or '-' are accepted as sign characters and must appear after /// any leading blanks at the beginning of the string. The rest of the string /// must contain a fractional number of the format: /// /// [s][nnn].[fff][Dmm] /// /// where [s] is the optional sign, [nnn] is the optional mantissa and [fff] /// is an optional fraction. If an exponent is specified, it must contain an /// exponentiation value. /// /// If no exponent is explicitly specified then any scale factor is applied to /// the number. /// /// Blanks are treated as either '0' or ignored depending on the BlanksAsZero /// flag in the record. /// </summary> /// <param name="doubleString">A string containing a double precision number</param> /// <param name="record">A FormatRecord containing formatting settings</param> /// <returns>The double precision value of the string</returns> public static double ParseDouble(string doubleString, FormatRecord record) { if (doubleString == null) { throw new ArgumentNullException("doubleString"); } bool inFraction = false; bool hasExponentPart = false; int exponent = 0; long mantissaPart = 0; int sign = 1; FormatParser parser = new FormatParser(doubleString, record); char ch = parser.Next(); if (ch == '+') { ch = parser.Next(); } else if (ch == '-') { sign = -1; ch = parser.Next(); } while (ch != '\0') { if (ch == '.') { if (inFraction) { throw new JComRuntimeException("Fraction already specified"); } inFraction = true; } else { if (!Char.IsDigit(ch)) { break; } mantissaPart = (mantissaPart * 10) + (ch - '0'); if (inFraction) { --exponent; } } ch = parser.Next(); } if ("EeDd".IndexOf(ch) >= 0) { ch = parser.Next(); hasExponentPart = true; } if (hasExponentPart || ch == '+' || ch == '-') { int exponentSign = 1; int exponentPart = 0; if (ch == '+') { ch = parser.Next(); } else if (ch == '-') { exponentSign = -1; ch = parser.Next(); } if (ch == '\0') { throw new JComRuntimeException("Exponent must have a value"); } while (ch != '\0') { if (!Char.IsDigit(ch)) { throw new JComRuntimeException(string.Format("Illegal character {0} in number", ch)); } exponentPart = (exponentPart * 10) + (ch - '0'); ch = parser.Next(); } exponent += (exponentSign * exponentPart); hasExponentPart = true; } else { if (ch != '\0') { throw new JComRuntimeException(string.Format("Illegal character {0} in number", ch)); } } if (!hasExponentPart && record != null) { exponent -= record.ScaleFactor; } bool expSign = false; if (exponent < 0) { expSign = true; exponent = -exponent; } if (exponent > 511) { exponent = 511; } double doubleExponent = 1.0; for (int d = 0; exponent != 0; exponent >>= 1, ++d) { if ((exponent & 1) != 0) { doubleExponent *= powersOf10[d]; } } double fraction = mantissaPart; if (expSign) { fraction /= doubleExponent; } else { fraction *= doubleExponent; } return fraction * sign; }