JAL-1645 source formatting and organise imports
[jalview.git] / src / jalview / util / StringUtils.java
1 package jalview.util;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.regex.Pattern;
6
7 public class StringUtils
8 {
9   private static final Pattern DELIMITERS_PATTERN = Pattern
10           .compile(".*='[^']*(?!')");
11
12   private static final boolean DEBUG = false;
13
14   /**
15    * Returns a new character array, after inserting characters into the given
16    * character array.
17    * 
18    * @param in
19    *          the character array to insert into
20    * @param position
21    *          the 0-based position for insertion
22    * @param count
23    *          the number of characters to insert
24    * @param ch
25    *          the character to insert
26    */
27   public static final char[] insertCharAt(char[] in, int position,
28           int count, char ch)
29   {
30     char[] tmp = new char[in.length + count];
31
32     if (position >= in.length)
33     {
34       System.arraycopy(in, 0, tmp, 0, in.length);
35       position = in.length;
36     }
37     else
38     {
39       System.arraycopy(in, 0, tmp, 0, position);
40     }
41
42     int index = position;
43     while (count > 0)
44     {
45       tmp[index++] = ch;
46       count--;
47     }
48
49     if (position < in.length)
50     {
51       System.arraycopy(in, position, tmp, index, in.length - position);
52     }
53
54     return tmp;
55   }
56
57   /**
58    * Delete
59    * 
60    * @param in
61    * @param from
62    * @param to
63    * @return
64    */
65   public static final char[] deleteChars(char[] in, int from, int to)
66   {
67     if (from >= in.length || from < 0)
68     {
69       return in;
70     }
71
72     char[] tmp;
73
74     if (to >= in.length)
75     {
76       tmp = new char[from];
77       System.arraycopy(in, 0, tmp, 0, from);
78       to = in.length;
79     }
80     else
81     {
82       tmp = new char[in.length - to + from];
83       System.arraycopy(in, 0, tmp, 0, from);
84       System.arraycopy(in, to, tmp, from, in.length - to);
85     }
86     return tmp;
87   }
88
89   /**
90    * Returns the last part of 'input' after the last occurrence of 'token'. For
91    * example to extract only the filename from a full path or URL.
92    * 
93    * @param input
94    * @param token
95    *          a delimiter which must be in regular expression format
96    * @return
97    */
98   public static String getLastToken(String input, String token)
99   {
100     if (input == null)
101     {
102       return null;
103     }
104     if (token == null)
105     {
106       return input;
107     }
108     String[] st = input.split(token);
109     return st[st.length - 1];
110   }
111
112   /**
113    * Parses the input string into components separated by the delimiter. Unlike
114    * String.split(), this method will ignore occurrences of the delimiter which
115    * are nested within single quotes in name-value pair values, e.g. a='b,c'.
116    * 
117    * @param input
118    * @param delimiter
119    * @return elements separated by separator
120    */
121   public static String[] separatorListToArray(String input, String delimiter)
122   {
123     int seplen = delimiter.length();
124     if (input == null || input.equals("") || input.equals(delimiter))
125     {
126       return null;
127     }
128     List<String> jv = new ArrayList<String>();
129     int cp = 0, pos, escape;
130     boolean wasescaped = false, wasquoted = false;
131     String lstitem = null;
132     while ((pos = input.indexOf(delimiter, cp)) >= cp)
133     {
134       escape = (pos > 0 && input.charAt(pos - 1) == '\\') ? -1 : 0;
135       if (wasescaped || wasquoted)
136       {
137         // append to previous pos
138         jv.set(jv.size() - 1,
139                 lstitem = lstitem + delimiter
140                         + input.substring(cp, pos + escape));
141       }
142       else
143       {
144         jv.add(lstitem = input.substring(cp, pos + escape));
145       }
146       cp = pos + seplen;
147       wasescaped = escape == -1;
148       // last separator may be in an unmatched quote
149       wasquoted = DELIMITERS_PATTERN.matcher(lstitem).matches();
150     }
151     if (cp < input.length())
152     {
153       String c = input.substring(cp);
154       if (wasescaped || wasquoted)
155       {
156         // append final separator
157         jv.set(jv.size() - 1, lstitem + delimiter + c);
158       }
159       else
160       {
161         if (!c.equals(delimiter))
162         {
163           jv.add(c);
164         }
165       }
166     }
167     if (jv.size() > 0)
168     {
169       String[] v = jv.toArray(new String[jv.size()]);
170       jv.clear();
171       if (DEBUG)
172       {
173         System.err.println("Array from '" + delimiter
174                 + "' separated List:\n" + v.length);
175         for (int i = 0; i < v.length; i++)
176         {
177           System.err.println("item " + i + " '" + v[i] + "'");
178         }
179       }
180       return v;
181     }
182     if (DEBUG)
183     {
184       System.err.println("Empty Array from '" + delimiter
185               + "' separated List");
186     }
187     return null;
188   }
189
190   /**
191    * Returns a string which contains the list elements delimited by the
192    * separator. Null items are ignored. If the input is null or has length zero,
193    * a single delimiter is returned.
194    * 
195    * @param list
196    * @param separator
197    * @return concatenated string
198    */
199   public static String arrayToSeparatorList(String[] list, String separator)
200   {
201     StringBuffer v = new StringBuffer();
202     if (list != null && list.length > 0)
203     {
204       for (int i = 0, iSize = list.length; i < iSize; i++)
205       {
206         if (list[i] != null)
207         {
208           if (v.length() > 0)
209           {
210             v.append(separator);
211           }
212           // TODO - escape any separator values in list[i]
213           v.append(list[i]);
214         }
215       }
216       if (DEBUG)
217       {
218         System.err.println("Returning '" + separator
219                 + "' separated List:\n");
220         System.err.println(v);
221       }
222       return v.toString();
223     }
224     if (DEBUG)
225     {
226       System.err.println("Returning empty '" + separator
227               + "' separated List\n");
228     }
229     return "" + separator;
230   }
231 }