142 |
|
|
143 |
// When the value is MIN_VALUE, it overflows when made positive |
// When the value is MIN_VALUE, it overflows when made positive |
144 |
if (num < 0) |
if (num < 0) |
145 |
return "" + MIN_VALUE; |
{ |
146 |
|
buffer[--i] = digits[(int) (-(num + radix) % radix)]; |
147 |
|
num = -(num / radix); |
148 |
|
} |
149 |
} |
} |
150 |
|
|
151 |
do |
do |
519 |
*/ |
*/ |
520 |
private static String toUnsignedString(long num, int exp) |
private static String toUnsignedString(long num, int exp) |
521 |
{ |
{ |
522 |
// Use the Integer toString for efficiency if possible. |
// Use the Integer toUnsignedString for efficiency if possible. |
523 |
if ((int) num == num) |
// If NUM<0 then this particular optimization doesn't work |
524 |
|
// properly. |
525 |
|
if (num >= 0 && (int) num == num) |
526 |
return Integer.toUnsignedString((int) num, exp); |
return Integer.toUnsignedString((int) num, exp); |
527 |
|
|
528 |
// Use an array large enough for a binary number. |
// Use an array large enough for a binary number. |
591 |
} |
} |
592 |
if (index == len) |
if (index == len) |
593 |
throw new NumberFormatException(); |
throw new NumberFormatException(); |
594 |
|
|
595 |
|
long max = MAX_VALUE / radix; |
596 |
|
// We can't directly write `max = (MAX_VALUE + 1) / radix'. |
597 |
|
// So instead we fake it. |
598 |
|
if (isNeg && MAX_VALUE % radix == radix - 1) |
599 |
|
++max; |
600 |
|
|
601 |
long val = 0; |
long val = 0; |
602 |
while (index < len) |
while (index < len) |
603 |
{ |
{ |
604 |
|
if (val < 0 || val > max) |
605 |
|
throw new NumberFormatException(); |
606 |
|
|
607 |
ch = Character.digit(str.charAt(index++), radix); |
ch = Character.digit(str.charAt(index++), radix); |
608 |
val = val * radix + ch; |
val = val * radix + ch; |
609 |
if (ch < 0 || (val < 0 && (index < len || ! isNeg |
if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE))) |
|
|| val != MIN_VALUE))) |
|
610 |
throw new NumberFormatException(); |
throw new NumberFormatException(); |
611 |
} |
} |
612 |
return isNeg ? -val : val; |
return isNeg ? -val : val; |