rt/emul/mini/src/main/java/java/lang/Integer.java
changeset 1837 62b289bb87c7
parent 772 d382dacfd73f
     1.1 --- a/rt/emul/mini/src/main/java/java/lang/Integer.java	Tue Feb 26 16:54:16 2013 +0100
     1.2 +++ b/rt/emul/mini/src/main/java/java/lang/Integer.java	Tue Jul 07 07:46:52 2015 +0200
     1.3 @@ -442,71 +442,17 @@
     1.4       * @exception  NumberFormatException if the {@code String}
     1.5       *             does not contain a parsable {@code int}.
     1.6       */
     1.7 +    public static int parseInt(String s, int radix)
     1.8 +    throws NumberFormatException {
     1.9 +        double val = parseInt0(s, radix);
    1.10 +        if (Double.isNaN(val) || s.contains(".")) {
    1.11 +            throw new NumberFormatException("For input string: \"" + s + '\"');
    1.12 +        }
    1.13 +        return (int)val;
    1.14 +    }
    1.15 +
    1.16      @JavaScriptBody(args={"s", "radix"}, body="return parseInt(s,radix);")
    1.17 -    public static int parseInt(String s, int radix)
    1.18 -                throws NumberFormatException
    1.19 -    {
    1.20 -        /*
    1.21 -         * WARNING: This method may be invoked early during VM initialization
    1.22 -         * before IntegerCache is initialized. Care must be taken to not use
    1.23 -         * the valueOf method.
    1.24 -         */
    1.25 -
    1.26 -        if (s == null) {
    1.27 -            throw new NumberFormatException("null");
    1.28 -        }
    1.29 -
    1.30 -        if (radix < Character.MIN_RADIX) {
    1.31 -            throw new NumberFormatException("radix " + radix +
    1.32 -                                            " less than Character.MIN_RADIX");
    1.33 -        }
    1.34 -
    1.35 -        if (radix > Character.MAX_RADIX) {
    1.36 -            throw new NumberFormatException("radix " + radix +
    1.37 -                                            " greater than Character.MAX_RADIX");
    1.38 -        }
    1.39 -
    1.40 -        int result = 0;
    1.41 -        boolean negative = false;
    1.42 -        int i = 0, len = s.length();
    1.43 -        int limit = -Integer.MAX_VALUE;
    1.44 -        int multmin;
    1.45 -        int digit;
    1.46 -
    1.47 -        if (len > 0) {
    1.48 -            char firstChar = s.charAt(0);
    1.49 -            if (firstChar < '0') { // Possible leading "+" or "-"
    1.50 -                if (firstChar == '-') {
    1.51 -                    negative = true;
    1.52 -                    limit = Integer.MIN_VALUE;
    1.53 -                } else if (firstChar != '+')
    1.54 -                    throw NumberFormatException.forInputString(s);
    1.55 -
    1.56 -                if (len == 1) // Cannot have lone "+" or "-"
    1.57 -                    throw NumberFormatException.forInputString(s);
    1.58 -                i++;
    1.59 -            }
    1.60 -            multmin = limit / radix;
    1.61 -            while (i < len) {
    1.62 -                // Accumulating negatively avoids surprises near MAX_VALUE
    1.63 -                digit = Character.digit(s.charAt(i++),radix);
    1.64 -                if (digit < 0) {
    1.65 -                    throw NumberFormatException.forInputString(s);
    1.66 -                }
    1.67 -                if (result < multmin) {
    1.68 -                    throw NumberFormatException.forInputString(s);
    1.69 -                }
    1.70 -                result *= radix;
    1.71 -                if (result < limit + digit) {
    1.72 -                    throw NumberFormatException.forInputString(s);
    1.73 -                }
    1.74 -                result -= digit;
    1.75 -            }
    1.76 -        } else {
    1.77 -            throw NumberFormatException.forInputString(s);
    1.78 -        }
    1.79 -        return negative ? result : -result;
    1.80 -    }
    1.81 +    private static native double parseInt0(String s, int radix);
    1.82  
    1.83      /**
    1.84       * Parses the string argument as a signed decimal integer. The