private static bool TryParse(string s, out MathContext context, out Exception exception) { char[] charVal = s.ToCharArray(); int i; // Index of charVal int j; // Index of chRoundingMode int digit; // It will contain the digit parsed if ((charVal.Length < 27) || (charVal.Length > 45)) { // math.0E=bad string format exception = new FormatException(Messages.math0E); //$NON-NLS-1$ context = null; return(false); } // Parsing "precision=" String for (i = 0; (i < chPrecision.Length) && (charVal[i] == chPrecision[i]); i++) { } if (i < chPrecision.Length) { // math.0E=bad string format throw new FormatException(Messages.math0E); //$NON-NLS-1$ } // Parsing the value for "precision="... digit = CharHelper.toDigit(charVal[i], 10); if (digit == -1) { // math.0E=bad string format exception = new FormatException(Messages.math0E); //$NON-NLS-1$ context = null; return(false); } var precision = digit; i++; do { digit = CharHelper.toDigit(charVal[i], 10); if (digit == -1) { if (charVal[i] == ' ') { // It parsed all the digits i++; break; } // It isn't a valid digit, and isn't a white space // math.0E=bad string format exception = new ArgumentException(Messages.math0E); //$NON-NLS-1$ context = null; return(false); } // Accumulating the value parsed precision = precision * 10 + digit; if (precision < 0) { // math.0E=bad string format exception = new ArgumentException(Messages.math0E); //$NON-NLS-1$ context = null; return(false); } i++; } while (true); // Parsing "roundingMode=" for (j = 0; (j < chRoundingMode.Length) && (charVal[i] == chRoundingMode[j]); i++, j++) { ; } if (j < chRoundingMode.Length) { // math.0E=bad string format throw new FormatException(Messages.math0E); //$NON-NLS-1$ } RoundingMode roundingMode; try { // Parsing the value for "roundingMode"... roundingMode = (RoundingMode)Enum.Parse(typeof(RoundingMode), new string(charVal, i, charVal.Length - i), true); } catch (Exception ex) { exception = ex; context = null; return(false); } exception = null; context = new MathContext(precision, roundingMode); return(true); }
/** * Constructs a new {@code MathContext} from a string. The string has to * specify the precision and the rounding mode to be used and has to follow * the following syntax: "precision=<precision> roundingMode=<roundingMode>" * This is the same form as the one returned by the {@link #ToString} * method. * * @param val * a string describing the precision and rounding mode for the * new {@code MathContext}. * @throws IllegalArgumentException * if the string is not in the correct format or if the * precision specified is < 0. */ public MathContext(String val) { char[] charVal = val.ToCharArray(); int i; // Index of charVal int j; // Index of chRoundingMode int digit; // It will contain the digit parsed if ((charVal.Length < 27) || (charVal.Length > 45)) { // math.0E=bad string format throw new ArgumentException(Messages.math0E); //$NON-NLS-1$ } // Parsing "precision=" String for (i = 0; (i < chPrecision.Length) && (charVal[i] == chPrecision[i]); i++) { ; } if (i < chPrecision.Length) { // math.0E=bad string format throw new ArgumentException(Messages.math0E); //$NON-NLS-1$ } // Parsing the value for "precision="... digit = CharHelper.toDigit(charVal[i], 10); if (digit == -1) { // math.0E=bad string format throw new ArgumentException(Messages.math0E); //$NON-NLS-1$ } this.precision = this.precision * 10 + digit; i++; do { digit = CharHelper.toDigit(charVal[i], 10); if (digit == -1) { if (charVal[i] == ' ') { // It parsed all the digits i++; break; } // It isn't a valid digit, and isn't a white space // math.0E=bad string format throw new ArgumentException(Messages.math0E); //$NON-NLS-1$ } // Accumulating the value parsed this.precision = this.precision * 10 + digit; if (this.precision < 0) { // math.0E=bad string format throw new ArgumentException(Messages.math0E); //$NON-NLS-1$ } i++; } while (true); // Parsing "roundingMode=" for (j = 0; (j < chRoundingMode.Length) && (charVal[i] == chRoundingMode[j]); i++, j++) { ; } if (j < chRoundingMode.Length) { // math.0E=bad string format throw new ArgumentException(Messages.math0E); //$NON-NLS-1$ } // Parsing the value for "roundingMode"... this.roundingMode = (RoundingMode)Enum.Parse(typeof(RoundingMode), new string(charVal, i, charVal.Length - i), true); }