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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.util;
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;
19 * Helper class to provide String manipulation functions not available in standard JDK.
21 public class Strings {
24 * Indent char is a space char but not a line delimiters.
25 * <code>== Character.isWhitespace(ch) && ch != '\n' && ch != '\r'</code>
27 public static boolean isIndentChar(char ch) {
28 return Character.isWhitespace(ch) && !isLineDelimiterChar(ch);
32 * tests if a char is lower case. Fix for 26529
34 public static boolean isLowerCase(char ch) {
35 return Character.toLowerCase(ch) == ch;
39 * Line delimiter chars are '\n' and '\r'.
41 public static boolean isLineDelimiterChar(char ch) {
42 return ch == '\n' || ch == '\r';
45 public static String removeNewLine(String message) {
46 StringBuffer result= new StringBuffer();
48 int index= message.indexOf('\n', 0);
50 result.append(message.substring(current, index));
51 if (current < index && index != 0)
54 index= message.indexOf('\n', current);
56 result.append(message.substring(current));
57 return result.toString();
61 * Converts the given string into an array of lines. The lines
62 * don't contain any line delimiter characters.
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.
67 public static String[] convertIntoLines(String input) {
69 ILineTracker tracker= new DefaultLineTracker();
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());
79 } catch (BadLocationException e) {
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.
89 * @return <code>true</code> if the string only consists of white
90 * spaces; otherwise <code>false</code> is returned
92 * @see java.lang.Character#isWhitespace(char)
94 public static boolean containsOnlyWhitespaces(String s) {
96 for (int i= 0; i < size; i++) {
97 if (!Character.isWhitespace(s.charAt(i)))
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
108 public static String trimLeadingTabsAndSpaces(String line) {
109 int size= line.length();
111 for (int i= 0; i < size; i++) {
112 char c= line.charAt(i);
113 if (!isIndentChar(c)) {
120 else if (start == size)
121 return ""; //$NON-NLS-1$
123 return line.substring(start);
126 public static String trimTrailingTabsAndSpaces(String line) {
127 int size= line.length();
129 for (int i= size - 1; i >= 0; i--) {
130 char c= line.charAt(i);
131 if (isIndentChar(c)) {
140 return ""; //$NON-NLS-1$
142 return line.substring(0, end);
146 * Returns the indent of the given string.
148 * @param line the text line
149 * @param tabWidth the width of the '\t' character.
151 public static int computeIndent(String line, int tabWidth) {
154 int size= line.length();
155 for (int i= 0; i < size; i++) {
156 char c= line.charAt(i);
160 } else if (isIndentChar(c)) {
162 if (blanks == tabWidth) {
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.
178 public static String trimIndent(String line, int indentsToRemove, int tabWidth) {
179 if (line == null || indentsToRemove <= 0)
185 int size= line.length();
186 for (int i= 0; i < size; i++) {
187 char c= line.charAt(i);
191 } else if (isIndentChar(c)) {
193 if (blanks == tabWidth) {
198 // Assert.isTrue(false, "Line does not have requested number of indents"); //$NON-NLS-1$
202 if (indents == indentsToRemove) {
208 return ""; //$NON-NLS-1$
210 return line.substring(start);
214 * Removes all leading indents from the given line. If the line doesn't contain
215 * any indents the line itself is returned.
217 public static String trimIndents(String s, int tabWidth) {
218 int indent= computeIndent(s, tabWidth);
221 return trimIndent(s, indent, tabWidth);
225 * Removes the common number of indents from all lines. If a line
226 * only consists out of white space it is ignored.
228 public static void trimIndentation(String[] lines, int tabWidth) {
229 trimIndentation(lines, tabWidth, true);
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.
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))
246 int indent= computeIndent(line, tabWidth);
247 if (indent < minIndent) {
253 // remove this indent from all lines
254 for (int i= considerFirstLine ? 0 : 1; i < toDo.length; i++) {
257 lines[i]= trimIndent(s, minIndent, tabWidth);
259 String line= lines[i];
260 int indent= computeIndent(line, tabWidth);
261 if (indent > minIndent)
262 lines[i]= trimIndent(line, minIndent, tabWidth);
264 lines[i]= trimLeadingTabsAndSpaces(line);
270 public static String getIndentString(String line, int tabWidth) {
271 int size= line.length();
274 for (int i= 0; i < size; i++) {
275 char c= line.charAt(i);
279 } else if (isIndentChar(c)) {
281 if (blanks == tabWidth) {
290 return ""; //$NON-NLS-1$
291 else if (end == size)
294 return line.substring(0, end);
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];
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$
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)
319 public static String changeIndent(String code, int codeIndentLevel, int tabWidth, String newIndent, String lineDelim) {
321 ILineTracker tracker= new DefaultLineTracker();
323 int nLines= tracker.getNumberOfLines();
328 StringBuffer buf= new StringBuffer();
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);
336 if (i == 0) { // no indent for first line (contained in the formatted string)
338 } else { // no new line after last line
339 buf.append(lineDelim);
340 buf.append(newIndent);
341 buf.append(trimIndent(line, codeIndentLevel, tabWidth));
344 return buf.toString();
345 } catch (BadLocationException e) {
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.
355 public static String concatenate(String[] lines, String delimiter) {
356 StringBuffer buffer= new StringBuffer();
357 for (int i= 0; i < lines.length; i++) {
359 buffer.append(delimiter);
360 buffer.append(lines[i]);
362 return buffer.toString();
365 public static boolean equals(String s, char[] c) {
366 if (s.length() != c.length)
369 for (int i = c.length; --i >= 0;)
370 if (s.charAt(i) != c[i])