RuneHive-Tarnish
Neural OSRS Enhancement Framework
Loading...
Searching...
No Matches
StringUtils.java
1package com.osroyale.util;
2
3import com.osroyale.fs.cache.FileSystem;
4
5import java.text.NumberFormat;
6import java.util.stream.IntStream;
7
42
43public class StringUtils {
44
46 private static final char VALID_USERNAME_CHARACTERS[] = {
47 '_',
48 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
49 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
50 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
51 };
52
53 public static String formatPrice(long amount) {
54 if (amount >= 1_000 && amount < 1_000_000) {
55 return (amount / 1_000) + "K";
56 } else if (amount >= 1_000_000 && amount < 1_000_000_000) {
57 return (amount / 1_000_000) + "M";
58 } else if (amount >= 1_000_000_000) {
59 return (amount / 1_000_000_000) + "B";
60 }
61 return "" + amount;
62 }
63
64 public static int convertAmount(String input) {
65 try {
66 input = input.toLowerCase().replace(" ", "");
67
68 int pos = input.indexOf('k');
69
70 if (pos != -1) {
71 return Integer.parseInt(input.substring(0, pos)) * 1_000;
72 }
73
74 pos = input.indexOf('m');
75
76 if (pos != -1) {
77 return Integer.parseInt(input.substring(0, pos)) * 1_000_000;
78 }
79
80 pos = input.indexOf('b');
81
82 if (pos != -1) {
83 return Integer.parseInt(input.substring(0, pos)) * 1_000_000_000;
84 }
85
86 } catch (Exception ex) {
87
88 }
89 return 0;
90 }
91
98 private static String determineIndefiniteArticle(String thing) {
99 char first = thing.toLowerCase().charAt(0);
100 boolean vowel = first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u';
101 return vowel ? "an" : "a";
102 }
103
110 private static String determinePluralCheck(String thing) {
111 boolean needsPlural = !thing.endsWith("s") && !thing.endsWith(")");
112 return needsPlural ? "s" : "";
113 }
114
121 public static String appendPluralCheck(String thing) {
122 return thing.concat(determinePluralCheck(thing));
123 }
124
131 public static String appendIndefiniteArticle(String thing) {
132 return determineIndefiniteArticle(thing).concat(" " + thing);
133 }
134
135 public static String capitalize(String string) {
136 return Character.toUpperCase(string.charAt(0)) + string.substring(1);
137 }
138
139 public static String format(int num) {
140 return NumberFormat.getInstance().format(num);
141 }
142
143 public static String formatText(String s) {
144 for (int i = 0; i < s.length(); i++) {
145 if (i == 0) {
146 s = String.format("%s%s", Character.toUpperCase(s.charAt(0)), s.substring(1));
147 }
148 if (!Character.isLetterOrDigit(s.charAt(i))) {
149 if (i + 1 < s.length()) {
150 s = String.format("%s%s%s", s.subSequence(0, i + 1), Character.toUpperCase(s.charAt(i + 1)), s.substring(i + 2));
151 }
152 }
153 }
154 return s.replace("_", " ");
155 }
156
157 public static String getAOrAn(String nextWord) {
158 String s = "a";
159 final char c = nextWord.toUpperCase().charAt(0);
160 if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
161 s = "an";
162 }
163 return s;
164 }
165
166 public static String hashToString(long input) {
167 int length = 12;
168 char decoded[] = new char[length];
169
170 while (input != 0) {
171 long hash = input;
172 input /= 37;
173 int index = (int) (hash - input * 37);
174 decoded[length--] = VALID_USERNAME_CHARACTERS[index];
175 }
176
177 return new String(decoded, length, decoded.length - length);
178 }
179
186 public static long hash(String input) {
187 long hash = 0;
188
189 for (int index = 0; index < input.length(); index++) {
190 char key = input.charAt(index);
191
192 hash *= 37;
193
194 if (key >= 'A' && key <= 'Z') {
195 hash += (1 + key) - 65;
196 } else if (key >= 'a' && key <= 'z') {
197 hash += (1 + key) - 97;
198 } else if (key >= '0' && key <= '9') {
199 hash += (27 + key) - 48;
200 }
201 }
202
203 while (hash % 37 == 0 && hash != 0) hash /= 37;
204 return hash;
205 }
206
214 public static int hashArchive(String string) {
215 return _hash(string.toUpperCase());
216 }
217
236 @Deprecated
237 private static int _hash(String string) {
238 return IntStream.range(0, string.length()).reduce(0, (hash, index) -> hash * 61 + string.charAt(index) - 32);
239 }
240
241}
static int hashArchive(String string)
static long hash(String input)
static String appendIndefiniteArticle(String thing)
static String appendPluralCheck(String thing)