forked from mergebase/log4j-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrings.java
285 lines (256 loc) · 8.68 KB
/
Strings.java
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
package com.mergebase.log4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class Strings {
public final static String WHITESPACE_SEPARATOR = "[ \\t\\n\\x0B\\f\\r]+";
public final static String WHITESPACE_COMMA_SEPARATOR = "[, \\t\\n\\x0B\\f\\r]+";
public final static String WHITESPACE_COMMA_SEMICOLON_SEPARATOR = "[ \\t\\n\\x0B\\f\\r,;\\|]+";
public final static String WHITESPACE_COMMA_SEPARATOR_BUT_NOT_SPACES = "[\\t\\n\\x0B\\f\\r,\\|]+";
public static String nullSafeTrim(String s) {
return s != null ? s.trim() : null;
}
/**
* Given a line like "BLAH BLAH (column, foo, bar) BLAH BLAH"
* returns a map: {"column" --> 0, "foo" --> 1, "bar" --> 2}
*/
public static Map<String, Integer> positions(String headerLine) {
HashMap<String, Integer> m = new HashMap<String, Integer>();
int j = headerLine.indexOf('(');
int k = headerLine.indexOf(')');
if (j >= 0 && j < k) {
headerLine = headerLine.substring(j + 1, k);
String[] toks = headerLine.split(Strings.WHITESPACE_COMMA_SEPARATOR);
for (int i = 0; i < toks.length; i++) {
m.put(toks[i], i);
}
} else {
throw new RuntimeException("cannot parse headerLine for column positions: [" + headerLine + "]");
}
return m;
}
public static String nullSafeTrim(Object o) {
if (o == null) {
return "";
} else if (o instanceof String) {
return ((String) o).trim();
} else {
return o.toString().trim();
}
}
/**
* Default Java case-sensitive goes A-Za-z, but I prefer AaBbCc.
*/
public final static Comparator<String> CASE_SENSITIVE_SANE = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
if (s1 == s2) {
return 0;
} else if (s1 == null) {
return -1;
} else if (s2 == null) {
return 1;
}
int c = s1.compareToIgnoreCase(s2);
if (c == 0) {
c = s1.compareTo(s2);
}
return c;
}
};
public static Long parseLong(String s, long defaultVal) {
s = s != null ? s.trim() : "";
if ("".equals(s)) {
return defaultVal;
}
try {
return Long.parseLong(s);
} catch (RuntimeException e) {
return defaultVal;
}
}
public static int countChar(String s, char c) {
int count = 0;
for (char ch : s.toCharArray()) {
if (ch == c) {
count++;
}
}
return count;
}
public static int countLeadingChars(String s) {
int count = 0;
if (s.length() > 0) {
char c = s.charAt(0);
count = 1;
for (int i = 1; i < s.length(); i++) {
if (c == s.charAt(i)) {
count++;
} else {
break;
}
}
}
return count;
}
/**
* Strips leading characters from supplied string.
* Also adds leading characters if necessary to create a string
* that is at least minLen long.
*
* @param s String to strip leading characters from.
* @param minLen minimum length of String to return (padded with leading characters if necessary)
* @return String with leading characters removed.
*/
public static String stripLeadingCharacter(String s, char c, int minLen) {
if (s == null) {
return null;
}
StringBuilder result = null;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != c) {
result = new StringBuilder(s.substring(i));
break;
}
}
if (result == null) {
result = new StringBuilder();
}
for (int i = result.length(); i < minLen; i++) {
result.insert(0, c);
}
return result.toString();
}
public static String stripLeadingZeroes(long l, int minLen) {
return stripLeadingCharacter(Long.toString(l), '0', minLen);
}
/**
* Strips leading zeroes from supplied string.
* Also adds leading zeroes if necessary to create a string
* that is at least minLen long.
*
* @param s String to strip leading zeroes from.
* @param minLen minimum length of String to return (padded with leading zeroes if necessary)
* @return String with leading zeroes removed.
*/
public static String stripLeadingZeroes(String s, int minLen) {
return stripLeadingCharacter(s, '0', minLen);
}
public static String stripTrailingNonAlphaNumerics(String s) {
if (s == null) {
return null;
} else {
StringBuilder buf = new StringBuilder(s);
for (int i = s.length() - 1; i >= 0; i--) {
char c = s.charAt(i);
if (isLetterOrDigit(c)) {
return buf.toString();
} else {
buf.deleteCharAt(i);
}
}
}
return "";
}
public static boolean isLetterOrDigit(char c) {
return isLetter(c) || isDigit(c);
}
public static boolean isDigit(char c) {
return '0' <= c && c <= '9';
}
public static boolean isLetter(char c) {
return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
}
public static boolean containsOnlyDigits(String s) {
if (s == null || "".equals(s)) {
return false;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
boolean isValid = (i == 0 && c == '-') || isDigit(c);
if (!isValid) {
return false;
}
}
return true;
}
public static boolean containsOnlyLetters(String s) {
if (s == null || "".equals(s)) {
return false;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!isLetter(c)) {
return false;
}
}
return true;
}
public static List<String> intoLines(final String... strings) {
List<String> list = new ArrayList<String>();
if (strings != null) {
for (final String s : strings) {
if (s != null) {
StringReader sr = new StringReader(s);
BufferedReader br = new BufferedReader(sr);
String line;
try {
while ((line = br.readLine()) != null) {
list.add(line);
}
} catch (IOException ioe) {
throw new RuntimeException("impossible - StringReader does not throw IOException - " + ioe, ioe);
}
}
}
}
return list;
}
public static String cleanForSql(String s) {
return cleanForSql(s, false);
}
public static String cleanForSql(String s, boolean doubleUpSingleQuotes) {
s = s != null ? s.trim() : "";
s = s.replace("\n", " ");
s = s.replace("\r", " ");
s = s.replace("\t", " ");
if (doubleUpSingleQuotes) {
return s.replace("'", "''");
} else {
return s.replace("'", "");
}
}
public static boolean isYes(String s) {
s = s != null ? s.trim().toLowerCase(Locale.ENGLISH) : "";
return "1".equals(s) || "y".equals(s) || "t".equals(s) || "yes".equals(s) || "true".equals(s);
}
public static boolean isNo(String s) {
s = s != null ? s.trim().toLowerCase(Locale.ENGLISH) : "";
return "0".equals(s) || "n".equals(s) || "f".equals(s) || "no".equals(s) || "false".equals(s);
}
public static String safeForHTML(String s) {
if (s == null) {
return null;
}
s = s.replace("&", "&");
s = s.replace("\"", """);
s = s.replace("<", "<");
s = s.replace(">", ">");
return s;
}
public static boolean startsWithYearDot(String s) {
if (s != null && s.length() > 4 && (s.startsWith("20") || s.startsWith("19"))) {
s = s.substring(2);
if (s.charAt(2) == '.') {
return Strings.isDigit(s.charAt(0)) && Strings.isDigit(s.charAt(1));
}
}
return false;
}
}