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