1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.util; |
22 |
|
|
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.List; |
25 |
|
import java.util.regex.Pattern; |
26 |
|
|
|
|
| 89.5% |
Uncovered Elements: 24 (228) |
Complexity: 65 |
Complexity Density: 0.47 |
|
27 |
|
public class StringUtils |
28 |
|
{ |
29 |
|
private static final Pattern DELIMITERS_PATTERN = Pattern |
30 |
|
.compile(".*='[^']*(?!')"); |
31 |
|
|
32 |
|
private static final boolean DEBUG = false; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@param |
39 |
|
|
40 |
|
@param |
41 |
|
|
42 |
|
@param |
43 |
|
|
44 |
|
@param |
45 |
|
|
46 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
47 |
21 |
public static final char[] insertCharAt(char[] in, int position,... |
48 |
|
int count, char ch) |
49 |
|
{ |
50 |
21 |
char[] tmp = new char[in.length + count]; |
51 |
|
|
52 |
21 |
if (position >= in.length) |
53 |
|
{ |
54 |
5 |
System.arraycopy(in, 0, tmp, 0, in.length); |
55 |
5 |
position = in.length; |
56 |
|
} |
57 |
|
else |
58 |
|
{ |
59 |
16 |
System.arraycopy(in, 0, tmp, 0, position); |
60 |
|
} |
61 |
|
|
62 |
21 |
int index = position; |
63 |
59 |
while (count > 0) |
64 |
|
{ |
65 |
38 |
tmp[index++] = ch; |
66 |
38 |
count--; |
67 |
|
} |
68 |
|
|
69 |
21 |
if (position < in.length) |
70 |
|
{ |
71 |
16 |
System.arraycopy(in, position, tmp, index, in.length - position); |
72 |
|
} |
73 |
|
|
74 |
21 |
return tmp; |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@param |
81 |
|
@param |
82 |
|
@param |
83 |
|
@return |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 4 |
Complexity Density: 0.36 |
|
85 |
225 |
public static final char[] deleteChars(char[] in, int from, int to)... |
86 |
|
{ |
87 |
225 |
if (from >= in.length || from < 0) |
88 |
|
{ |
89 |
1 |
return in; |
90 |
|
} |
91 |
|
|
92 |
224 |
char[] tmp; |
93 |
|
|
94 |
224 |
if (to >= in.length) |
95 |
|
{ |
96 |
107 |
tmp = new char[from]; |
97 |
107 |
System.arraycopy(in, 0, tmp, 0, from); |
98 |
107 |
to = in.length; |
99 |
|
} |
100 |
|
else |
101 |
|
{ |
102 |
117 |
tmp = new char[in.length - to + from]; |
103 |
117 |
System.arraycopy(in, 0, tmp, 0, from); |
104 |
117 |
System.arraycopy(in, to, tmp, from, in.length - to); |
105 |
|
} |
106 |
224 |
return tmp; |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
@param |
115 |
|
|
116 |
|
@return |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
118 |
6 |
public static String getLastToken(String input, String token)... |
119 |
|
{ |
120 |
6 |
if (input == null) |
121 |
|
{ |
122 |
2 |
return null; |
123 |
|
} |
124 |
4 |
if (token == null) |
125 |
|
{ |
126 |
1 |
return input; |
127 |
|
} |
128 |
3 |
String[] st = input.split(token); |
129 |
3 |
return st[st.length - 1]; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
@param |
138 |
|
@param |
139 |
|
@return |
140 |
|
|
|
|
| 64.8% |
Uncovered Elements: 19 (54) |
Complexity: 17 |
Complexity Density: 0.53 |
|
141 |
4427 |
public static String[] separatorListToArray(String input,... |
142 |
|
String delimiter) |
143 |
|
{ |
144 |
4427 |
int seplen = delimiter.length(); |
145 |
4427 |
if (input == null || input.equals("") || input.equals(delimiter)) |
146 |
|
{ |
147 |
0 |
return null; |
148 |
|
} |
149 |
4427 |
List<String> jv = new ArrayList<String>(); |
150 |
4427 |
int cp = 0, pos, escape; |
151 |
4427 |
boolean wasescaped = false, wasquoted = false; |
152 |
4427 |
String lstitem = null; |
153 |
? |
while ((pos = input.indexOf(delimiter, cp)) >= cp) |
154 |
|
{ |
155 |
12178 |
escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0; |
156 |
12178 |
if (wasescaped || wasquoted) |
157 |
|
{ |
158 |
|
|
159 |
4 |
jv.set(jv.size() - 1, lstitem = lstitem + delimiter |
160 |
|
+ input.substring(cp, pos + escape)); |
161 |
|
} |
162 |
|
else |
163 |
|
{ |
164 |
12174 |
jv.add(lstitem = input.substring(cp, pos + escape)); |
165 |
|
} |
166 |
12178 |
cp = pos + seplen; |
167 |
12178 |
wasescaped = escape == -1; |
168 |
|
|
169 |
12178 |
wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches(); |
170 |
|
} |
171 |
4427 |
if (cp < input.length()) |
172 |
|
{ |
173 |
4427 |
String c = input.substring(cp); |
174 |
4427 |
if (wasescaped || wasquoted) |
175 |
|
{ |
176 |
|
|
177 |
2 |
jv.set(jv.size() - 1, lstitem + delimiter + c); |
178 |
|
} |
179 |
|
else |
180 |
|
{ |
181 |
4425 |
if (!c.equals(delimiter)) |
182 |
|
{ |
183 |
4425 |
jv.add(c); |
184 |
|
} |
185 |
|
} |
186 |
|
} |
187 |
4427 |
if (jv.size() > 0) |
188 |
|
{ |
189 |
4427 |
String[] v = jv.toArray(new String[jv.size()]); |
190 |
4427 |
jv.clear(); |
191 |
4427 |
if (DEBUG) |
192 |
|
{ |
193 |
0 |
System.err.println("Array from '" + delimiter |
194 |
|
+ "' separated List:\n" + v.length); |
195 |
0 |
for (int i = 0; i < v.length; i++) |
196 |
|
{ |
197 |
0 |
System.err.println("item " + i + " '" + v[i] + "'"); |
198 |
|
} |
199 |
|
} |
200 |
4427 |
return v; |
201 |
|
} |
202 |
0 |
if (DEBUG) |
203 |
|
{ |
204 |
0 |
System.err.println( |
205 |
|
"Empty Array from '" + delimiter + "' separated List"); |
206 |
|
} |
207 |
0 |
return null; |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
@param |
216 |
|
@param |
217 |
|
@return |
218 |
|
|
|
|
| 80.8% |
Uncovered Elements: 5 (26) |
Complexity: 8 |
Complexity Density: 0.57 |
|
219 |
22 |
public static String arrayToSeparatorList(String[] list, String separator)... |
220 |
|
{ |
221 |
22 |
StringBuffer v = new StringBuffer(); |
222 |
22 |
if (list != null && list.length > 0) |
223 |
|
{ |
224 |
74 |
for (int i = 0, iSize = list.length; i < iSize; i++) |
225 |
|
{ |
226 |
54 |
if (list[i] != null) |
227 |
|
{ |
228 |
53 |
if (v.length() > 0) |
229 |
|
{ |
230 |
33 |
v.append(separator); |
231 |
|
} |
232 |
|
|
233 |
53 |
v.append(list[i]); |
234 |
|
} |
235 |
|
} |
236 |
20 |
if (DEBUG) |
237 |
|
{ |
238 |
0 |
System.err |
239 |
|
.println("Returning '" + separator + "' separated List:\n"); |
240 |
0 |
System.err.println(v); |
241 |
|
} |
242 |
20 |
return v.toString(); |
243 |
|
} |
244 |
2 |
if (DEBUG) |
245 |
|
{ |
246 |
0 |
System.err.println( |
247 |
|
"Returning empty '" + separator + "' separated List\n"); |
248 |
|
} |
249 |
2 |
return "" + separator; |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
@param |
258 |
|
@param |
259 |
|
@return |
260 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
261 |
56 |
public static String listToDelimitedString(List<String> terms,... |
262 |
|
String delim) |
263 |
|
{ |
264 |
56 |
StringBuilder sb = new StringBuilder(32); |
265 |
56 |
if (terms != null && !terms.isEmpty()) |
266 |
|
{ |
267 |
54 |
boolean appended = false; |
268 |
54 |
for (String term : terms) |
269 |
|
{ |
270 |
70 |
if (appended) |
271 |
|
{ |
272 |
16 |
sb.append(delim); |
273 |
|
} |
274 |
70 |
appended = true; |
275 |
70 |
sb.append(term); |
276 |
|
} |
277 |
|
} |
278 |
56 |
return sb.toString(); |
279 |
|
} |
280 |
|
|
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
@param |
286 |
|
@return |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 4 |
Complexity Density: 0.8 |
|
288 |
8 |
public static int parseInt(String s)... |
289 |
|
{ |
290 |
8 |
int result = 0; |
291 |
8 |
if (s != null && s.length() > 0) |
292 |
|
{ |
293 |
6 |
try |
294 |
|
{ |
295 |
6 |
result = Integer.parseInt(s); |
296 |
|
} catch (NumberFormatException ex) |
297 |
|
{ |
298 |
|
} |
299 |
|
} |
300 |
8 |
return result; |
301 |
|
} |
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
@param |
308 |
|
@param |
309 |
|
@return |
310 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
311 |
15 |
public static int compareVersions(String v1, String v2)... |
312 |
|
{ |
313 |
15 |
return compareVersions(v1, v2, null); |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
|
@param |
321 |
|
@param |
322 |
|
@param |
323 |
|
|
324 |
|
|
325 |
|
@return |
326 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (36) |
Complexity: 9 |
Complexity Density: 0.38 |
|
327 |
89 |
public static int compareVersions(String v1, String v2,... |
328 |
|
String pointSeparator) |
329 |
|
{ |
330 |
89 |
if (v1 == null || v2 == null) |
331 |
|
{ |
332 |
2 |
return 0; |
333 |
|
} |
334 |
87 |
String[] toks1 = v1.split("\\."); |
335 |
87 |
String[] toks2 = v2.split("\\."); |
336 |
87 |
int i = 0; |
337 |
205 |
for (; i < toks1.length; i++) |
338 |
|
{ |
339 |
196 |
if (i >= toks2.length) |
340 |
|
{ |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
3 |
return 1; |
345 |
|
} |
346 |
193 |
String tok1 = toks1[i]; |
347 |
193 |
String tok2 = toks2[i]; |
348 |
193 |
if (pointSeparator != null) |
349 |
|
{ |
350 |
|
|
351 |
|
|
352 |
|
|
353 |
166 |
tok1 = tok1.replace(pointSeparator, "."); |
354 |
166 |
tok2 = tok2.replace(pointSeparator, "."); |
355 |
|
} |
356 |
193 |
try |
357 |
|
{ |
358 |
193 |
float f1 = Float.valueOf(tok1); |
359 |
191 |
float f2 = Float.valueOf(tok2); |
360 |
189 |
int comp = Float.compare(f1, f2); |
361 |
189 |
if (comp != 0) |
362 |
|
{ |
363 |
71 |
return comp; |
364 |
|
} |
365 |
|
} catch (NumberFormatException e) |
366 |
|
{ |
367 |
4 |
System.err |
368 |
|
.println("Invalid version format found: " + e.getMessage()); |
369 |
4 |
return 0; |
370 |
|
} |
371 |
|
} |
372 |
|
|
373 |
9 |
if (i < toks2.length) |
374 |
|
{ |
375 |
|
|
376 |
|
|
377 |
|
|
378 |
3 |
return -1; |
379 |
|
} |
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
6 |
return 0; |
385 |
|
} |
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
@param |
392 |
|
@return |
393 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
394 |
26 |
public static String toSentenceCase(String s)... |
395 |
|
{ |
396 |
26 |
if (s == null) |
397 |
|
{ |
398 |
1 |
return s; |
399 |
|
} |
400 |
25 |
if (s.length() <= 1) |
401 |
|
{ |
402 |
2 |
return s.toUpperCase(); |
403 |
|
} |
404 |
23 |
return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
@param |
412 |
|
@return |
413 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 8 |
Complexity Density: 0.44 |
|
414 |
36 |
public static String stripHtmlTags(String text)... |
415 |
|
{ |
416 |
36 |
if (text == null) |
417 |
|
{ |
418 |
1 |
return null; |
419 |
|
} |
420 |
35 |
String tmp2up = text.toUpperCase(); |
421 |
35 |
int startTag = tmp2up.indexOf("<HTML>"); |
422 |
35 |
if (startTag > -1) |
423 |
|
{ |
424 |
4 |
text = text.substring(startTag + 6); |
425 |
4 |
tmp2up = tmp2up.substring(startTag + 6); |
426 |
|
} |
427 |
|
|
428 |
35 |
int endTag = tmp2up.indexOf("</BODY>"); |
429 |
35 |
if (endTag > -1) |
430 |
|
{ |
431 |
2 |
text = text.substring(0, endTag); |
432 |
2 |
tmp2up = tmp2up.substring(0, endTag); |
433 |
|
} |
434 |
35 |
endTag = tmp2up.indexOf("</HTML>"); |
435 |
35 |
if (endTag > -1) |
436 |
|
{ |
437 |
2 |
text = text.substring(0, endTag); |
438 |
|
} |
439 |
|
|
440 |
35 |
if (startTag == -1 && (text.contains("<") || text.contains(">"))) |
441 |
|
{ |
442 |
3 |
text = text.replaceAll("<", "<"); |
443 |
3 |
text = text.replaceAll(">", ">"); |
444 |
|
} |
445 |
35 |
return text; |
446 |
|
} |
447 |
|
} |