JAL-4059 Converting Pattern/Matcher methods that are possibly incompatible with JS...
[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    * 
148    * @param input
149    * @param delimiter
150    * @return elements separated by separator
151    */
152   public static String[] separatorListToArray(String input,
153           String delimiter)
154   {
155     int seplen = delimiter.length();
156     if (input == null || input.equals("") || input.equals(delimiter))
157     {
158       return null;
159     }
160     List<String> jv = new ArrayList<>();
161     int cp = 0, pos, escape;
162     boolean wasescaped = false, wasquoted = false;
163     String lstitem = null;
164     while ((pos = input.indexOf(delimiter, cp)) >= cp)
165     {
166       escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
167       if (wasescaped || wasquoted)
168       {
169         // append to previous pos
170         jv.set(jv.size() - 1, lstitem = lstitem + delimiter
171                 + input.substring(cp, pos + escape));
172       }
173       else
174       {
175         jv.add(lstitem = input.substring(cp, pos + escape));
176       }
177       cp = pos + seplen;
178       wasescaped = escape == -1;
179       // last separator may be in an unmatched quote
180       // private static final Pattern DELIMITERS_PATTERN =
181       // Pattern.compile(".*='[^']*(?!')");
182       wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
183     }
184     if (cp < input.length())
185     {
186       String c = input.substring(cp);
187       if (wasescaped || wasquoted)
188       {
189         // append final separator
190         jv.set(jv.size() - 1, lstitem + delimiter + c);
191       }
192       else
193       {
194         if (!c.equals(delimiter))
195         {
196           jv.add(c);
197         }
198       }
199     }
200     if (jv.size() > 0)
201     {
202       String[] v = jv.toArray(new String[jv.size()]);
203       jv.clear();
204       if (DEBUG)
205       {
206         jalview.bin.Console.errPrintln("Array from '" + delimiter
207                 + "' separated List:\n" + v.length);
208         for (int i = 0; i < v.length; i++)
209         {
210           jalview.bin.Console.errPrintln("item " + i + " '" + v[i] + "'");
211         }
212       }
213       return v;
214     }
215     if (DEBUG)
216     {
217       jalview.bin.Console.errPrintln(
218               "Empty Array from '" + delimiter + "' separated List");
219     }
220     return null;
221   }
222
223   /**
224    * Returns a string which contains the list elements delimited by the
225    * separator. Null items are ignored. If the input is null or has length zero,
226    * a single delimiter is returned.
227    * 
228    * @param list
229    * @param separator
230    * @return concatenated string
231    */
232   public static String arrayToSeparatorList(String[] list, String separator)
233   {
234     StringBuffer v = new StringBuffer();
235     if (list != null && list.length > 0)
236     {
237       for (int i = 0, iSize = list.length; i < iSize; i++)
238       {
239         if (list[i] != null)
240         {
241           if (v.length() > 0)
242           {
243             v.append(separator);
244           }
245           // TODO - escape any separator values in list[i]
246           v.append(list[i]);
247         }
248       }
249       if (DEBUG)
250       {
251         System.err
252                 .println("Returning '" + separator + "' separated List:\n");
253         jalview.bin.Console.errPrintln(v);
254       }
255       return v.toString();
256     }
257     if (DEBUG)
258     {
259       jalview.bin.Console.errPrintln(
260               "Returning empty '" + separator + "' separated List\n");
261     }
262     return "" + separator;
263   }
264
265   /**
266    * Converts a list to a string with a delimiter before each term except the
267    * first. Returns an empty string given a null or zero-length argument. This
268    * can be replaced with StringJoiner in Java 8.
269    * 
270    * @param terms
271    * @param delim
272    * @return
273    */
274   public static String listToDelimitedString(List<String> terms,
275           String delim)
276   {
277     StringBuilder sb = new StringBuilder(32);
278     if (terms != null && !terms.isEmpty())
279     {
280       boolean appended = false;
281       for (String term : terms)
282       {
283         if (appended)
284         {
285           sb.append(delim);
286         }
287         appended = true;
288         sb.append(term);
289       }
290     }
291     return sb.toString();
292   }
293
294   /**
295    * Convenience method to parse a string to an integer, returning 0 if the
296    * input is null or not a valid integer
297    * 
298    * @param s
299    * @return
300    */
301   public static int parseInt(String s)
302   {
303     int result = 0;
304     if (s != null && s.length() > 0)
305     {
306       try
307       {
308         result = Integer.parseInt(s);
309       } catch (NumberFormatException ex)
310       {
311       }
312     }
313     return result;
314   }
315
316   /**
317    * Compares two versions formatted as e.g. "3.4.5" and returns -1, 0 or 1 as
318    * the first version precedes, is equal to, or follows the second
319    * 
320    * @param v1
321    * @param v2
322    * @return
323    */
324   public static int compareVersions(String v1, String v2)
325   {
326     return compareVersions(v1, v2, null);
327   }
328
329   /**
330    * Compares two versions formatted as e.g. "3.4.5b1" and returns -1, 0 or 1 as
331    * the first version precedes, is equal to, or follows the second
332    * 
333    * @param v1
334    * @param v2
335    * @param pointSeparator
336    *          a string used to delimit point increments in sub-tokens of the
337    *          version
338    * @return
339    */
340   public static int compareVersions(String v1, String v2,
341           String pointSeparator)
342   {
343     if (v1 == null || v2 == null)
344     {
345       return 0;
346     }
347     String[] toks1 = v1.split("\\.");
348     String[] toks2 = v2.split("\\.");
349     int i = 0;
350     for (; i < toks1.length; i++)
351     {
352       if (i >= toks2.length)
353       {
354         /*
355          * extra tokens in v1
356          */
357         return 1;
358       }
359       String tok1 = toks1[i];
360       String tok2 = toks2[i];
361       if (pointSeparator != null)
362       {
363         /*
364          * convert e.g. 5b2 into decimal 5.2 for comparison purposes
365          */
366         tok1 = tok1.replace(pointSeparator, ".");
367         tok2 = tok2.replace(pointSeparator, ".");
368       }
369       try
370       {
371         float f1 = Float.valueOf(tok1);
372         float f2 = Float.valueOf(tok2);
373         int comp = Float.compare(f1, f2);
374         if (comp != 0)
375         {
376           return comp;
377         }
378       } catch (NumberFormatException e)
379       {
380         System.err
381                 .println("Invalid version format found: " + e.getMessage());
382         return 0;
383       }
384     }
385
386     if (i < toks2.length)
387     {
388       /*
389        * extra tokens in v2 
390        */
391       return -1;
392     }
393
394     /*
395      * same length, all tokens match
396      */
397     return 0;
398   }
399
400   /**
401    * Converts the string to all lower-case except the first character which is
402    * upper-cased
403    * 
404    * @param s
405    * @return
406    */
407   public static String toSentenceCase(String s)
408   {
409     if (s == null)
410     {
411       return s;
412     }
413     if (s.length() <= 1)
414     {
415       return s.toUpperCase(Locale.ROOT);
416     }
417     return s.substring(0, 1).toUpperCase(Locale.ROOT)
418             + s.substring(1).toLowerCase(Locale.ROOT);
419   }
420
421   /**
422    * A helper method that strips off any leading or trailing html and body tags.
423    * If no html tag is found, then also html-encodes angle bracket characters.
424    * 
425    * @param text
426    * @return
427    */
428   public static String stripHtmlTags(String text)
429   {
430     if (text == null)
431     {
432       return null;
433     }
434     String tmp2up = text.toUpperCase(Locale.ROOT);
435     int startTag = tmp2up.indexOf("<HTML>");
436     if (startTag > -1)
437     {
438       text = text.substring(startTag + 6);
439       tmp2up = tmp2up.substring(startTag + 6);
440     }
441     // is omission of "<BODY>" intentional here??
442     int endTag = tmp2up.indexOf("</BODY>");
443     if (endTag > -1)
444     {
445       text = text.substring(0, endTag);
446       tmp2up = tmp2up.substring(0, endTag);
447     }
448     endTag = tmp2up.indexOf("</HTML>");
449     if (endTag > -1)
450     {
451       text = text.substring(0, endTag);
452     }
453
454     if (startTag == -1 && (text.contains("<") || text.contains(">")))
455     {
456       text = text.replaceAll("<", "&lt;");
457       text = text.replaceAll(">", "&gt;");
458     }
459     return text;
460   }
461
462   /**
463    * Answers the input string with any occurrences of the 'encodeable'
464    * characters replaced by their URL encoding
465    * 
466    * @param s
467    * @param encodable
468    * @return
469    */
470   public static String urlEncode(String s, String encodable)
471   {
472     if (s == null || s.isEmpty())
473     {
474       return s;
475     }
476
477     /*
478      * do % encoding first, as otherwise it may double-encode!
479      */
480     if (encodable.indexOf(PERCENT) != -1)
481     {
482       s = urlEncode(s, PERCENT);
483     }
484
485     for (char c : encodable.toCharArray())
486     {
487       if (c != PERCENT)
488       {
489         s = urlEncode(s, c);
490       }
491     }
492     return s;
493   }
494
495   /**
496    * Answers the input string with any occurrences of {@code c} replaced with
497    * their url encoding. Answers the input string if it is unchanged.
498    * 
499    * @param s
500    * @param c
501    * @return
502    */
503   static String urlEncode(String s, char c)
504   {
505     String decoded = String.valueOf(c);
506     if (s.indexOf(decoded) != -1)
507     {
508       String encoded = getUrlEncoding(c);
509       if (!encoded.equals(decoded))
510       {
511         s = s.replace(decoded, encoded);
512       }
513     }
514     return s;
515   }
516
517   /**
518    * Answers the input string with any occurrences of the specified (unencoded)
519    * characters replaced by their URL decoding.
520    * <p>
521    * Example: {@code urlDecode("a%3Db%3Bc", "-;=,")} should answer
522    * {@code "a=b;c"}.
523    * 
524    * @param s
525    * @param encodable
526    * @return
527    */
528   public static String urlDecode(String s, String encodable)
529   {
530     if (s == null || s.isEmpty())
531     {
532       return s;
533     }
534
535     for (char c : encodable.toCharArray())
536     {
537       String encoded = getUrlEncoding(c);
538       if (s.indexOf(encoded) != -1)
539       {
540         String decoded = String.valueOf(c);
541         s = s.replace(encoded, decoded);
542       }
543     }
544     return s;
545   }
546
547   /**
548    * Does a lazy lookup of the url encoding of the given character, saving the
549    * value for repeat lookups
550    * 
551    * @param c
552    * @return
553    */
554   private static String getUrlEncoding(char c)
555   {
556     if (c < 0 || c >= urlEncodings.length)
557     {
558       return String.valueOf(c);
559     }
560
561     String enc = urlEncodings[c];
562     if (enc == null)
563     {
564       try
565       {
566         enc = urlEncodings[c] = URLEncoder.encode(String.valueOf(c),
567                 "UTF-8");
568       } catch (UnsupportedEncodingException e)
569       {
570         enc = urlEncodings[c] = String.valueOf(c);
571       }
572     }
573     return enc;
574   }
575
576   public static int firstCharPosIgnoreCase(String text, String chars)
577   {
578     int min = text.length() + 1;
579     for (char c : chars.toLowerCase(Locale.ROOT).toCharArray())
580     {
581       int i = text.toLowerCase(Locale.ROOT).indexOf(c);
582       if (0 <= i && i < min)
583       {
584         min = i;
585       }
586     }
587     return min < text.length() + 1 ? min : -1;
588   }
589
590   public static boolean equalsIgnoreCase(String s1, String s2)
591   {
592     if (s1 == null || s2 == null)
593     {
594       return s1 == s2;
595     }
596     return s1.toLowerCase(Locale.ROOT).equals(s2.toLowerCase(Locale.ROOT));
597   }
598
599   public static int indexOfFirstWhitespace(String text)
600   {
601     // Rewritten to not use regex for Jalviewjs. Probably more efficient this
602     // way anyway.
603     if (text == null)
604     {
605       return -1;
606     }
607     for (int i = 0; i < text.length(); i++)
608     {
609       if (Character.isWhitespace(text.charAt(i)))
610       {
611         return i;
612       }
613     }
614     return -1;
615   }
616
617   /*
618    * implementation of String.replaceLast.
619    * Replaces only the last occurrence of toReplace in string with replacement.
620    */
621   public static String replaceLast(String string, String toReplace,
622           String replacement)
623   {
624     int pos = string.lastIndexOf(toReplace);
625     if (pos > -1)
626     {
627       return new StringBuilder().append(string.substring(0, pos))
628               .append(replacement)
629               .append(string.substring(pos + toReplace.length()))
630               .toString();
631     }
632     else
633     {
634       return string;
635     }
636
637   }
638
639   /* 
640    * return the maximum length of a List of Strings
641    */
642   public static int maxLength(List<String> l)
643   {
644     int max = 0;
645     for (String s : l)
646     {
647       if (s == null)
648         continue;
649       if (s.length() > max)
650         max = s.length();
651     }
652     return max;
653   }
654 }