-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathTextUtils.java
More file actions
438 lines (398 loc) · 16.7 KB
/
TextUtils.java
File metadata and controls
438 lines (398 loc) · 16.7 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package codes.biscuit.skyblockaddons.utils;
import codes.biscuit.skyblockaddons.SkyblockAddons;
import com.google.gson.JsonObject;
import java.nio.charset.StandardCharsets;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Collection of text/string related utility methods
*/
public class TextUtils {
/**
* Hypixel uses US number format.
*/
public static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);
private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)§[0-9A-FK-ORZ]");
private static final Pattern STRIP_ICONS_PATTERN = Pattern.compile("[♲Ⓑ⚒ቾ]+");
private static final Pattern REPEATED_COLOR_PATTERN = Pattern.compile("(?i)(§[0-9A-FK-ORZ])+");
private static final Pattern NUMBERS_SLASHES = Pattern.compile("[^0-9 /]");
private static final Pattern SCOREBOARD_CHARACTERS = Pattern.compile("[^a-z A-Z:0-9_/'.!§\\[\\]❤]");
private static final Pattern FLOAT_CHARACTERS = Pattern.compile("[^.0-9\\-]");
private static final Pattern INTEGER_CHARACTERS = Pattern.compile("[^0-9]");
private static final Pattern TRIM_WHITESPACE_RESETS = Pattern.compile("^(?:\\s|§r)*|(?:\\s|§r)*$");
private static final Pattern USERNAME_PATTERN = Pattern.compile("[A-Za-z0-9_]+");
private static final Pattern RESET_CODE_PATTERN = Pattern.compile("(?i)§R");
private static final Pattern MAGNITUDE_PATTERN = Pattern.compile("(\\d[\\d,.]*\\d*)+([kKmMbBtT])");
private static final NavigableMap<Integer, String> suffixes = new TreeMap<>();
static {
suffixes.put(1_000, "k");
suffixes.put(1_000_000, "M");
suffixes.put(1_000_000_000, "B");
NUMBER_FORMAT.setMaximumFractionDigits(2);
}
/**
* Formats a double number to look better with commas every 3 digits and up to two decimal places.
* For example: {@code 1,006,789.5}
*
* @param number Number to format
* @return Formatted string
*/
public static String formatDouble(double number) {
return NUMBER_FORMAT.format(number);
}
/**
* Strips color codes from a given text
*
* @param input Text to strip colors from
* @return Text without color codes
*/
public static String stripColor(final String input) {
return STRIP_COLOR_PATTERN.matcher(input).replaceAll("");
}
/**
* Strips icons from player names
* @param input Text to strip icons from
* @return Text without icons
*/
public static String stripIcons(String input) {
return STRIP_ICONS_PATTERN.matcher(input).replaceAll("");
}
/**
* Strips icons and colors and trims spaces from a potential username
* @param input Text to strip from
* @return Stripped Text
*/
public static String stripUsername(String input) {
return trimWhitespaceAndResets(stripIcons(stripColor((input))));
}
/**
* Computationally efficient way to test if a given string has a rendered length of 0
* @param input string to test
* @return {@code true} if the input string is length 0 or only contains repeated formatting codes
*/
public static boolean isZeroLength(String input) {
return input.length() == 0 || REPEATED_COLOR_PATTERN.matcher(input).matches();
}
/**
* Removes any character that isn't a number, letter, or common symbol from a given text.
*
* @param text Input text
* @return Input text with only letters and numbers
*/
public static String keepScoreboardCharacters(String text) {
return SCOREBOARD_CHARACTERS.matcher(text).replaceAll("");
}
/**
* Removes any character that isn't a number, - or . from a given text.
*
* @param text Input text
* @return Input text with only valid float number characters
*/
public static String keepFloatCharactersOnly(String text) {
return FLOAT_CHARACTERS.matcher(text).replaceAll("");
}
/**
* Removes any character that isn't a number from a given text.
*
* @param text Input text
* @return Input text with only valid integer number characters
*/
public static String keepIntegerCharactersOnly(String text) {
return INTEGER_CHARACTERS.matcher(text).replaceAll("");
}
/**
* Removes any character that isn't a number from a given text.
*
* @param text Input text
* @return Input text with only numbers
*/
public static String getNumbersOnly(String text) {
return NUMBERS_SLASHES.matcher(text).replaceAll("");
}
/**
* Converts all numbers with magnitudes in a given string, e.g. "10k" -> "10000" and "10M" -> "10000000." Magnitudes
* are not case-sensitive.
*
* <b>Supported magnitudes:</b>
* <p>k - thousand</p>
* <p>m - million</p>
* <p>b - billion</p>
* <p>t - trillion</p>
* <p>
* <p>
* <b>Examples:</b>
* <p>1k -> 1,000</p>
* <p>2.5K -> 2,500</p>
* <p>100M -> 100,000,000</p>
*
* @param text - Input text
* @return Input text with converted magnitudes
*/
public static String convertMagnitudes(String text) throws ParseException {
Matcher matcher = MAGNITUDE_PATTERN.matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
double parsedDouble = NUMBER_FORMAT.parse(matcher.group(1)).doubleValue();
String magnitude = matcher.group(2).toLowerCase(Locale.ROOT);
switch (magnitude) {
case "k":
parsedDouble *= 1_000;
break;
case "m":
parsedDouble *= 1_000_000;
break;
case "b":
parsedDouble *= 1_000_000_000;
break;
case "t":
parsedDouble *= 1_000_000_000_000L;
}
matcher.appendReplacement(sb, NUMBER_FORMAT.format(parsedDouble));
}
matcher.appendTail(sb);
return sb.toString();
}
/**
* Removes any duplicate spaces from a given text.
*
* @param text Input text
* @return Input text without repeating spaces
*/
public static String removeDuplicateSpaces(String text) {
return text.replaceAll("\\s+", " ");
}
/**
* Reverses a given text while leaving the english parts intact and in order.
* (Maybe its more complicated than it has to be, but it gets the job done.)
*
* @param originalText Input text
* @return Reversed input text
*/
public static String reverseText(String originalText) {
StringBuilder newString = new StringBuilder();
String[] parts = originalText.split(" ");
for (int i = parts.length; i > 0; i--) {
String textPart = parts[i-1];
boolean foundCharacter = false;
for (char letter : textPart.toCharArray()) {
if (letter > 191) { // Found special character
foundCharacter = true;
newString.append(new StringBuilder(textPart).reverse());
break;
}
}
newString.append(" ");
if (!foundCharacter) {
newString.insert(0, textPart);
}
newString.insert(0, " ");
}
return removeDuplicateSpaces(newString.toString().trim());
}
/**
* Get the ordinal suffix of a number, meaning
* <ul>
* <li>st - if n ends with 1 but isn't 11</li>
* <li>nd - if n ends with 2 but isn't 12</li>
* <li>rd - if n ends with 3 but isn't 13</li>
* <li>th - in all other cases</li>
* </ul>
*/
public static String getOrdinalSuffix(final int n) {
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
/**
* @param textureURL The texture ID/hash that is in the texture URL (not including http://textures.minecraft.net/texture/)
* @return A json string including the texture URL as a skin texture (used in NBT)
*/
public static String encodeSkinTextureURL(String textureURL) {
JsonObject skin = new JsonObject();
skin.addProperty("url", "http://textures.minecraft.net/texture/" + textureURL);
JsonObject textures = new JsonObject();
textures.add("SKIN", skin);
JsonObject root = new JsonObject();
root.add("textures", textures);
return Base64.getEncoder().encodeToString(SkyblockAddons.getGson().toJson(root).getBytes(StandardCharsets.UTF_8));
}
public static String abbreviate(int number) {
if (number < 0) {
return "-" + abbreviate(-number);
}
if (number < 1000) {
return Long.toString(number);
}
Map.Entry<Integer, String> entry = suffixes.floorEntry(number);
Integer divideBy = entry.getKey();
String suffix = entry.getValue();
int truncated = number / (divideBy / 10); //the number part of the output times 10
//noinspection IntegerDivisionInFloatingPointContext
boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10);
return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix;
}
/**
* Removes all leading or trailing reset color codes and whitespace from a string.
*
* @param input Text to trim
* @return Text without leading or trailing reset color codes and whitespace
*/
public static String trimWhitespaceAndResets(String input) {
return TRIM_WHITESPACE_RESETS.matcher(input).replaceAll("");
}
/**
* Checks if text matches a Minecraft username
*
* @param input Text to check
* @return Whether this input can be Minecraft username or not
*/
public static boolean isUsername(String input) {
return USERNAME_PATTERN.matcher(input).matches();
}
/**
* Removes all reset color codes from a given text
*
* @param input Text to strip
* @return Text with all reset color codes removed
*/
public static String stripResets(String input) {
return RESET_CODE_PATTERN.matcher(input).replaceAll("");
}
/**
* Converts a string into proper case (Source: <a href="https://dev-notes.com">Dev Notes</a>)
* @param inputString a string
* @return a new string in which the first letter of each word is capitalized
*/
public static String toProperCase(String inputString) {
String ret;
StringBuffer sb = new StringBuffer();
Matcher match = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(inputString);
while (match.find()) {
match.appendReplacement(sb, match.group(1).toUpperCase() + match.group(2).toLowerCase());
}
ret = match.appendTail(sb).toString();
return ret;
}
/**
* Calculates and returns the first formatted substring that matches the unformatted string
* <p>
* Used for color/style compatibility mode.
*
* @param unformattedSubstring the uncolored/unstyled substring of which we request a match
* @param formatted the colored string, from which we request a substring
* @return {@code null} if {@param unformattedSubstring} is not found in {@param formatted}, or the colored/styled substring.
*/
public static String getFormattedString(String formatted, String unformattedSubstring) {
if (unformattedSubstring.length() == 0) {
return "";
}
String styles = "kKlLmMnNoO";
StringBuilder preEnchantFormat = new StringBuilder();
StringBuilder formattedEnchant = new StringBuilder();
int i = -2;
int len = formatted.length();
int unformattedEnchantIdx = 0;
int k = 0;
while (true) {
i = formatted.indexOf('§', i + 2);
// No more formatting codes were found in the string
if (i == -1) {
// Test if there is an instance of the formatted enchant in the rest of the string
for (; k < len; k++) {
// Enchant string matches at position k
if (formatted.charAt(k) == unformattedSubstring.charAt(unformattedEnchantIdx)) {
formattedEnchant.append(formatted.charAt(k));
unformattedEnchantIdx++;
// We have matched the entire enchant. Return the current format + the formatted enchant
if (unformattedEnchantIdx == unformattedSubstring.length()) {
return preEnchantFormat.append(formattedEnchant).toString();
}
}
// Enchant string doesn't match at position k
else {
unformattedEnchantIdx = 0;
// Transfer formats from formatted enchant to format
preEnchantFormat = new StringBuilder(mergeFormats(preEnchantFormat.toString(), formattedEnchant.toString()));
formattedEnchant = new StringBuilder();
}
}
// No matching enchant found
return null;
} else {
for (; k < i; k++) {
if (formatted.charAt(k) == unformattedSubstring.charAt(unformattedEnchantIdx)) {
formattedEnchant.append(formatted.charAt(k));
unformattedEnchantIdx++;
// We have matched the entire enchant. Return the current format + the formatted enchant
if (unformattedEnchantIdx == unformattedSubstring.length()) {
return preEnchantFormat.append(formattedEnchant).toString();
}
} else {
unformattedEnchantIdx = 0;
// Transfer formats from formatted enchant to format
preEnchantFormat = new StringBuilder(mergeFormats(preEnchantFormat.toString(), formattedEnchant.toString()));
formattedEnchant = new StringBuilder();
}
}
// Add the format code if present
if (i + 1 < len) {
char formatChar = formatted.charAt(i + 1);
// If not parsing an enchant, alter the pre enchant format
if (unformattedEnchantIdx == 0) {
// Restart format at a new color
if (styles.indexOf(formatChar) == -1) {
preEnchantFormat = new StringBuilder();
}
// Append the new format code to the formatter
preEnchantFormat.append("§").append(formatChar);
}
// If parsing an enchant, alter the current enchant format and the formatted enchant
else {
// Restart format at a new color
formattedEnchant.append("§").append(formatChar);
}
// Skip the formatting code "§[0-9a-zA-Z]" on the next round
k = i + 2;
}
}
}
}
/**
* Calculate the color/style formatting after first and second format strings
* <p>
* Used for: Given the color/style formatting before an enchantment. as well as the enchantment itself,
* Calculate the color/style formatting after the enchantment
*
* @param firstFormat the color/style formatting before the string
* @param secondFormat the string that may have formatting codes within it
* @return the relevant formatting codes in effect after {@param secondFormat}
*/
private static String mergeFormats(String firstFormat, String secondFormat) {
if (secondFormat == null || secondFormat.length() == 0) {
return firstFormat;
}
String styles = "kKlLmMnNoO";
StringBuilder builder = new StringBuilder(firstFormat);
int i = -2;
while ((i = secondFormat.indexOf('§', i + 2)) != -1) {
if (i + 1 < secondFormat.length()) {
char c = secondFormat.charAt(i + 1);
// If it's not a style then it's a color code
if (styles.indexOf(c) == -1) {
builder = new StringBuilder();
}
builder.append("§").append(c);
}
}
return builder.toString();
}
}