mirror of
https://github.com/corda/corda.git
synced 2025-02-27 03:27:34 +00:00
handle negative numbers in Long.parseLong() and improve error detection
This commit is contained in:
parent
dd17a27485
commit
493667a6cc
@ -98,19 +98,30 @@ public final class Long extends Number {
|
|||||||
return parseLong(s, 10);
|
return parseLong(s, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long parseLong(String s, int radix) {
|
public static long parseLong(String s, int radix) {
|
||||||
|
int i = 0;
|
||||||
long number = 0;
|
long number = 0;
|
||||||
|
boolean negative = s.startsWith("-");
|
||||||
|
if (negative) {
|
||||||
|
i = 1;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < s.length(); ++i) {
|
for (; i < s.length(); ++i) {
|
||||||
char c = s.charAt(i);
|
char c = s.charAt(i);
|
||||||
if (((c >= '0') && (c <= '9')) ||
|
if (((c >= '0') && (c <= '9')) ||
|
||||||
((c >= 'a') && (c <= 'z'))) {
|
((c >= 'a') && (c <= 'z'))) {
|
||||||
long digit = ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10));
|
long digit = ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10));
|
||||||
number += digit * pow(radix, (s.length() - i - 1));
|
if (digit < radix) {
|
||||||
} else {
|
number += digit * pow(radix, (s.length() - i - 1));
|
||||||
throw new NumberFormatException("invalid character " + c + " code " +
|
continue;
|
||||||
(int) c);
|
}
|
||||||
}
|
}
|
||||||
|
throw new NumberFormatException("invalid character " + c + " code " +
|
||||||
|
(int) c);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (negative) {
|
||||||
|
number = -number;
|
||||||
}
|
}
|
||||||
|
|
||||||
return number;
|
return number;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user