2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.util;
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;
12 //import org.eclipse.jdt.internal.corext.Assert;
15 * Helper class to provide String manipulation functions not available in standard JDK.
17 public class Strings {
19 public static String removeNewLine(String message) {
20 StringBuffer result= new StringBuffer();
22 int index= message.indexOf('\n', 0);
24 result.append(message.substring(current, index));
25 if (current < index && index != 0)
28 index= message.indexOf('\n', current);
30 result.append(message.substring(current));
31 return result.toString();
35 * Converts the given string into an array of lines. The lines
36 * don't contain any line delimiter characters.
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.
41 public static String[] convertIntoLines(String input) {
43 ILineTracker tracker= new DefaultLineTracker();
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());
53 } catch (BadLocationException e) {
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.
63 * @return <code>true</code> if the string only consists of white
64 * spaces; otherwise <code>false</code> is returned
66 * @see java.lang.Character#isWhitespace(char)
68 public static boolean containsOnlyWhitespaces(String s) {
70 for (int i= 0; i < size; i++) {
71 if (!Character.isWhitespace(s.charAt(i)))
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
82 public static String trimLeadingTabsAndSpaces(String line) {
83 int size= line.length();
85 for (int i= 0; i < size; i++) {
86 char c= line.charAt(i);
87 if (c != '\t' && !Character.isSpaceChar(c)) {
94 else if (start == size)
95 return ""; //$NON-NLS-1$
97 return line.substring(start);
100 public static String trimTrailingTabsAndSpaces(String line) {
101 int size= line.length();
103 for (int i= size - 1; i >= 0; i--) {
104 char c= line.charAt(i);
105 if (c == '\t' || Character.isSpaceChar(c)) {
114 return ""; //$NON-NLS-1$
116 return line.substring(0, end);
120 * Returns the indent of the given string.
122 * @param line the text line
123 * @param tabWidth the width of the '\t' character.
125 public static int computeIndent(String line, int tabWidth) {
128 int size= line.length();
129 for (int i= 0; i < size; i++) {
130 char c= line.charAt(i);
134 } else if (Character.isSpaceChar(c)) {
136 if (blanks == tabWidth) {
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.
152 public static String trimIndent(String line, int indentsToRemove, int tabWidth) {
153 if (line == null || indentsToRemove <= 0)
159 int size= line.length();
160 for (int i= 0; i < size; i++) {
161 char c= line.charAt(i);
165 } else if (Character.isSpaceChar(c)) {
167 if (blanks == tabWidth) {
172 // Assert.isTrue(false, "Line does not have requested number of indents"); //$NON-NLS-1$
174 if (indents == indentsToRemove) {
180 return ""; //$NON-NLS-1$
182 return line.substring(start);
186 * Removes all leading indents from the given line. If the line doesn't contain
187 * any indents the line itself is returned.
189 public static String trimIndents(String s, int tabWidth) {
190 int indent= computeIndent(s, tabWidth);
193 return trimIndent(s, indent, tabWidth);
197 * Removes the common number of indents from all lines. If a line
198 * only consists out of white space it is ignored.
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))
209 int indent= computeIndent(line, tabWidth);
210 if (indent < minIndent) {
216 // remove this indent from all lines
217 for (int i= 0; i < toDo.length; i++) {
220 lines[i]= trimIndent(s, minIndent, tabWidth);
222 String line= lines[i];
223 int indent= computeIndent(line, tabWidth);
224 if (indent > minIndent)
225 lines[i]= trimIndent(line, minIndent, tabWidth);
227 lines[i]= trimLeadingTabsAndSpaces(line);
233 public static String getIndentString(String line, int tabWidth) {
234 int size= line.length();
237 for (int i= 0; i < size; i++) {
238 char c= line.charAt(i);
242 } else if (Character.isSpaceChar(c)) {
244 if (blanks == tabWidth) {
253 return ""; //$NON-NLS-1$
254 else if (end == size)
257 return line.substring(0, end + 1);