-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.pde
100 lines (87 loc) · 3.41 KB
/
utils.pde
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
// =============================================================================
// Misc. common utility methods
// =============================================================================
import java.util.regex.*;
// Math ========================================================================
// Mod function that handles negatives
// https://dev.to/maurobringolf/a-neat-trick-to-compute-modulo-of-negative-numbers-111e
/**
* Mod function that handles negative numbers. Equivalent to x % n, but
* negative numbers wrap
* Based on:
* https://dev.to/maurobringolf/a-neat-trick-to-compute-modulo-of-negative-numbers-111e
*/
int mod(int x, int n) {
return (x % n + n) % n;
}
// Strings =====================================================================
/**
* Truncate a string if it's longer than the specified length
* @param s String to truncate
* @param maxLength Max length string should have before getting truncated
* @param ellipsis If true, append "..." to the end. 3 extra characters will be
* removed to ensure the output is the desired max length
* @param hash If true, append the string's hash code to the end. Additional
* characters will be removed to ensure the output is the desired max length
* @return Truncated string
*/
String truncateString(String s, int maxLength, boolean ellipsis, boolean hash) {
if (s.length() > maxLength) {
String suffix = "";
if (ellipsis)
suffix += "...";
if (hash)
suffix += s.hashCode();
s = s.substring(0, maxLength - suffix.length()) + suffix;
}
return s;
}
/**
* Truncate a string if it's longer than the specified length. If truncated,
* "..." and the string's hash code will be appended to the end. Extra
* characters will be removed to ensure the output is the desired max length
* @param s String to truncate
* @param maxLength Max length string should have before getting truncated
* @return Truncated string
*/
String truncateString(String s, int maxLength) {
return truncateString(s, maxLength, true, true);
}
// Input Helpers ===============================================================
// Numeric Inputs --------------------------------------------------------------
/**
* Clear non-numeric values from text input and return the int representation
* @param input The GTextField object
* @return int representation of sanitized text (or -1 if empty string after
* sanitizing)
*/
int sanitizeIntegerInputValue(GTextField input) {
String sanitized = input.getText().trim().replaceAll("\\D", "");
// Return -1 if empty string
if (sanitized.equals(""))
return -1;
int parsed = Integer.parseInt(sanitized);
return parsed;
}
/**
* Clear non-numeric/decimal values from text input and return the float
* representation
* @param input The GTextField object
* @return float representation of sanitized text (or -1.0 if empty string
* after sanitizing)
*/
float sanitizeFloatInputValue(GTextField input) {
// Replace all but digits and decimals
String sanitized = input.getText().trim().replaceAll("[^\\d.]", "");
// Strip any multiple points from string
Pattern p = Pattern.compile("\\d*\\.?\\d*");
Matcher m = p.matcher(sanitized);
// If there's a match, add leading 0 to fix parsing errors with edge cases
// (e.g. just a ".")
sanitized = m.find() ? "0" + m.group() : "";
// Return -1 if empty string
if (sanitized.equals(""))
return -1.0;
float parsed = Float.parseFloat(sanitized);
return parsed;
}