1 11 package org.eclipse.jdt.internal.corext.util; 12 13 import org.eclipse.core.runtime.Assert; 14 15 import org.eclipse.jface.action.LegacyActionTools; 16 17 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.DefaultLineTracker; 19 import org.eclipse.jface.text.ILineTracker; 20 import org.eclipse.jface.text.IRegion; 21 22 import org.eclipse.jdt.core.IJavaProject; 23 import org.eclipse.jdt.core.formatter.IndentManipulation; 24 25 26 29 public class Strings { 30 31 private Strings(){} 32 33 36 public static boolean isLowerCase(char ch) { 37 return Character.toLowerCase(ch) == ch; 38 } 39 40 public static boolean startsWithIgnoreCase(String text, String prefix) { 41 int textLength= text.length(); 42 int prefixLength= prefix.length(); 43 if (textLength < prefixLength) 44 return false; 45 for (int i= prefixLength - 1; i >= 0; i--) { 46 if (Character.toLowerCase(prefix.charAt(i)) != Character.toLowerCase(text.charAt(i))) 47 return false; 48 } 49 return true; 50 } 51 52 public static String removeNewLine(String message) { 53 StringBuffer result= new StringBuffer (); 54 int current= 0; 55 int index= message.indexOf('\n', 0); 56 while (index != -1) { 57 result.append(message.substring(current, index)); 58 if (current < index && index != 0) 59 result.append(' '); 60 current= index + 1; 61 index= message.indexOf('\n', current); 62 } 63 result.append(message.substring(current)); 64 return result.toString(); 65 } 66 67 74 public static String [] convertIntoLines(String input) { 75 try { 76 ILineTracker tracker= new DefaultLineTracker(); 77 tracker.set(input); 78 int size= tracker.getNumberOfLines(); 79 String result[]= new String [size]; 80 for (int i= 0; i < size; i++) { 81 IRegion region= tracker.getLineInformation(i); 82 int offset= region.getOffset(); 83 result[i]= input.substring(offset, offset + region.getLength()); 84 } 85 return result; 86 } catch (BadLocationException e) { 87 return null; 88 } 89 } 90 91 101 public static boolean containsOnlyWhitespaces(String s) { 102 int size= s.length(); 103 for (int i= 0; i < size; i++) { 104 if (!Character.isWhitespace(s.charAt(i))) 105 return false; 106 } 107 return true; 108 } 109 110 115 public static String trimLeadingTabsAndSpaces(String line) { 116 int size= line.length(); 117 int start= size; 118 for (int i= 0; i < size; i++) { 119 char c= line.charAt(i); 120 if (!IndentManipulation.isIndentChar(c)) { 121 start= i; 122 break; 123 } 124 } 125 if (start == 0) 126 return line; 127 else if (start == size) 128 return ""; else 130 return line.substring(start); 131 } 132 133 public static String trimTrailingTabsAndSpaces(String line) { 134 int size= line.length(); 135 int end= size; 136 for (int i= size - 1; i >= 0; i--) { 137 char c= line.charAt(i); 138 if (IndentManipulation.isIndentChar(c)) { 139 end= i; 140 } else { 141 break; 142 } 143 } 144 if (end == size) 145 return line; 146 else if (end == 0) 147 return ""; else 149 return line.substring(0, end); 150 } 151 152 161 public static int computeIndentUnits(String line, IJavaProject project) { 162 return IndentManipulation.measureIndentUnits(line, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project)); 163 } 164 165 174 public static int computeIndentUnits(String line, int tabWidth, int indentWidth) { 175 return IndentManipulation.measureIndentUnits(line, tabWidth, indentWidth); 176 } 177 178 188 public static int measureIndentLength(CharSequence line, int tabSize) { 189 return IndentManipulation.measureIndentInSpaces(line, tabSize); 190 } 191 192 201 public static String trimIndent(String line, int indentsToRemove, IJavaProject project) { 202 return IndentManipulation.trimIndent(line, indentsToRemove, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project)); 203 } 204 205 212 public static String trimIndent(String line, int indentsToRemove, int tabWidth, int indentWidth) { 213 return IndentManipulation.trimIndent(line, indentsToRemove, tabWidth, indentWidth); 214 } 215 216 224 public static void trimIndentation(String [] lines, IJavaProject project) { 225 trimIndentation(lines, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project), true); 226 } 227 233 public static void trimIndentation(String [] lines, int tabWidth, int indentWidth) { 234 trimIndentation(lines, tabWidth, indentWidth, true); 235 } 236 237 246 public static void trimIndentation(String [] lines, IJavaProject project, boolean considerFirstLine) { 247 trimIndentation(lines, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project), considerFirstLine); 248 } 249 250 256 public static void trimIndentation(String [] lines, int tabWidth, int indentWidth, boolean considerFirstLine) { 257 String [] toDo= new String [lines.length]; 258 int minIndent= Integer.MAX_VALUE; for (int i= considerFirstLine ? 0 : 1; i < lines.length; i++) { 261 String line= lines[i]; 262 if (containsOnlyWhitespaces(line)) 263 continue; 264 toDo[i]= line; 265 int indent= computeIndentUnits(line, tabWidth, indentWidth); 266 if (indent < minIndent) { 267 minIndent= indent; 268 } 269 } 270 271 if (minIndent > 0) { 272 for (int i= considerFirstLine ? 0 : 1; i < toDo.length; i++) { 274 String s= toDo[i]; 275 if (s != null) 276 lines[i]= trimIndent(s, minIndent, tabWidth, indentWidth); 277 else { 278 String line= lines[i]; 279 int indent= computeIndentUnits(line, tabWidth, indentWidth); 280 if (indent > minIndent) 281 lines[i]= trimIndent(line, minIndent, tabWidth, indentWidth); 282 else 283 lines[i]= trimLeadingTabsAndSpaces(line); 284 } 285 } 286 } 287 } 288 289 299 public static String getIndentString(String line, IJavaProject project) { 300 return IndentManipulation.extractIndentString(line, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project)); 301 } 302 303 313 public static String getIndentString(String line, int tabWidth, int indentWidth) { 314 return IndentManipulation.extractIndentString(line, tabWidth, indentWidth); 315 } 316 317 public static String [] removeTrailingEmptyLines(String [] sourceLines) { 318 int lastNonEmpty= findLastNonEmptyLineIndex(sourceLines); 319 String [] result= new String [lastNonEmpty + 1]; 320 for (int i= 0; i < result.length; i++) { 321 result[i]= sourceLines[i]; 322 } 323 return result; 324 } 325 326 private static int findLastNonEmptyLineIndex(String [] sourceLines) { 327 for (int i= sourceLines.length - 1; i >= 0; i--) { 328 if (! sourceLines[i].trim().equals("")) return i; 330 } 331 return -1; 332 } 333 334 343 public static String changeIndent(String code, int codeIndentLevel, IJavaProject project, String newIndent, String lineDelim) { 344 return IndentManipulation.changeIndent(code, codeIndentLevel, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project), newIndent, lineDelim); 345 } 346 347 353 public static String changeIndent(String code, int codeIndentLevel, int tabWidth, int indentWidth, String newIndent, String lineDelim) { 354 return IndentManipulation.changeIndent(code, codeIndentLevel, tabWidth, indentWidth, newIndent, lineDelim); 355 } 356 357 public static String trimIndentation(String source, IJavaProject project, boolean considerFirstLine) { 358 return trimIndentation(source, CodeFormatterUtil.getTabWidth(project), CodeFormatterUtil.getIndentWidth(project), considerFirstLine); 359 } 360 361 public static String trimIndentation(String source, int tabWidth, int indentWidth, boolean considerFirstLine) { 362 try { 363 ILineTracker tracker= new DefaultLineTracker(); 364 tracker.set(source); 365 int size= tracker.getNumberOfLines(); 366 if (size == 1) 367 return source; 368 String lines[]= new String [size]; 369 for (int i= 0; i < size; i++) { 370 IRegion region= tracker.getLineInformation(i); 371 int offset= region.getOffset(); 372 lines[i]= source.substring(offset, offset + region.getLength()); 373 } 374 Strings.trimIndentation(lines, tabWidth, indentWidth, considerFirstLine); 375 StringBuffer result= new StringBuffer (); 376 int last= size - 1; 377 for (int i= 0; i < size; i++) { 378 result.append(lines[i]); 379 if (i < last) 380 result.append(tracker.getLineDelimiter(i)); 381 } 382 return result.toString(); 383 } catch (BadLocationException e) { 384 Assert.isTrue(false,"Can not happend"); return null; 386 } 387 } 388 389 390 394 public static String concatenate(String [] lines, String delimiter) { 395 StringBuffer buffer= new StringBuffer (); 396 for (int i= 0; i < lines.length; i++) { 397 if (i > 0) 398 buffer.append(delimiter); 399 buffer.append(lines[i]); 400 } 401 return buffer.toString(); 402 } 403 404 public static boolean equals(String s, char[] c) { 405 if (s.length() != c.length) 406 return false; 407 408 for (int i = c.length; --i >= 0;) 409 if (s.charAt(i) != c[i]) 410 return false; 411 return true; 412 } 413 414 public static String removeTrailingCharacters(String text, char toRemove) { 415 int size= text.length(); 416 int end= size; 417 for (int i= size - 1; i >= 0; i--) { 418 char c= text.charAt(i); 419 if (c == toRemove) { 420 end= i; 421 } else { 422 break; 423 } 424 } 425 if (end == size) 426 return text; 427 else if (end == 0) 428 return ""; else 430 return text.substring(0, end); 431 } 432 433 public static String removeMnemonicIndicator(String string) { 434 return LegacyActionTools.removeMnemonics(string); 435 } 436 } 437 | Popular Tags |