ab807321779336efad93e738bc48c563de449854
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / util / Strings.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.corext.util;
6
7 import org.eclipse.jface.text.BadLocationException;
8 import org.eclipse.jface.text.DefaultLineTracker;
9 import org.eclipse.jface.text.ILineTracker;
10 import org.eclipse.jface.text.IRegion;
11
12 //import org.eclipse.jdt.internal.corext.Assert;
13
14 /**
15  * Helper class to provide String manipulation functions not available in standard JDK.
16  */
17 public class Strings {
18
19         public static String removeNewLine(String message) {
20                 StringBuffer result= new StringBuffer();
21                 int current= 0;
22                 int index= message.indexOf('\n', 0);
23                 while (index != -1) {
24                         result.append(message.substring(current, index));
25                         if (current < index && index != 0)
26                                 result.append(' ');
27                         current= index + 1;
28                         index= message.indexOf('\n', current);
29                 }
30                 result.append(message.substring(current));
31                 return result.toString();
32         }
33
34         /**
35          * Converts the given string into an array of lines. The lines 
36          * don't contain any line delimiter characters.
37          *
38          * @return the string converted into an array of strings. Returns <code>
39          *      null</code> if the input string can't be converted in an array of lines.
40          */
41         public static String[] convertIntoLines(String input) {
42                 try {
43                         ILineTracker tracker= new DefaultLineTracker();
44                         tracker.set(input);
45                         int size= tracker.getNumberOfLines();
46                         String result[]= new String[size];
47                         for (int i= 0; i < size; i++) {
48                                 IRegion region= tracker.getLineInformation(i);
49                                 int offset= region.getOffset();
50                                 result[i]= input.substring(offset, offset + region.getLength());
51                         }
52                         return result;
53                 } catch (BadLocationException e) {
54                         return null;
55                 }
56         }
57
58         /**
59          * Returns <code>true</code> if the given string only consists of
60          * white spaces according to Java. If the string is empty, <code>true
61          * </code> is returned.
62          * 
63          * @return <code>true</code> if the string only consists of white
64          *      spaces; otherwise <code>false</code> is returned
65          * 
66          * @see java.lang.Character#isWhitespace(char)
67          */
68         public static boolean containsOnlyWhitespaces(String s) {
69                 int size= s.length();
70                 for (int i= 0; i < size; i++) {
71                         if (!Character.isWhitespace(s.charAt(i)))
72                                 return false;
73                 }
74                 return true;
75         }
76         
77         /**
78          * Removes leading tabs and spaces from the given string. If the string
79          * doesn't contain any leading tabs or spaces then the string itself is 
80          * returned.
81          */
82         public static String trimLeadingTabsAndSpaces(String line) {
83                 int size= line.length();
84                 int start= size;
85                 for (int i= 0; i < size; i++) {
86                         char c= line.charAt(i);
87                         if (c != '\t' && !Character.isSpaceChar(c)) {
88                                 start= i;
89                                 break;
90                         }
91                 }
92                 if (start == 0)
93                         return line;
94                 else if (start == size)
95                         return ""; //$NON-NLS-1$
96                 else
97                         return line.substring(start);
98         }
99         
100         public static String trimTrailingTabsAndSpaces(String line) {
101                 int size= line.length();
102                 int end= size;
103                 for (int i= size - 1; i >= 0; i--) {
104                         char c= line.charAt(i);
105                         if (c == '\t' || Character.isSpaceChar(c)) {
106                                 end= i;
107                         } else {
108                                 break;
109                         }
110                 }
111                 if (end == size)
112                         return line;
113                 else if (end == 0)
114                         return ""; //$NON-NLS-1$
115                 else
116                         return line.substring(0, end);
117         }
118         
119         /**
120          * Returns the indent of the given string.
121          * 
122          * @param line the text line
123          * @param tabWidth the width of the '\t' character.
124          */
125         public static int computeIndent(String line, int tabWidth) {
126                 int result= 0;
127                 int blanks= 0;
128                 int size= line.length();
129                 for (int i= 0; i < size; i++) {
130                         char c= line.charAt(i);
131                         if (c == '\t') {
132                                 result++;
133                                 blanks= 0;
134                         } else if (Character.isSpaceChar(c)) {
135                                 blanks++;
136                                 if (blanks == tabWidth) {
137                                         result++;
138                                         blanks= 0;
139                                 }
140                         } else {
141                                 return result;
142                         }
143                 }
144                 return result;
145         }
146         
147         /**
148          * Removes the given number of idents from the line. Asserts that the given line 
149          * has the requested number of indents. If <code>indentsToRemove <= 0</code>
150          * the line is returned.
151          */
152         public static String trimIndent(String line, int indentsToRemove, int tabWidth) {
153                 if (line == null || indentsToRemove <= 0)
154                         return line;
155                         
156                 int start= 0;
157                 int indents= 0;
158                 int blanks= 0;
159                 int size= line.length();
160                 for (int i= 0; i < size; i++) {
161                         char c= line.charAt(i);
162                         if (c == '\t') {
163                                 indents++;
164                                 blanks= 0;
165                         } else if (Character.isSpaceChar(c)) {
166                                         blanks++;
167                                         if (blanks == tabWidth) {
168                                                 indents++;
169                                                 blanks= 0;
170                                         }
171                         } else {
172 //                              Assert.isTrue(false, "Line does not have requested number of indents"); //$NON-NLS-1$
173                         }
174                         if (indents == indentsToRemove) {
175                                 start= i + 1;
176                                 break;
177                         }       
178                 }
179                 if (start == size)
180                         return ""; //$NON-NLS-1$
181                 else
182                         return line.substring(start);
183         }
184         
185         /**
186          * Removes all leading indents from the given line. If the line doesn't contain
187          * any indents the line itself is returned.
188          */
189         public static String trimIndents(String s, int tabWidth) {
190                 int indent= computeIndent(s, tabWidth);
191                 if (indent == 0)
192                         return s;
193                 return trimIndent(s, indent, tabWidth);
194         }
195         
196         /**
197          * Removes the common number of indents from all lines. If a line
198          * only consists out of white space it is ignored.
199          */
200         public static void trimIndentation(String[] lines, int tabWidth) {
201                 String[] toDo= new String[lines.length];
202                 // find indentation common to all lines
203                 int minIndent= Integer.MAX_VALUE; // very large
204                 for (int i= 0; i < lines.length; i++) {
205                         String line= lines[i];
206                         if (containsOnlyWhitespaces(line))
207                                 continue;
208                         toDo[i]= line;
209                         int indent= computeIndent(line, tabWidth);
210                         if (indent < minIndent) {
211                                 minIndent= indent;
212                         }
213                 }
214                 
215                 if (minIndent > 0) {
216                         // remove this indent from all lines
217                         for (int i= 0; i < toDo.length; i++) {
218                                 String s= toDo[i];
219                                 if (s != null)
220                                         lines[i]= trimIndent(s, minIndent, tabWidth);
221                                 else {
222                                         String line= lines[i];
223                                         int indent= computeIndent(line, tabWidth);
224                                         if (indent > minIndent)
225                                                 lines[i]= trimIndent(line, minIndent, tabWidth);
226                                         else
227                                                 lines[i]= trimLeadingTabsAndSpaces(line);
228                                 }
229                         }
230                 }
231         }
232         
233         public static String getIndentString(String line, int tabWidth) {
234                 int size= line.length();
235                 int end= 0;
236                 int blanks= 0;
237                 for (int i= 0; i < size; i++) {
238                         char c= line.charAt(i);
239                         if (c == '\t') {
240                                 end= i;
241                                 blanks= 0;
242                         } else if (Character.isSpaceChar(c)) {
243                                 blanks++;
244                                 if (blanks == tabWidth) {
245                                         end= i;
246                                         blanks= 0;
247                                 }
248                         } else {
249                                 break;
250                         }
251                 }
252                 if (end == 0)
253                         return ""; //$NON-NLS-1$
254                 else if (end == size)
255                         return line;
256                 else
257                         return line.substring(0, end + 1);
258         }
259 }
260