-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathSmallDigits.java
More file actions
61 lines (51 loc) · 1.86 KB
/
Copy pathSmallDigits.java
File metadata and controls
61 lines (51 loc) · 1.86 KB
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
59
60
61
package gregtech.api.util;
import org.jetbrains.annotations.NotNull;
public final class SmallDigits {
@SuppressWarnings("UnnecessaryUnicodeEscape")
private static final int SMALL_DOWN_NUMBER_BASE = '\u2080';
@SuppressWarnings("UnnecessaryUnicodeEscape")
private static final int SMALL_UP_NUMBER_BASE = '\u2080';
private static final int NUMBER_BASE = '0';
@SuppressWarnings("UnnecessaryUnicodeEscape")
private static final int FORMATTING_SYMBOL = '\u00a7';
private SmallDigits() {}
public static @NotNull String toSmallUpNumbers(String string) {
return convert(string, SMALL_UP_NUMBER_BASE);
}
public static @NotNull String toSmallDownNumbers(String string) {
return convert(string, SMALL_DOWN_NUMBER_BASE);
}
private static @NotNull String convert(@NotNull String string, int base) {
char[] chars = string.toCharArray();
boolean hasPrefix = false;
boolean hasFormat = false;
for (int i = 0; i < chars.length; i++) {
if (hasFormat) {
// skip over the next character once the format flag is set
hasFormat = false;
continue;
}
char c = chars[i];
if (c == '-') {
hasPrefix = true;
continue;
}
if (c == FORMATTING_SYMBOL) {
hasFormat = true;
continue;
}
if (Character.isDigit(c)) {
if (!hasPrefix) {
chars[i] = convertToBase(c, base);
}
} else {
// reset prefix as soon as a non-digit is encountered
hasPrefix = false;
}
}
return new String(chars);
}
private static char convertToBase(char c, int base) {
return (char) (base + (c - NUMBER_BASE));
}
}