jdk/src/java.base/share/classes/java/lang/Integer.java
changeset 26462 d6d34934be12
parent 25859 3317bb8137f4
child 28059 e576535359cc
equal deleted inserted replaced
26461:47fae3bfe8ed 26462:d6d34934be12
   593     }
   593     }
   594 
   594 
   595     /**
   595     /**
   596      * Parses the {@link CharSequence} argument as a signed {@code int} in the
   596      * Parses the {@link CharSequence} argument as a signed {@code int} in the
   597      * specified {@code radix}, beginning at the specified {@code beginIndex}
   597      * specified {@code radix}, beginning at the specified {@code beginIndex}
   598      * and extending to the end of the sequence.
   598      * and extending to {@code endIndex - 1}.
   599      *
   599      *
   600      * <p>The method does not take steps to guard against the
   600      * <p>The method does not take steps to guard against the
   601      * {@code CharSequence} being mutated while parsing.
   601      * {@code CharSequence} being mutated while parsing.
   602      *
   602      *
   603      * @param      s   the {@code CharSequence} containing the {@code int}
   603      * @param      s   the {@code CharSequence} containing the {@code int}
   604      *                  representation to be parsed
   604      *                  representation to be parsed
   605      * @param      radix   the radix to be used while parsing {@code s}.
       
   606      * @param      beginIndex   the beginning index, inclusive.
       
   607      * @return     the signed {@code int} represented by the subsequence in
       
   608      *             the specified radix.
       
   609      * @throws     NullPointerException  if {@code s} is null.
       
   610      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
       
   611      *             negative, or if {@code beginIndex} is greater than
       
   612      *             {@code s.length()}.
       
   613      * @throws     NumberFormatException  if the {@code CharSequence} does not
       
   614      *             contain a parsable {@code int} in the specified
       
   615      *             {@code radix}, or if {@code radix} is either smaller than
       
   616      *             {@link java.lang.Character#MIN_RADIX} or larger than
       
   617      *             {@link java.lang.Character#MAX_RADIX}.
       
   618      * @since  1.9
       
   619      */
       
   620     public static int parseInt(CharSequence s, int radix, int beginIndex)
       
   621                 throws NumberFormatException {
       
   622         // forces an implicit null check of s
       
   623         return parseInt(s, radix, beginIndex, s.length());
       
   624     }
       
   625 
       
   626     /**
       
   627      * Parses the {@link CharSequence} argument as a signed {@code int} in the
       
   628      * specified {@code radix}, beginning at the specified {@code beginIndex}
       
   629      * and extending to {@code endIndex - 1}.
       
   630      *
       
   631      * <p>The method does not take steps to guard against the
       
   632      * {@code CharSequence} being mutated while parsing.
       
   633      *
       
   634      * @param      s   the {@code CharSequence} containing the {@code int}
       
   635      *                  representation to be parsed
       
   636      * @param      radix   the radix to be used while parsing {@code s}.
       
   637      * @param      beginIndex   the beginning index, inclusive.
   605      * @param      beginIndex   the beginning index, inclusive.
   638      * @param      endIndex     the ending index, exclusive.
   606      * @param      endIndex     the ending index, exclusive.
       
   607      * @param      radix   the radix to be used while parsing {@code s}.
   639      * @return     the signed {@code int} represented by the subsequence in
   608      * @return     the signed {@code int} represented by the subsequence in
   640      *             the specified radix.
   609      *             the specified radix.
   641      * @throws     NullPointerException  if {@code s} is null.
   610      * @throws     NullPointerException  if {@code s} is null.
   642      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
   611      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
   643      *             negative, or if {@code beginIndex} is greater than
   612      *             negative, or if {@code beginIndex} is greater than
   648      *             {@code radix}, or if {@code radix} is either smaller than
   617      *             {@code radix}, or if {@code radix} is either smaller than
   649      *             {@link java.lang.Character#MIN_RADIX} or larger than
   618      *             {@link java.lang.Character#MIN_RADIX} or larger than
   650      *             {@link java.lang.Character#MAX_RADIX}.
   619      *             {@link java.lang.Character#MAX_RADIX}.
   651      * @since  1.9
   620      * @since  1.9
   652      */
   621      */
   653     public static int parseInt(CharSequence s, int radix, int beginIndex, int endIndex)
   622     public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
   654                 throws NumberFormatException {
   623                 throws NumberFormatException {
   655         s = Objects.requireNonNull(s);
   624         s = Objects.requireNonNull(s);
   656 
   625 
   657         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   626         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   658             throw new IndexOutOfBoundsException();
   627             throw new IndexOutOfBoundsException();
   688             }
   657             }
   689             int multmin = limit / radix;
   658             int multmin = limit / radix;
   690             int result = 0;
   659             int result = 0;
   691             while (i < endIndex) {
   660             while (i < endIndex) {
   692                 // Accumulating negatively avoids surprises near MAX_VALUE
   661                 // Accumulating negatively avoids surprises near MAX_VALUE
   693                 int digit = Character.digit(s.charAt(i++), radix);
   662                 int digit = Character.digit(s.charAt(i), radix);
   694                 if (digit < 0 || result < multmin) {
   663                 if (digit < 0 || result < multmin) {
   695                     throw NumberFormatException.forCharSequence(s, beginIndex,
   664                     throw NumberFormatException.forCharSequence(s, beginIndex,
   696                             endIndex, i);
   665                             endIndex, i);
   697                 }
   666                 }
   698                 result *= radix;
   667                 result *= radix;
   699                 if (result < limit + digit) {
   668                 if (result < limit + digit) {
   700                     throw NumberFormatException.forCharSequence(s, beginIndex,
   669                     throw NumberFormatException.forCharSequence(s, beginIndex,
   701                             endIndex, i);
   670                             endIndex, i);
   702                 }
   671                 }
       
   672                 i++;
   703                 result -= digit;
   673                 result -= digit;
   704             }
   674             }
   705             return negative ? result : -result;
   675             return negative ? result : -result;
   706         } else {
   676         } else {
   707             throw NumberFormatException.forInputString("");
   677             throw NumberFormatException.forInputString("");
   806     }
   776     }
   807 
   777 
   808     /**
   778     /**
   809      * Parses the {@link CharSequence} argument as an unsigned {@code int} in
   779      * Parses the {@link CharSequence} argument as an unsigned {@code int} in
   810      * the specified {@code radix}, beginning at the specified
   780      * the specified {@code radix}, beginning at the specified
   811      * {@code beginIndex} and extending to the end of the sequence.
   781      * {@code beginIndex} and extending to {@code endIndex - 1}.
   812      *
   782      *
   813      * <p>The method does not take steps to guard against the
   783      * <p>The method does not take steps to guard against the
   814      * {@code CharSequence} being mutated while parsing.
   784      * {@code CharSequence} being mutated while parsing.
   815      *
   785      *
   816      * @param      s   the {@code CharSequence} containing the unsigned
   786      * @param      s   the {@code CharSequence} containing the unsigned
   817      *                 {@code int} representation to be parsed
   787      *                 {@code int} representation to be parsed
   818      * @param      radix   the radix to be used while parsing {@code s}.
       
   819      * @param      beginIndex   the beginning index, inclusive.
       
   820      * @return     the unsigned {@code int} represented by the subsequence in
       
   821      *             the specified radix.
       
   822      * @throws     NullPointerException  if {@code s} is null.
       
   823      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
       
   824      *             negative, or if {@code beginIndex} is greater than
       
   825      *             {@code s.length()}.
       
   826      * @throws     NumberFormatException  if the {@code CharSequence} does not
       
   827      *             contain a parsable unsigned {@code int} in the specified
       
   828      *             {@code radix}, or if {@code radix} is either smaller than
       
   829      *             {@link java.lang.Character#MIN_RADIX} or larger than
       
   830      *             {@link java.lang.Character#MAX_RADIX}.
       
   831      * @since  1.9
       
   832      */
       
   833     public static int parseUnsignedInt(CharSequence s, int radix, int beginIndex)
       
   834                 throws NumberFormatException {
       
   835         // forces an implicit null check of s
       
   836         return parseUnsignedInt(s, radix, beginIndex, s.length());
       
   837     }
       
   838 
       
   839     /**
       
   840      * Parses the {@link CharSequence} argument as an unsigned {@code int} in
       
   841      * the specified {@code radix}, beginning at the specified
       
   842      * {@code beginIndex} and extending to {@code endIndex - 1}.
       
   843      *
       
   844      * <p>The method does not take steps to guard against the
       
   845      * {@code CharSequence} being mutated while parsing.
       
   846      *
       
   847      * @param      s   the {@code CharSequence} containing the unsigned
       
   848      *                 {@code int} representation to be parsed
       
   849      * @param      radix   the radix to be used while parsing {@code s}.
       
   850      * @param      beginIndex   the beginning index, inclusive.
   788      * @param      beginIndex   the beginning index, inclusive.
   851      * @param      endIndex     the ending index, exclusive.
   789      * @param      endIndex     the ending index, exclusive.
       
   790      * @param      radix   the radix to be used while parsing {@code s}.
   852      * @return     the unsigned {@code int} represented by the subsequence in
   791      * @return     the unsigned {@code int} represented by the subsequence in
   853      *             the specified radix.
   792      *             the specified radix.
   854      * @throws     NullPointerException  if {@code s} is null.
   793      * @throws     NullPointerException  if {@code s} is null.
   855      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
   794      * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
   856      *             negative, or if {@code beginIndex} is greater than
   795      *             negative, or if {@code beginIndex} is greater than
   861      *             {@code radix}, or if {@code radix} is either smaller than
   800      *             {@code radix}, or if {@code radix} is either smaller than
   862      *             {@link java.lang.Character#MIN_RADIX} or larger than
   801      *             {@link java.lang.Character#MIN_RADIX} or larger than
   863      *             {@link java.lang.Character#MAX_RADIX}.
   802      *             {@link java.lang.Character#MAX_RADIX}.
   864      * @since  1.9
   803      * @since  1.9
   865      */
   804      */
   866     public static int parseUnsignedInt(CharSequence s, int radix, int beginIndex, int endIndex)
   805     public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
   867                 throws NumberFormatException {
   806                 throws NumberFormatException {
   868         s = Objects.requireNonNull(s);
   807         s = Objects.requireNonNull(s);
   869 
   808 
   870         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   809         if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
   871             throw new IndexOutOfBoundsException();
   810             throw new IndexOutOfBoundsException();
   879                     NumberFormatException(String.format("Illegal leading minus sign " +
   818                     NumberFormatException(String.format("Illegal leading minus sign " +
   880                                                        "on unsigned string %s.", s));
   819                                                        "on unsigned string %s.", s));
   881             } else {
   820             } else {
   882                 if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
   821                 if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
   883                         (radix == 10 && len <= 9)) { // Integer.MAX_VALUE in base 10 is 10 digits
   822                         (radix == 10 && len <= 9)) { // Integer.MAX_VALUE in base 10 is 10 digits
   884                     return parseInt(s, radix, start, start + len);
   823                     return parseInt(s, start, start + len, radix);
   885                 } else {
   824                 } else {
   886                     long ell = Long.parseLong(s, radix, start, start + len);
   825                     long ell = Long.parseLong(s, start, start + len, radix);
   887                     if ((ell & 0xffff_ffff_0000_0000L) == 0) {
   826                     if ((ell & 0xffff_ffff_0000_0000L) == 0) {
   888                         return (int) ell;
   827                         return (int) ell;
   889                     } else {
   828                     } else {
   890                         throw new
   829                         throw new
   891                             NumberFormatException(String.format("String value %s exceeds " +
   830                             NumberFormatException(String.format("String value %s exceeds " +