2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
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;
30 public class StringUtils
32 private static final Pattern DELIMITERS_PATTERN = Pattern
33 .compile(".*='[^']*(?!')");
35 private static final char PERCENT = '%';
37 private static final boolean DEBUG = false;
40 * URL encoded characters, indexed by char value
41 * e.g. urlEncodings['='] = urlEncodings[61] = "%3D"
43 private static String[] urlEncodings = new String[255];
46 * Returns a new character array, after inserting characters into the given
50 * the character array to insert into
52 * the 0-based position for insertion
54 * the number of characters to insert
56 * the character to insert
58 public static final char[] insertCharAt(char[] in, int position,
61 char[] tmp = new char[in.length + count];
63 if (position >= in.length)
65 System.arraycopy(in, 0, tmp, 0, in.length);
70 System.arraycopy(in, 0, tmp, 0, position);
80 if (position < in.length)
82 System.arraycopy(in, position, tmp, index, in.length - position);
96 public static final char[] deleteChars(char[] in, int from, int to)
98 if (from >= in.length || from < 0)
107 tmp = new char[from];
108 System.arraycopy(in, 0, tmp, 0, from);
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);
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.
126 * a delimiter which must be in regular expression format
129 public static String getLastToken(String input, String token)
139 String[] st = input.split(token);
140 return st[st.length - 1];
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.
151 * @return elements separated by separator
153 public static String[] separatorListToArray(String input,
157 // these two shouldn't return null (one or two "" respectively)
158 || input.equals("") || input.equals(delimiter))
163 final char escapeChar = '\\';
164 final char quoteChar = '\'';
165 int ilength = input.length();
166 int dlength = delimiter.length();
167 List<String> values = new ArrayList<>();
169 boolean escape = false;
170 boolean inquote = false;
173 for (int i = 0; i < ilength; i++)
175 if (!escape && !inquote && ilength >= i + dlength
176 && input.substring(i, i + dlength).equals(delimiter))
179 values.add(input.substring(start, i));
184 char c = input.charAt(i);
200 // add the last value
201 values.add(input.substring(start, ilength));
203 return values.toArray(new String[values.size()]);
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.
213 * @return concatenated string
215 public static String arrayToSeparatorList(String[] list, String separator)
217 StringBuffer v = new StringBuffer();
218 if (list != null && list.length > 0)
220 for (int i = 0, iSize = list.length; i < iSize; i++)
228 // TODO - escape any separator values in list[i]
235 .println("Returning '" + separator + "' separated List:\n");
236 jalview.bin.Console.errPrintln(v);
242 jalview.bin.Console.errPrintln(
243 "Returning empty '" + separator + "' separated List\n");
245 return "" + separator;
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.
257 public static String listToDelimitedString(List<String> terms,
260 StringBuilder sb = new StringBuilder(32);
261 if (terms != null && !terms.isEmpty())
263 boolean appended = false;
264 for (String term : terms)
274 return sb.toString();
278 * Convenience method to parse a string to an integer, returning 0 if the
279 * input is null or not a valid integer
284 public static int parseInt(String s)
287 if (s != null && s.length() > 0)
291 result = Integer.parseInt(s);
292 } catch (NumberFormatException ex)
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
307 public static int compareVersions(String v1, String v2)
309 return compareVersions(v1, v2, null);
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
318 * @param pointSeparator
319 * a string used to delimit point increments in sub-tokens of the
323 public static int compareVersions(String v1, String v2,
324 String pointSeparator)
326 if (v1 == null || v2 == null)
330 String[] toks1 = v1.split("\\.");
331 String[] toks2 = v2.split("\\.");
333 for (; i < toks1.length; i++)
335 if (i >= toks2.length)
342 String tok1 = toks1[i];
343 String tok2 = toks2[i];
344 if (pointSeparator != null)
347 * convert e.g. 5b2 into decimal 5.2 for comparison purposes
349 tok1 = tok1.replace(pointSeparator, ".");
350 tok2 = tok2.replace(pointSeparator, ".");
354 float f1 = Float.valueOf(tok1);
355 float f2 = Float.valueOf(tok2);
356 int comp = Float.compare(f1, f2);
361 } catch (NumberFormatException e)
364 .println("Invalid version format found: " + e.getMessage());
369 if (i < toks2.length)
378 * same length, all tokens match
384 * Converts the string to all lower-case except the first character which is
390 public static String toSentenceCase(String s)
398 return s.toUpperCase(Locale.ROOT);
400 return s.substring(0, 1).toUpperCase(Locale.ROOT)
401 + s.substring(1).toLowerCase(Locale.ROOT);
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.
411 public static String stripHtmlTags(String text)
417 String tmp2up = text.toUpperCase(Locale.ROOT);
418 int startTag = tmp2up.indexOf("<HTML>");
421 text = text.substring(startTag + 6);
422 tmp2up = tmp2up.substring(startTag + 6);
424 // is omission of "<BODY>" intentional here??
425 int endTag = tmp2up.indexOf("</BODY>");
428 text = text.substring(0, endTag);
429 tmp2up = tmp2up.substring(0, endTag);
431 endTag = tmp2up.indexOf("</HTML>");
434 text = text.substring(0, endTag);
437 if (startTag == -1 && (text.contains("<") || text.contains(">")))
439 text = text.replaceAll("<", "<");
440 text = text.replaceAll(">", ">");
446 * Answers the input string with any occurrences of the 'encodeable'
447 * characters replaced by their URL encoding
453 public static String urlEncode(String s, String encodable)
455 if (s == null || s.isEmpty())
461 * do % encoding first, as otherwise it may double-encode!
463 if (encodable.indexOf(PERCENT) != -1)
465 s = urlEncode(s, PERCENT);
468 for (char c : encodable.toCharArray())
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.
486 static String urlEncode(String s, char c)
488 String decoded = String.valueOf(c);
489 if (s.indexOf(decoded) != -1)
491 String encoded = getUrlEncoding(c);
492 if (!encoded.equals(decoded))
494 s = s.replace(decoded, encoded);
501 * Answers the input string with any occurrences of the specified (unencoded)
502 * characters replaced by their URL decoding.
504 * Example: {@code urlDecode("a%3Db%3Bc", "-;=,")} should answer
511 public static String urlDecode(String s, String encodable)
513 if (s == null || s.isEmpty())
518 for (char c : encodable.toCharArray())
520 String encoded = getUrlEncoding(c);
521 if (s.indexOf(encoded) != -1)
523 String decoded = String.valueOf(c);
524 s = s.replace(encoded, decoded);
531 * Does a lazy lookup of the url encoding of the given character, saving the
532 * value for repeat lookups
537 private static String getUrlEncoding(char c)
539 if (c < 0 || c >= urlEncodings.length)
541 return String.valueOf(c);
544 String enc = urlEncodings[c];
549 enc = urlEncodings[c] = URLEncoder.encode(String.valueOf(c),
551 } catch (UnsupportedEncodingException e)
553 enc = urlEncodings[c] = String.valueOf(c);
559 public static int firstCharPosIgnoreCase(String text, String chars)
561 int min = text.length() + 1;
562 for (char c : chars.toLowerCase(Locale.ROOT).toCharArray())
564 int i = text.toLowerCase(Locale.ROOT).indexOf(c);
565 if (0 <= i && i < min)
570 return min < text.length() + 1 ? min : -1;
573 public static boolean equalsIgnoreCase(String s1, String s2)
575 if (s1 == null || s2 == null)
579 return s1.toLowerCase(Locale.ROOT).equals(s2.toLowerCase(Locale.ROOT));
582 public static int indexOfFirstWhitespace(String text)
584 // Rewritten to not use regex for Jalviewjs. Probably more efficient this
590 for (int i = 0; i < text.length(); i++)
592 if (Character.isWhitespace(text.charAt(i)))
601 * implementation of String.replaceLast.
602 * Replaces only the last occurrence of toReplace in string with replacement.
604 public static String replaceLast(String string, String toReplace,
607 int pos = string.lastIndexOf(toReplace);
610 return new StringBuilder().append(string.substring(0, pos))
612 .append(string.substring(pos + toReplace.length()))
623 * return the maximum length of a List of Strings
625 public static int maxLength(List<String> l)
632 if (s.length() > max)