src/java.base/share/classes/java/lang/Long.java
changeset 51412 8f7e3f9ddbc0
parent 50211 5afedc9e4662
child 51509 758b3f3f3a8d
equal deleted inserted replaced
51411:4699147a4f91 51412:8f7e3f9ddbc0
   673             if (firstChar < '0') { // Possible leading "+" or "-"
   673             if (firstChar < '0') { // Possible leading "+" or "-"
   674                 if (firstChar == '-') {
   674                 if (firstChar == '-') {
   675                     negative = true;
   675                     negative = true;
   676                     limit = Long.MIN_VALUE;
   676                     limit = Long.MIN_VALUE;
   677                 } else if (firstChar != '+') {
   677                 } else if (firstChar != '+') {
   678                     throw NumberFormatException.forInputString(s);
   678                     throw NumberFormatException.forInputString(s, radix);
   679                 }
   679                 }
   680 
   680 
   681                 if (len == 1) { // Cannot have lone "+" or "-"
   681                 if (len == 1) { // Cannot have lone "+" or "-"
   682                     throw NumberFormatException.forInputString(s);
   682                     throw NumberFormatException.forInputString(s, radix);
   683                 }
   683                 }
   684                 i++;
   684                 i++;
   685             }
   685             }
   686             long multmin = limit / radix;
   686             long multmin = limit / radix;
   687             long result = 0;
   687             long result = 0;
   688             while (i < len) {
   688             while (i < len) {
   689                 // Accumulating negatively avoids surprises near MAX_VALUE
   689                 // Accumulating negatively avoids surprises near MAX_VALUE
   690                 int digit = Character.digit(s.charAt(i++),radix);
   690                 int digit = Character.digit(s.charAt(i++),radix);
   691                 if (digit < 0 || result < multmin) {
   691                 if (digit < 0 || result < multmin) {
   692                     throw NumberFormatException.forInputString(s);
   692                     throw NumberFormatException.forInputString(s, radix);
   693                 }
   693                 }
   694                 result *= radix;
   694                 result *= radix;
   695                 if (result < limit + digit) {
   695                 if (result < limit + digit) {
   696                     throw NumberFormatException.forInputString(s);
   696                     throw NumberFormatException.forInputString(s, radix);
   697                 }
   697                 }
   698                 result -= digit;
   698                 result -= digit;
   699             }
   699             }
   700             return negative ? result : -result;
   700             return negative ? result : -result;
   701         } else {
   701         } else {
   702             throw NumberFormatException.forInputString(s);
   702             throw NumberFormatException.forInputString(s, radix);
   703         }
   703         }
   704     }
   704     }
   705 
   705 
   706     /**
   706     /**
   707      * Parses the {@link CharSequence} argument as a signed {@code long} in
   707      * Parses the {@link CharSequence} argument as a signed {@code long} in
   943                                                                   "range of unsigned long.", s));
   943                                                                   "range of unsigned long.", s));
   944                 }
   944                 }
   945                 return result;
   945                 return result;
   946             }
   946             }
   947         } else {
   947         } else {
   948             throw NumberFormatException.forInputString(s);
   948             throw NumberFormatException.forInputString(s, radix);
   949         }
   949         }
   950     }
   950     }
   951 
   951 
   952     /**
   952     /**
   953      * Parses the {@link CharSequence} argument as an unsigned {@code long} in
   953      * Parses the {@link CharSequence} argument as an unsigned {@code long} in
  1061                             "range of unsigned long.", s.subSequence(start, start + len)));
  1061                             "range of unsigned long.", s.subSequence(start, start + len)));
  1062                 }
  1062                 }
  1063                 return result;
  1063                 return result;
  1064             }
  1064             }
  1065         } else {
  1065         } else {
  1066             throw NumberFormatException.forInputString("");
  1066             throw NumberFormatException.forInputString("", radix);
  1067         }
  1067         }
  1068     }
  1068     }
  1069 
  1069 
  1070     /**
  1070     /**
  1071      * Parses the string argument as an unsigned decimal {@code long}. The
  1071      * Parses the string argument as an unsigned decimal {@code long}. The