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
@ -99,18 +99,29 @@ public final class Long extends Number {
|
||||
}
|
||||
|
||||
public static long parseLong(String s, int radix) {
|
||||
int i = 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);
|
||||
if (((c >= '0') && (c <= '9')) ||
|
||||
((c >= 'a') && (c <= 'z'))) {
|
||||
long digit = ((c >= '0' && c <= '9') ? (c - '0') : (c - 'a' + 10));
|
||||
if (digit < radix) {
|
||||
number += digit * pow(radix, (s.length() - i - 1));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
throw new NumberFormatException("invalid character " + c + " code " +
|
||||
(int) c);
|
||||
}
|
||||
|
||||
if (negative) {
|
||||
number = -number;
|
||||
}
|
||||
|
||||
return number;
|
||||
|
Loading…
x
Reference in New Issue
Block a user