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
22 public class Strings {
25 * Indent char is a space char but not a line delimiters.
26 * <code>== Character.isWhitespace(ch) && ch != '\n' && ch != '\r'</code>
28 public static boolean isIndentChar(char ch) {
29 return Character.isWhitespace(ch) && !isLineDelimiterChar(ch);
33 * tests if a char is lower case. Fix for 26529
35 public static boolean isLowerCase(char ch) {
36 return Character.toLowerCase(ch) == ch;
40 * Line delimiter chars are '\n' and '\r'.
42 public static boolean isLineDelimiterChar(char ch) {
43 return ch == '\n' || ch == '\r';
46 public static String removeNewLine(String message) {
47 StringBuffer result = new StringBuffer();
49 int index = message.indexOf('\n', 0);
51 result.append(message.substring(current, index));
52 if (current < index && index != 0)
55 index = message.indexOf('\n', current);
57 result.append(message.substring(current));
58 return result.toString();
62 * Converts the given string into an array of lines. The lines don't contain
63 * any line delimiter characters.
65 * @return the string converted into an array of strings. Returns <code>
67 * if the input string can't be converted in an array of lines.
69 public static String[] convertIntoLines(String input) {
71 ILineTracker tracker = new DefaultLineTracker();
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();
79 .substring(offset, offset + region.getLength());
82 } catch (BadLocationException e) {
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
93 * @return <code>true</code> if the string only consists of white spaces;
94 * otherwise <code>false</code> is returned
96 * @see java.lang.Character#isWhitespace(char)
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)))
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
112 public static String trimLeadingTabsAndSpaces(String line) {
113 int size = line.length();
115 for (int i = 0; i < size; i++) {
116 char c = line.charAt(i);
117 if (!isIndentChar(c)) {
124 else if (start == size)
125 return ""; //$NON-NLS-1$
127 return line.substring(start);
130 public static String trimTrailingTabsAndSpaces(String line) {
131 int size = line.length();
133 for (int i = size - 1; i >= 0; i--) {
134 char c = line.charAt(i);
135 if (isIndentChar(c)) {
144 return ""; //$NON-NLS-1$
146 return line.substring(0, end);
150 * Returns the indent of the given string.
155 * the width of the '\t' character.
157 public static int computeIndent(String line, int tabWidth) {
160 int size = line.length();
161 for (int i = 0; i < size; i++) {
162 char c = line.charAt(i);
166 } else if (isIndentChar(c)) {
168 if (blanks == tabWidth) {
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.
184 public static String trimIndent(String line, int indentsToRemove,
186 if (line == null || indentsToRemove <= 0)
192 int size = line.length();
193 for (int i = 0; i < size; i++) {
194 char c = line.charAt(i);
198 } else if (isIndentChar(c)) {
200 if (blanks == tabWidth) {
205 // Assert.isTrue(false, "Line does not have requested number of
206 // indents"); //$NON-NLS-1$
210 if (indents == indentsToRemove) {
216 return ""; //$NON-NLS-1$
218 return line.substring(start);
222 * Removes all leading indents from the given line. If the line doesn't
223 * contain any indents the line itself is returned.
225 public static String trimIndents(String s, int tabWidth) {
226 int indent = computeIndent(s, tabWidth);
229 return trimIndent(s, indent, tabWidth);
233 * Removes the common number of indents from all lines. If a line only
234 * consists out of white space it is ignored.
236 public static void trimIndentation(String[] lines, int tabWidth) {
237 trimIndentation(lines, tabWidth, true);
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.
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))
256 int indent = computeIndent(line, tabWidth);
257 if (indent < minIndent) {
263 // remove this indent from all lines
264 for (int i = considerFirstLine ? 0 : 1; i < toDo.length; i++) {
267 lines[i] = trimIndent(s, minIndent, tabWidth);
269 String line = lines[i];
270 int indent = computeIndent(line, tabWidth);
271 if (indent > minIndent)
272 lines[i] = trimIndent(line, minIndent, tabWidth);
274 lines[i] = trimLeadingTabsAndSpaces(line);
280 public static String getIndentString(String line, int tabWidth) {
281 int size = line.length();
284 for (int i = 0; i < size; i++) {
285 char c = line.charAt(i);
289 } else if (isIndentChar(c)) {
291 if (blanks == tabWidth) {
300 return ""; //$NON-NLS-1$
301 else if (end == size)
304 return line.substring(0, end);
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];
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$
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
330 public static String changeIndent(String code, int codeIndentLevel,
331 int tabWidth, String newIndent, String lineDelim) {
333 ILineTracker tracker = new DefaultLineTracker();
335 int nLines = tracker.getNumberOfLines();
340 StringBuffer buf = new StringBuffer();
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);
348 if (i == 0) { // no indent for first line (contained in the
351 } else { // no new line after last line
352 buf.append(lineDelim);
353 buf.append(newIndent);
354 buf.append(trimIndent(line, codeIndentLevel, tabWidth));
357 return buf.toString();
358 } catch (BadLocationException e) {
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.
368 public static String concatenate(String[] lines, String delimiter) {
369 StringBuffer buffer = new StringBuffer();
370 for (int i = 0; i < lines.length; i++) {
372 buffer.append(delimiter);
373 buffer.append(lines[i]);
375 return buffer.toString();
378 public static boolean equals(String s, char[] c) {
379 if (s.length() != c.length)
382 for (int i = c.length; --i >= 0;)
383 if (s.charAt(i) != c[i])