Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.util

File StringUtils.java

 

Coverage histogram

../../img/srcFileCovDistChart4.png
40% of files have more coverage

Code metrics

112
193
20
1
647
426
96
0.5
9.65
20
4.8

Classes

Class Line # Actions
StringUtils 31 193 96
0.3753846337.5%
 

Contributing tests

This file is covered by 216 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
7    * Jalview is free software: you can redistribute it and/or
8    * modify it under the terms of the GNU General Public License
9    * as published by the Free Software Foundation, either version 3
10    * of the License, or (at your option) any later version.
11    *
12    * Jalview is distributed in the hope that it will be useful, but
13    * WITHOUT ANY WARRANTY; without even the implied warranty
14    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15    * PURPOSE. See the GNU General Public License for more details.
16    *
17    * You should have received a copy of the GNU General Public License
18    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19    * The Jalview Authors are detailed in the 'AUTHORS' file.
20    */
21    package jalview.util;
22   
23    import java.io.UnsupportedEncodingException;
24    import java.net.URLEncoder;
25    import java.util.ArrayList;
26    import java.util.List;
27    import java.util.Locale;
28    import java.util.regex.Matcher;
29    import java.util.regex.Pattern;
30   
 
31    public class StringUtils
32    {
33    private static final Pattern DELIMITERS_PATTERN = Pattern
34    .compile(".*='[^']*(?!')");
35   
36    private static final char PERCENT = '%';
37   
38    private static final boolean DEBUG = false;
39   
40    /*
41    * URL encoded characters, indexed by char value
42    * e.g. urlEncodings['='] = urlEncodings[61] = "%3D"
43    */
44    private static String[] urlEncodings = new String[255];
45   
46    /**
47    * Returns a new character array, after inserting characters into the given
48    * character array.
49    *
50    * @param in
51    * the character array to insert into
52    * @param position
53    * the 0-based position for insertion
54    * @param count
55    * the number of characters to insert
56    * @param ch
57    * the character to insert
58    */
 
59  16 toggle public static final char[] insertCharAt(char[] in, int position,
60    int count, char ch)
61    {
62  16 char[] tmp = new char[in.length + count];
63   
64  16 if (position >= in.length)
65    {
66  0 System.arraycopy(in, 0, tmp, 0, in.length);
67  0 position = in.length;
68    }
69    else
70    {
71  16 System.arraycopy(in, 0, tmp, 0, position);
72    }
73   
74  16 int index = position;
75  44 while (count > 0)
76    {
77  28 tmp[index++] = ch;
78  28 count--;
79    }
80   
81  16 if (position < in.length)
82    {
83  16 System.arraycopy(in, position, tmp, index, in.length - position);
84    }
85   
86  16 return tmp;
87    }
88   
89    /**
90    * Delete
91    *
92    * @param in
93    * @param from
94    * @param to
95    * @return
96    */
 
97  156 toggle public static final char[] deleteChars(char[] in, int from, int to)
98    {
99  156 if (from >= in.length || from < 0)
100    {
101  0 return in;
102    }
103   
104  156 char[] tmp;
105   
106  156 if (to >= in.length)
107    {
108  20 tmp = new char[from];
109  20 System.arraycopy(in, 0, tmp, 0, from);
110  20 to = in.length;
111    }
112    else
113    {
114  136 tmp = new char[in.length - to + from];
115  136 System.arraycopy(in, 0, tmp, 0, from);
116  136 System.arraycopy(in, to, tmp, from, in.length - to);
117    }
118  156 return tmp;
119    }
120   
121    /**
122    * Returns the last part of 'input' after the last occurrence of 'token'. For
123    * example to extract only the filename from a full path or URL.
124    *
125    * @param input
126    * @param token
127    * a delimiter which must be in regular expression format
128    * @return
129    */
 
130  0 toggle public static String getLastToken(String input, String token)
131    {
132  0 if (input == null)
133    {
134  0 return null;
135    }
136  0 if (token == null)
137    {
138  0 return input;
139    }
140  0 String[] st = input.split(token);
141  0 return st[st.length - 1];
142    }
143   
144    /**
145    * Parses the input string into components separated by the delimiter. Unlike
146    * String.split(), this method will ignore occurrences of the delimiter which
147    * are nested within single quotes in name-value pair values, e.g. a='b,c'.
148    *
149    * @param input
150    * @param delimiter
151    * @return elements separated by separator
152    */
 
153  8224 toggle public static String[] separatorListToArray(String input,
154    String delimiter)
155    {
156  8224 int seplen = delimiter.length();
157  8224 if (input == null || input.equals("") || input.equals(delimiter))
158    {
159  0 return null;
160    }
161  8224 List<String> jv = new ArrayList<>();
162  8224 int cp = 0, pos, escape;
163  8224 boolean wasescaped = false, wasquoted = false;
164  8224 String lstitem = null;
165  ? while ((pos = input.indexOf(delimiter, cp)) >= cp)
166    {
167  22616 escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
168  22616 if (wasescaped || wasquoted)
169    {
170    // append to previous pos
171  0 jv.set(jv.size() - 1, lstitem = lstitem + delimiter
172    + input.substring(cp, pos + escape));
173    }
174    else
175    {
176  22616 jv.add(lstitem = input.substring(cp, pos + escape));
177    }
178  22616 cp = pos + seplen;
179  22616 wasescaped = escape == -1;
180    // last separator may be in an unmatched quote
181  22616 wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
182    }
183  8224 if (cp < input.length())
184    {
185  8224 String c = input.substring(cp);
186  8224 if (wasescaped || wasquoted)
187    {
188    // append final separator
189  0 jv.set(jv.size() - 1, lstitem + delimiter + c);
190    }
191    else
192    {
193  8224 if (!c.equals(delimiter))
194    {
195  8224 jv.add(c);
196    }
197    }
198    }
199  8224 if (jv.size() > 0)
200    {
201  8224 String[] v = jv.toArray(new String[jv.size()]);
202  8224 jv.clear();
203  8224 if (DEBUG)
204    {
205  0 ErrorLog.errPrintln("Array from '" + delimiter
206    + "' separated List:\n" + v.length);
207  0 for (int i = 0; i < v.length; i++)
208    {
209  0 ErrorLog.errPrintln("item " + i + " '" + v[i] + "'");
210    }
211    }
212  8224 return v;
213    }
214  0 if (DEBUG)
215    {
216  0 ErrorLog.errPrintln(
217    "Empty Array from '" + delimiter + "' separated List");
218    }
219  0 return null;
220    }
221   
222    /**
223    * Returns a string which contains the list elements delimited by the
224    * separator. Null items are ignored. If the input is null or has length zero,
225    * a single delimiter is returned.
226    *
227    * @param list
228    * @param separator
229    * @return concatenated string
230    */
 
231  15 toggle public static String arrayToSeparatorList(String[] list, String separator)
232    {
233  15 StringBuffer v = new StringBuffer();
234  15 if (list != null && list.length > 0)
235    {
236  55 for (int i = 0, iSize = list.length; i < iSize; i++)
237    {
238  40 if (list[i] != null)
239    {
240  40 if (v.length() > 0)
241    {
242  25 v.append(separator);
243    }
244    // TODO - escape any separator values in list[i]
245  40 v.append(list[i]);
246    }
247    }
248  15 if (DEBUG)
249    {
250  0 System.err
251    .println("Returning '" + separator + "' separated List:\n");
252  0 ErrorLog.errPrintln(v.toString());
253    }
254  15 return v.toString();
255    }
256  0 if (DEBUG)
257    {
258  0 ErrorLog.errPrintln(
259    "Returning empty '" + separator + "' separated List\n");
260    }
261  0 return "" + separator;
262    }
263   
264    /**
265    * Converts a list to a string with a delimiter before each term except the
266    * first. Returns an empty string given a null or zero-length argument. This
267    * can be replaced with StringJoiner in Java 8.
268    *
269    * @param terms
270    * @param delim
271    * @return
272    */
 
273  0 toggle public static String listToDelimitedString(List<String> terms,
274    String delim)
275    {
276  0 StringBuilder sb = new StringBuilder(32);
277  0 if (terms != null && !terms.isEmpty())
278    {
279  0 boolean appended = false;
280  0 for (String term : terms)
281    {
282  0 if (appended)
283    {
284  0 sb.append(delim);
285    }
286  0 appended = true;
287  0 sb.append(term);
288    }
289    }
290  0 return sb.toString();
291    }
292   
293    /**
294    * Convenience method to parse a string to an integer, returning 0 if the
295    * input is null or not a valid integer
296    *
297    * @param s
298    * @return
299    */
 
300  0 toggle public static int parseInt(String s)
301    {
302  0 int result = 0;
303  0 if (s != null && s.length() > 0)
304    {
305  0 try
306    {
307  0 result = Integer.parseInt(s);
308    } catch (NumberFormatException ex)
309    {
310    }
311    }
312  0 return result;
313    }
314   
315    /**
316    * Compares two versions formatted as e.g. "3.4.5" and returns -1, 0 or 1 as
317    * the first version precedes, is equal to, or follows the second
318    *
319    * @param v1
320    * @param v2
321    * @return
322    */
 
323  0 toggle public static int compareVersions(String v1, String v2)
324    {
325  0 return compareVersions(v1, v2, null);
326    }
327   
328    /**
329    * Compares two versions formatted as e.g. "3.4.5b1" and returns -1, 0 or 1 as
330    * the first version precedes, is equal to, or follows the second
331    *
332    * @param v1
333    * @param v2
334    * @param pointSeparator
335    * a string used to delimit point increments in sub-tokens of the
336    * version
337    * @return
338    */
 
339  0 toggle public static int compareVersions(String v1, String v2,
340    String pointSeparator)
341    {
342  0 if (v1 == null || v2 == null)
343    {
344  0 return 0;
345    }
346  0 String[] toks1 = v1.split("\\.");
347  0 String[] toks2 = v2.split("\\.");
348  0 int i = 0;
349  0 for (; i < toks1.length; i++)
350    {
351  0 if (i >= toks2.length)
352    {
353    /*
354    * extra tokens in v1
355    */
356  0 return 1;
357    }
358  0 String tok1 = toks1[i];
359  0 String tok2 = toks2[i];
360  0 if (pointSeparator != null)
361    {
362    /*
363    * convert e.g. 5b2 into decimal 5.2 for comparison purposes
364    */
365  0 tok1 = tok1.replace(pointSeparator, ".");
366  0 tok2 = tok2.replace(pointSeparator, ".");
367    }
368  0 try
369    {
370  0 float f1 = Float.valueOf(tok1);
371  0 float f2 = Float.valueOf(tok2);
372  0 int comp = Float.compare(f1, f2);
373  0 if (comp != 0)
374    {
375  0 return comp;
376    }
377    } catch (NumberFormatException e)
378    {
379  0 System.err
380    .println("Invalid version format found: " + e.getMessage());
381  0 return 0;
382    }
383    }
384   
385  0 if (i < toks2.length)
386    {
387    /*
388    * extra tokens in v2
389    */
390  0 return -1;
391    }
392   
393    /*
394    * same length, all tokens match
395    */
396  0 return 0;
397    }
398   
399    /**
400    * Converts the string to all lower-case except the first character which is
401    * upper-cased
402    *
403    * @param s
404    * @return
405    */
 
406  67 toggle public static String toSentenceCase(String s)
407    {
408  67 if (s == null)
409    {
410  0 return s;
411    }
412  67 if (s.length() <= 1)
413    {
414  0 return s.toUpperCase(Locale.ROOT);
415    }
416  67 return s.substring(0, 1).toUpperCase(Locale.ROOT)
417    + s.substring(1).toLowerCase(Locale.ROOT);
418    }
419   
420    /**
421    * A helper method that strips off any leading or trailing html and body tags.
422    * If no html tag is found, then also html-encodes angle bracket characters.
423    *
424    * @param text
425    * @return
426    */
 
427  41 toggle public static String stripHtmlTags(String text)
428    {
429  41 if (text == null)
430    {
431  0 return null;
432    }
433  41 String tmp2up = text.toUpperCase(Locale.ROOT);
434  41 int startTag = tmp2up.indexOf("<HTML>");
435  41 if (startTag > -1)
436    {
437  1 text = text.substring(startTag + 6);
438  1 tmp2up = tmp2up.substring(startTag + 6);
439    }
440    // is omission of "<BODY>" intentional here??
441  41 int endTag = tmp2up.indexOf("</BODY>");
442  41 if (endTag > -1)
443    {
444  1 text = text.substring(0, endTag);
445  1 tmp2up = tmp2up.substring(0, endTag);
446    }
447  41 endTag = tmp2up.indexOf("</HTML>");
448  41 if (endTag > -1)
449    {
450  0 text = text.substring(0, endTag);
451    }
452   
453  41 if (startTag == -1 && (text.contains("<") || text.contains(">")))
454    {
455  1 text = text.replaceAll("<", "&lt;");
456  1 text = text.replaceAll(">", "&gt;");
457    }
458  41 return text;
459    }
460   
461    /**
462    * Answers the input string with any occurrences of the 'encodeable'
463    * characters replaced by their URL encoding
464    *
465    * @param s
466    * @param encodable
467    * @return
468    */
 
469  0 toggle public static String urlEncode(String s, String encodable)
470    {
471  0 if (s == null || s.isEmpty())
472    {
473  0 return s;
474    }
475   
476    /*
477    * do % encoding first, as otherwise it may double-encode!
478    */
479  0 if (encodable.indexOf(PERCENT) != -1)
480    {
481  0 s = urlEncode(s, PERCENT);
482    }
483   
484  0 for (char c : encodable.toCharArray())
485    {
486  0 if (c != PERCENT)
487    {
488  0 s = urlEncode(s, c);
489    }
490    }
491  0 return s;
492    }
493   
494    /**
495    * Answers the input string with any occurrences of {@code c} replaced with
496    * their url encoding. Answers the input string if it is unchanged.
497    *
498    * @param s
499    * @param c
500    * @return
501    */
 
502  0 toggle static String urlEncode(String s, char c)
503    {
504  0 String decoded = String.valueOf(c);
505  0 if (s.indexOf(decoded) != -1)
506    {
507  0 String encoded = getUrlEncoding(c);
508  0 if (!encoded.equals(decoded))
509    {
510  0 s = s.replace(decoded, encoded);
511    }
512    }
513  0 return s;
514    }
515   
516    /**
517    * Answers the input string with any occurrences of the specified (unencoded)
518    * characters replaced by their URL decoding.
519    * <p>
520    * Example: {@code urlDecode("a%3Db%3Bc", "-;=,")} should answer
521    * {@code "a=b;c"}.
522    *
523    * @param s
524    * @param encodable
525    * @return
526    */
 
527  0 toggle public static String urlDecode(String s, String encodable)
528    {
529  0 if (s == null || s.isEmpty())
530    {
531  0 return s;
532    }
533   
534  0 for (char c : encodable.toCharArray())
535    {
536  0 String encoded = getUrlEncoding(c);
537  0 if (s.indexOf(encoded) != -1)
538    {
539  0 String decoded = String.valueOf(c);
540  0 s = s.replace(encoded, decoded);
541    }
542    }
543  0 return s;
544    }
545   
546    /**
547    * Does a lazy lookup of the url encoding of the given character, saving the
548    * value for repeat lookups
549    *
550    * @param c
551    * @return
552    */
 
553  0 toggle private static String getUrlEncoding(char c)
554    {
555  0 if (c < 0 || c >= urlEncodings.length)
556    {
557  0 return String.valueOf(c);
558    }
559   
560  0 String enc = urlEncodings[c];
561  0 if (enc == null)
562    {
563  0 try
564    {
565  0 enc = urlEncodings[c] = URLEncoder.encode(String.valueOf(c),
566    "UTF-8");
567    } catch (UnsupportedEncodingException e)
568    {
569  0 enc = urlEncodings[c] = String.valueOf(c);
570    }
571    }
572  0 return enc;
573    }
574   
 
575  0 toggle public static int firstCharPosIgnoreCase(String text, String chars)
576    {
577  0 int min = text.length() + 1;
578  0 for (char c : chars.toLowerCase(Locale.ROOT).toCharArray())
579    {
580  0 int i = text.toLowerCase(Locale.ROOT).indexOf(c);
581  0 if (0 <= i && i < min)
582    {
583  0 min = i;
584    }
585    }
586  0 return min < text.length() + 1 ? min : -1;
587    }
588   
 
589  12 toggle public static boolean equalsIgnoreCase(String s1, String s2)
590    {
591  12 if (s1 == null || s2 == null)
592    {
593  0 return s1 == s2;
594    }
595  12 return s1.toLowerCase(Locale.ROOT).equals(s2.toLowerCase(Locale.ROOT));
596    }
597   
 
598  1568 toggle public static int indexOfFirstWhitespace(String text)
599    {
600  1568 int index = -1;
601  1568 Pattern pat = Pattern.compile("\\s");
602  1568 Matcher m = pat.matcher(text);
603  1568 if (m.find())
604    {
605  1135 index = m.start();
606    }
607  1568 return index;
608    }
609   
610    /*
611    * implementation of String.replaceLast.
612    * Replaces only the last occurrence of toReplace in string with replacement.
613    */
 
614  0 toggle public static String replaceLast(String string, String toReplace,
615    String replacement)
616    {
617  0 int pos = string.lastIndexOf(toReplace);
618  0 if (pos > -1)
619    {
620  0 return new StringBuilder().append(string.substring(0, pos))
621    .append(replacement)
622    .append(string.substring(pos + toReplace.length()))
623    .toString();
624    }
625    else
626    {
627  0 return string;
628    }
629   
630    }
631   
632    /*
633    * return the maximum length of a List of Strings
634    */
 
635  0 toggle public static int maxLength(List<String> l)
636    {
637  0 int max = 0;
638  0 for (String s : l)
639    {
640  0 if (s == null)
641  0 continue;
642  0 if (s.length() > max)
643  0 max = s.length();
644    }
645  0 return max;
646    }
647    }