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.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.regex.Pattern;
29 public class StringUtils
31 private static final Pattern DELIMITERS_PATTERN = Pattern
32 .compile(".*='[^']*(?!')");
34 private static final boolean DEBUG = false;
37 * Returns a new character array, after inserting characters into the given
41 * the character array to insert into
43 * the 0-based position for insertion
45 * the number of characters to insert
47 * the character to insert
49 public static final char[] insertCharAt(char[] in, int position,
52 char[] tmp = new char[in.length + count];
54 if (position >= in.length)
56 System.arraycopy(in, 0, tmp, 0, in.length);
61 System.arraycopy(in, 0, tmp, 0, position);
71 if (position < in.length)
73 System.arraycopy(in, position, tmp, index, in.length - position);
87 public static final char[] deleteChars(char[] in, int from, int to)
89 if (from >= in.length || from < 0)
99 System.arraycopy(in, 0, tmp, 0, from);
104 tmp = new char[in.length - to + from];
105 System.arraycopy(in, 0, tmp, 0, from);
106 System.arraycopy(in, to, tmp, from, in.length - to);
112 * Returns the last part of 'input' after the last occurrence of 'token'. For
113 * example to extract only the filename from a full path or URL.
117 * a delimiter which must be in regular expression format
120 public static String getLastToken(String input, String token)
130 String[] st = input.split(token);
131 return st[st.length - 1];
135 * Parses the input string into components separated by the delimiter. Unlike
136 * String.split(), this method will ignore occurrences of the delimiter which
137 * are nested within single quotes in name-value pair values, e.g. a='b,c'.
141 * @return elements separated by separator
143 public static String[] separatorListToArray(String input, String delimiter)
145 int seplen = delimiter.length();
146 if (input == null || input.equals("") || input.equals(delimiter))
150 List<String> jv = new ArrayList<String>();
151 int cp = 0, pos, escape;
152 boolean wasescaped = false, wasquoted = false;
153 String lstitem = null;
154 while ((pos = input.indexOf(delimiter, cp)) >= cp)
156 escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
157 if (wasescaped || wasquoted)
159 // append to previous pos
160 jv.set(jv.size() - 1,
161 lstitem = lstitem + delimiter
162 + input.substring(cp, pos + escape));
166 jv.add(lstitem = input.substring(cp, pos + escape));
169 wasescaped = escape == -1;
170 // last separator may be in an unmatched quote
171 wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
173 if (cp < input.length())
175 String c = input.substring(cp);
176 if (wasescaped || wasquoted)
178 // append final separator
179 jv.set(jv.size() - 1, lstitem + delimiter + c);
183 if (!c.equals(delimiter))
191 String[] v = jv.toArray(new String[jv.size()]);
195 System.err.println("Array from '" + delimiter
196 + "' separated List:\n" + v.length);
197 for (int i = 0; i < v.length; i++)
199 System.err.println("item " + i + " '" + v[i] + "'");
206 System.err.println("Empty Array from '" + delimiter
207 + "' separated List");
213 * Returns a string which contains the list elements delimited by the
214 * separator. Null items are ignored. If the input is null or has length zero,
215 * a single delimiter is returned.
219 * @return concatenated string
221 public static String arrayToSeparatorList(String[] list, String separator)
223 StringBuffer v = new StringBuffer();
224 if (list != null && list.length > 0)
226 for (int i = 0, iSize = list.length; i < iSize; i++)
234 // TODO - escape any separator values in list[i]
240 System.err.println("Returning '" + separator
241 + "' separated List:\n");
242 System.err.println(v);
248 System.err.println("Returning empty '" + separator
249 + "' separated List\n");
251 return "" + separator;
255 * Parses the input line to a map of name / value(s) pairs. For example the
257 * Notes=Fe-S;Method=manual curation; source = Pfam; Notes = Metal <br>
258 * if parsed with delimiter=";" and separators {' ', '='} <br>
259 * would return a map with { Notes={Fe=S, Metal}, Method={manual curation},
260 * source={Pfam}} <br>
261 * Note the name/value strings are trimmed of leading / trailing spaces; the
262 * first separator encountered is used
266 * the major delimiter between name-value pairs
268 * one or more separators used between name and value
269 * @return the name-values map (which may be empty but never null)
271 public static Map<String, List<String>> parseNameValuePairs(String line,
272 String delimiter, char[] separators)
274 Map<String, List<String>> map = new HashMap<String, List<String>>();
275 if (line == null || line.trim().length() == 0)
280 for (String pair : line.trim().split(delimiter))
283 if (pair.length() == 0)
289 for (char sep : separators)
291 int pos = pair.indexOf(sep);
292 if (pos > -1 && (sepPos == -1 || pos < sepPos))
300 // no name=value detected
304 String key = pair.substring(0, sepPos).trim();
305 String value = pair.substring(sepPos + 1).trim();
306 if (value.length() > 0)
308 List<String> vals = map.get(key);
311 vals = new ArrayList<String>();
321 * Converts a list to a string with a delimiter before each term except the
322 * first. Returns an empty string given a null or zero-length argument. This
323 * can be replaced with StringJoiner in Java 8.
329 public static String listToDelimitedString(List<String> terms,
332 StringBuilder sb = new StringBuilder(32);
333 if (terms != null && !terms.isEmpty())
335 boolean appended = false;
336 for (String term : terms)
346 return sb.toString();
350 * Convenience method to parse a string to an integer, returning 0 if the
351 * input is null or not a valid integer
356 public static int parseInt(String s)
359 if (s != null && s.length() > 0)
363 result = Integer.parseInt(s);
364 } catch (NumberFormatException ex)