Refactory: remove unused classes, imports, fields and methods.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / util / Strings.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.util;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.DefaultLineTracker;
15 import org.eclipse.jface.text.ILineTracker;
16 import org.eclipse.jface.text.IRegion;
17
18 /**
19  * Helper class to provide String manipulation functions not available in
20  * standard JDK.
21  */
22 public class Strings {
23
24         /**
25          * Indent char is a space char but not a line delimiters.
26          * <code>== Character.isWhitespace(ch) && ch != '\n' && ch != '\r'</code>
27          */
28         public static boolean isIndentChar(char ch) {
29                 return Character.isWhitespace(ch) && !isLineDelimiterChar(ch);
30         }
31
32         /**
33          * tests if a char is lower case. Fix for 26529
34          */
35 //      public static boolean isLowerCase(char ch) {
36 //              return Character.toLowerCase(ch) == ch;
37 //      }
38
39         /**
40          * Line delimiter chars are '\n' and '\r'.
41          */
42         public static boolean isLineDelimiterChar(char ch) {
43                 return ch == '\n' || ch == '\r';
44         }
45
46 //      public static String removeNewLine(String message) {
47 //              StringBuffer result = new StringBuffer();
48 //              int current = 0;
49 //              int index = message.indexOf('\n', 0);
50 //              while (index != -1) {
51 //                      result.append(message.substring(current, index));
52 //                      if (current < index && index != 0)
53 //                              result.append(' ');
54 //                      current = index + 1;
55 //                      index = message.indexOf('\n', current);
56 //              }
57 //              result.append(message.substring(current));
58 //              return result.toString();
59 //      }
60
61         /**
62          * Converts the given string into an array of lines. The lines don't contain
63          * any line delimiter characters.
64          * 
65          * @return the string converted into an array of strings. Returns <code>
66          *      null</code>
67          *         if the input string can't be converted in an array of lines.
68          */
69 //      public static String[] convertIntoLines(String input) {
70 //              try {
71 //                      ILineTracker tracker = new DefaultLineTracker();
72 //                      tracker.set(input);
73 //                      int size = tracker.getNumberOfLines();
74 //                      String result[] = new String[size];
75 //                      for (int i = 0; i < size; i++) {
76 //                              IRegion region = tracker.getLineInformation(i);
77 //                              int offset = region.getOffset();
78 //                              result[i] = input
79 //                                              .substring(offset, offset + region.getLength());
80 //                      }
81 //                      return result;
82 //              } catch (BadLocationException e) {
83 //                      return null;
84 //              }
85 //      }
86
87         /**
88          * Returns <code>true</code> if the given string only consists of white
89          * spaces according to Java. If the string is empty, <code>true
90          * </code> is
91          * returned.
92          * 
93          * @return <code>true</code> if the string only consists of white spaces;
94          *         otherwise <code>false</code> is returned
95          * 
96          * @see java.lang.Character#isWhitespace(char)
97          */
98         public static boolean containsOnlyWhitespaces(String s) {
99                 int size = s.length();
100                 for (int i = 0; i < size; i++) {
101                         if (!Character.isWhitespace(s.charAt(i)))
102                                 return false;
103                 }
104                 return true;
105         }
106
107         /**
108          * Removes leading tabs and spaces from the given string. If the string
109          * doesn't contain any leading tabs or spaces then the string itself is
110          * returned.
111          */
112         public static String trimLeadingTabsAndSpaces(String line) {
113                 int size = line.length();
114                 int start = size;
115                 for (int i = 0; i < size; i++) {
116                         char c = line.charAt(i);
117                         if (!isIndentChar(c)) {
118                                 start = i;
119                                 break;
120                         }
121                 }
122                 if (start == 0)
123                         return line;
124                 else if (start == size)
125                         return ""; //$NON-NLS-1$
126                 else
127                         return line.substring(start);
128         }
129
130 //      public static String trimTrailingTabsAndSpaces(String line) {
131 //              int size = line.length();
132 //              int end = size;
133 //              for (int i = size - 1; i >= 0; i--) {
134 //                      char c = line.charAt(i);
135 //                      if (isIndentChar(c)) {
136 //                              end = i;
137 //                      } else {
138 //                              break;
139 //                      }
140 //              }
141 //              if (end == size)
142 //                      return line;
143 //              else if (end == 0)
144 //                      return ""; //$NON-NLS-1$
145 //              else
146 //                      return line.substring(0, end);
147 //      }
148
149         /**
150          * Returns the indent of the given string.
151          * 
152          * @param line
153          *            the text line
154          * @param tabWidth
155          *            the width of the '\t' character.
156          */
157         public static int computeIndent(String line, int tabWidth) {
158                 int result = 0;
159                 int blanks = 0;
160                 int size = line.length();
161                 for (int i = 0; i < size; i++) {
162                         char c = line.charAt(i);
163                         if (c == '\t') {
164                                 result++;
165                                 blanks = 0;
166                         } else if (isIndentChar(c)) {
167                                 blanks++;
168                                 if (blanks == tabWidth) {
169                                         result++;
170                                         blanks = 0;
171                                 }
172                         } else {
173                                 return result;
174                         }
175                 }
176                 return result;
177         }
178
179         /**
180          * Removes the given number of idents from the line. Asserts that the given
181          * line has the requested number of indents. If
182          * <code>indentsToRemove <= 0</code> the line is returned.
183          */
184         public static String trimIndent(String line, int indentsToRemove,
185                         int tabWidth) {
186                 if (line == null || indentsToRemove <= 0)
187                         return line;
188
189                 int start = 0;
190                 int indents = 0;
191                 int blanks = 0;
192                 int size = line.length();
193                 for (int i = 0; i < size; i++) {
194                         char c = line.charAt(i);
195                         if (c == '\t') {
196                                 indents++;
197                                 blanks = 0;
198                         } else if (isIndentChar(c)) {
199                                 blanks++;
200                                 if (blanks == tabWidth) {
201                                         indents++;
202                                         blanks = 0;
203                                 }
204                         } else {
205                                 // Assert.isTrue(false, "Line does not have requested number of
206                                 // indents"); //$NON-NLS-1$
207                                 start = i + 1;
208                                 break;
209                         }
210                         if (indents == indentsToRemove) {
211                                 start = i + 1;
212                                 break;
213                         }
214                 }
215                 if (start == size)
216                         return ""; //$NON-NLS-1$
217                 else
218                         return line.substring(start);
219         }
220
221         /**
222          * Removes all leading indents from the given line. If the line doesn't
223          * contain any indents the line itself is returned.
224          */
225 //      public static String trimIndents(String s, int tabWidth) {
226 //              int indent = computeIndent(s, tabWidth);
227 //              if (indent == 0)
228 //                      return s;
229 //              return trimIndent(s, indent, tabWidth);
230 //      }
231
232         /**
233          * Removes the common number of indents from all lines. If a line only
234          * consists out of white space it is ignored.
235          */
236 //      public static void trimIndentation(String[] lines, int tabWidth) {
237 //              trimIndentation(lines, tabWidth, true);
238 //      }
239
240         /**
241          * Removes the common number of indents from all lines. If a line only
242          * consists out of white space it is ignored. If <code>
243          * considerFirstLine</code>
244          * is false the first line will be ignored.
245          */
246 //      public static void trimIndentation(String[] lines, int tabWidth,
247 //                      boolean considerFirstLine) {
248 //              String[] toDo = new String[lines.length];
249 //              // find indentation common to all lines
250 //              int minIndent = Integer.MAX_VALUE; // very large
251 //              for (int i = considerFirstLine ? 0 : 1; i < lines.length; i++) {
252 //                      String line = lines[i];
253 //                      if (containsOnlyWhitespaces(line))
254 //                              continue;
255 //                      toDo[i] = line;
256 //                      int indent = computeIndent(line, tabWidth);
257 //                      if (indent < minIndent) {
258 //                              minIndent = indent;
259 //                      }
260 //              }
261 //
262 //              if (minIndent > 0) {
263 //                      // remove this indent from all lines
264 //                      for (int i = considerFirstLine ? 0 : 1; i < toDo.length; i++) {
265 //                              String s = toDo[i];
266 //                              if (s != null)
267 //                                      lines[i] = trimIndent(s, minIndent, tabWidth);
268 //                              else {
269 //                                      String line = lines[i];
270 //                                      int indent = computeIndent(line, tabWidth);
271 //                                      if (indent > minIndent)
272 //                                              lines[i] = trimIndent(line, minIndent, tabWidth);
273 //                                      else
274 //                                              lines[i] = trimLeadingTabsAndSpaces(line);
275 //                              }
276 //                      }
277 //              }
278 //      }
279
280 //      public static String getIndentString(String line, int tabWidth) {
281 //              int size = line.length();
282 //              int end = 0;
283 //              int blanks = 0;
284 //              for (int i = 0; i < size; i++) {
285 //                      char c = line.charAt(i);
286 //                      if (c == '\t') {
287 //                              end = i + 1;
288 //                              blanks = 0;
289 //                      } else if (isIndentChar(c)) {
290 //                              blanks++;
291 //                              if (blanks == tabWidth) {
292 //                                      end = i + 1;
293 //                                      blanks = 0;
294 //                              }
295 //                      } else {
296 //                              break;
297 //                      }
298 //              }
299 //              if (end == 0)
300 //                      return ""; //$NON-NLS-1$
301 //              else if (end == size)
302 //                      return line;
303 //              else
304 //                      return line.substring(0, end);
305 //      }
306
307 //      public static String[] removeTrailingEmptyLines(String[] sourceLines) {
308 //              int lastNonEmpty = findLastNonEmptyLineIndex(sourceLines);
309 //              String[] result = new String[lastNonEmpty + 1];
310 //              for (int i = 0; i < result.length; i++) {
311 //                      result[i] = sourceLines[i];
312 //              }
313 //              return result;
314 //      }
315
316 //      private static int findLastNonEmptyLineIndex(String[] sourceLines) {
317 //              for (int i = sourceLines.length - 1; i >= 0; i--) {
318 //                      if (!sourceLines[i].trim().equals(""))//$NON-NLS-1$
319 //                              return i;
320 //              }
321 //              return -1;
322 //      }
323
324         /**
325          * Change the indent of, possible muti-line, code range. The current indent
326          * is removed, a new indent added. The first line of the code will not be
327          * changed. (It is considered to have no indent as it might start in the
328          * middle of a line)
329          */
330         public static String changeIndent(String code, int codeIndentLevel,
331                         int tabWidth, String newIndent, String lineDelim) {
332                 try {
333                         ILineTracker tracker = new DefaultLineTracker();
334                         tracker.set(code);
335                         int nLines = tracker.getNumberOfLines();
336                         if (nLines == 1) {
337                                 return code;
338                         }
339
340                         StringBuffer buf = new StringBuffer();
341
342                         for (int i = 0; i < nLines; i++) {
343                                 IRegion region = tracker.getLineInformation(i);
344                                 int start = region.getOffset();
345                                 int end = start + region.getLength();
346                                 String line = code.substring(start, end);
347
348                                 if (i == 0) { // no indent for first line (contained in the
349                                                                 // formatted string)
350                                         buf.append(line);
351                                 } else { // no new line after last line
352                                         buf.append(lineDelim);
353                                         buf.append(newIndent);
354                                         buf.append(trimIndent(line, codeIndentLevel, tabWidth));
355                                 }
356                         }
357                         return buf.toString();
358                 } catch (BadLocationException e) {
359                         // can not happen
360                         return code;
361                 }
362         }
363
364         /**
365          * Concatenate the given strings into one strings using the passed line
366          * delimiter as a delimiter. No delimiter is added to the last line.
367          */
368 //      public static String concatenate(String[] lines, String delimiter) {
369 //              StringBuffer buffer = new StringBuffer();
370 //              for (int i = 0; i < lines.length; i++) {
371 //                      if (i > 0)
372 //                              buffer.append(delimiter);
373 //                      buffer.append(lines[i]);
374 //              }
375 //              return buffer.toString();
376 //      }
377
378         public static boolean equals(String s, char[] c) {
379                 if (s.length() != c.length)
380                         return false;
381
382                 for (int i = c.length; --i >= 0;)
383                         if (s.charAt(i) != c[i])
384                                 return false;
385                 return true;
386         }
387 }