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