-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModStrings.java
58 lines (49 loc) · 1.48 KB
/
ModStrings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package mmc.utils.strings;
import org.jetbrains.annotations.*;
public class ModStrings{
public static boolean canParseLong(String s){
return parseLong(s)!=null;
}
@Nullable
public static Long parseLong(String s){
return parseLong(s, 10);
}
@Nullable
public static Long parseLong(String s, int radix){
return parseLong(s, radix, 0, s.length());
}
@Nullable
public static Long parseLong(String s, int radix, int start, int end){
boolean negative = false;
int i = start, len = end - start;
long limit = -9223372036854775807L;
if(len <= 0){
return null;
}else{
char firstChar = s.charAt(i);
if(firstChar < '0'){
if(firstChar == '-'){
negative = true;
limit = -9223372036854775808L;
}else if(firstChar != '+'){
return null;
}
if(len == 1) return null;
++i;
}
long result;
int digit;
for(result = 0L; i < end; result -= digit){
digit = Character.digit(s.charAt(i++), radix);
if(digit < 0){
return null;
}
result *= radix;
if(result < limit + (long)digit){
return null;
}
}
return negative ? result : -result;
}
}
}