diff options
| author | CraftyOldMiner <85420839+CraftyOldMiner@users.noreply.github.com> | 2022-04-30 09:17:14 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-04-30 16:17:14 +0200 |
| commit | 0418460b5ff12af5bc0b4078ec43210489d6ae99 (patch) | |
| tree | 1b7bee840dd6af5eba99df817c37c1ba39dbfa62 /src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java | |
| parent | 79bc28639e966415a97b1f21dd11617c6a4e0215 (diff) | |
| download | notenoughupdates-0418460b5ff12af5bc0b4078ec43210489d6ae99.tar.gz notenoughupdates-0418460b5ff12af5bc0b4078ec43210489d6ae99.tar.bz2 notenoughupdates-0418460b5ff12af5bc0b4078ec43210489d6ae99.zip | |
Crash & perf fixes (#121)
- Fix crash in profile viewer when name not found
- Parse numbers using exception-free methods in hot code paths
- Update price graph to handle items transitioning from the bz to ah
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java index 5fac9208..7b714cd0 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Utils.java @@ -653,6 +653,41 @@ public class Utils { return (float) Math.round(value * scale) / scale; } + // Parses Roman numerals, allowing for single character irregular subtractive notation (e.g. IL is 49, IIL is invalid) + public static int parseRomanNumeral(String input) { + int prevVal = 0; + int total = 0; + for (int i = input.length()-1; i >= 0; i--) { + int val; + char ch = input.charAt(i); + switch (ch) { + case 'I' : val = 1; break; + case 'V' : val = 5; break; + case 'X' : val = 10; break; + case 'L' : val = 50; break; + case 'C' : val = 100; break; + case 'D' : val = 500; break; + case 'M' : val = 1000; break; + default: throw new IllegalArgumentException("Invalid Roman Numeral Character: " + ch); + } + if (val < prevVal) val = -val; + total += val; + prevVal = val; + } + + return total; + } + + public static int parseIntOrRomanNumeral(String input) { + // 0 through 9, '-', and '+' come before 'A' in ANSI, UTF8, and UTF16 character sets + // + if (input.charAt(0) < 'A') { + return Integer.parseInt(input); + } + + return parseRomanNumeral(input); + } + public static void playPressSound() { playSound(new ResourceLocation("gui.button.press"), true); } |
