from @liach

This commit is contained in:
Shaojin Wen 2026-01-23 11:38:14 +08:00
parent 90778ed95f
commit 6c770a89d6
2 changed files with 70 additions and 64 deletions

View File

@ -482,38 +482,10 @@ public final class Integer extends Number
*/
public static int parseInt(String s, int radix)
throws NumberFormatException {
int len;
byte[] value;
if (s == null || radix != 10 || (len = (value = s.value()).length) == 0 || !s.isLatin1()) {
return parseInt0(s, radix);
if (radix == 10) {
return parseInt(s);
}
/* Accumulating negatively avoids surprises near MAX_VALUE */
int fc = value[0];
int result = Integer.isDigitLatin1(fc)
? '0' - fc
: len != 1 && (fc == '-' || fc == '+')
? 0
: 1; // or any value > 0
int i = 1;
int d;
while (i + 1 < len
&& (d = DecimalDigits.digit2(value, i)) != -1
&& MIN_VALUE / 100 <= result & result <= 0) {
result = result * 100 - d; // overflow from d => result > 0
i += 2;
}
if (i < len
&& Integer.isDigitLatin1(d = value[i])
&& MIN_VALUE / 10 <= result & result <= 0) {
result = result * 10 + '0' - d; // overflow from '0' - d => result > 0
i += 1;
}
if (i == len
& result <= 0
& (MIN_VALUE < result || fc == '-')) {
return fc == '-' ? result : -result;
}
throw NumberFormatException.forInputString(s);
return parseInt0(s, radix);
}
private static int parseInt0(String s, int radix) {
@ -625,7 +597,38 @@ public final class Integer extends Number
* parsable integer.
*/
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s, 10);
int len;
byte[] value;
if (s == null || (len = (value = s.value()).length) == 0 || !s.isLatin1()) {
return parseInt0(s, 10);
}
/* Accumulating negatively avoids surprises near MAX_VALUE */
int fc = value[0];
int result = Integer.isDigitLatin1(fc)
? '0' - fc
: len != 1 && (fc == '-' || fc == '+')
? 0
: 1; // or any value > 0
int i = 1;
int d;
while (i + 1 < len
&& (d = DecimalDigits.digit2(value, i)) != -1
&& MIN_VALUE / 100 <= result & result <= 0) {
result = result * 100 - d; // overflow from d => result > 0
i += 2;
}
if (i < len
&& Integer.isDigitLatin1(d = value[i])
&& MIN_VALUE / 10 <= result & result <= 0) {
result = result * 10 + '0' - d; // overflow from '0' - d => result > 0
i += 1;
}
if (i == len
& result <= 0
& (MIN_VALUE < result || fc == '-')) {
return fc == '-' ? result : -result;
}
throw NumberFormatException.forInputString(s);
}
/**

View File

@ -515,38 +515,10 @@ public final class Long extends Number
*/
public static long parseLong(String s, int radix)
throws NumberFormatException {
int len;
byte[] value;
if (s == null || radix != 10 || (len = (value = s.value()).length) == 0 || !s.isLatin1()) {
return parseLong0(s, radix);
if (radix == 10) {
return parseLong(s);
}
/* Accumulating negatively avoids surprises near MAX_VALUE */
int fc = value[0];
long result = Integer.isDigitLatin1(fc)
? '0' - fc
: len != 1 && (fc == '-' || fc == '+')
? 0
: 1; // or any value > 0
int i = 1;
int d;
while (i + 1 < len
&& (d = DecimalDigits.digit2(value, i)) != -1
&& MIN_VALUE / 100 <= result & result <= 0) {
result = result * 100 - d; // overflow from d => result > 0
i += 2;
}
if (i < len
&& Integer.isDigitLatin1(d = value[i])
&& MIN_VALUE / 10 <= result & result <= 0) {
result = result * 10 + '0' - d; // overflow from '0' - d => result > 0
i += 1;
}
if (i == len
& result <= 0
& (MIN_VALUE < result || fc == '-')) {
return fc == '-' ? result : -result;
}
throw NumberFormatException.forInputString(s);
return parseLong0(s, radix);
}
private static long parseLong0(String s, int radix) {
@ -661,7 +633,38 @@ public final class Long extends Number
* parsable {@code long}.
*/
public static long parseLong(String s) throws NumberFormatException {
return parseLong(s, 10);
int len;
byte[] value;
if (s == null || (len = (value = s.value()).length) == 0 || !s.isLatin1()) {
return parseLong0(s, 10);
}
/* Accumulating negatively avoids surprises near MAX_VALUE */
int fc = value[0];
long result = Integer.isDigitLatin1(fc)
? '0' - fc
: len != 1 && (fc == '-' || fc == '+')
? 0
: 1; // or any value > 0
int i = 1;
int d;
while (i + 1 < len
&& (d = DecimalDigits.digit2(value, i)) != -1
&& MIN_VALUE / 100 <= result & result <= 0) {
result = result * 100 - d; // overflow from d => result > 0
i += 2;
}
if (i < len
&& Integer.isDigitLatin1(d = value[i])
&& MIN_VALUE / 10 <= result & result <= 0) {
result = result * 10 + '0' - d; // overflow from '0' - d => result > 0
i += 1;
}
if (i == len
& result <= 0
& (MIN_VALUE < result || fc == '-')) {
return fc == '-' ? result : -result;
}
throw NumberFormatException.forInputString(s);
}
/**