Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / corext / util / Strings.java
index ab80732..22052c4 100644 (file)
@@ -1,7 +1,13 @@
-/*
- * (c) Copyright IBM Corp. 2000, 2001.
- * All Rights Reserved.
- */
+/*******************************************************************************
+ * Copyright (c) 2000, 2003 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials 
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
 package net.sourceforge.phpdt.internal.corext.util;
 
 import org.eclipse.jface.text.BadLocationException;
@@ -9,12 +15,32 @@ import org.eclipse.jface.text.DefaultLineTracker;
 import org.eclipse.jface.text.ILineTracker;
 import org.eclipse.jface.text.IRegion;
 
-//import org.eclipse.jdt.internal.corext.Assert;
-
 /**
  * Helper class to provide String manipulation functions not available in standard JDK.
  */
 public class Strings {
+       
+       /**
+        * Indent char is a space char but not a line delimiters.
+        * <code>== Character.isWhitespace(ch) && ch != '\n' && ch != '\r'</code>
+        */
+       public static boolean isIndentChar(char ch) {
+               return Character.isWhitespace(ch) && !isLineDelimiterChar(ch);
+       }
+       
+       /**
+        * tests if a char is lower case. Fix for 26529 
+        */
+       public static boolean isLowerCase(char ch) {
+               return Character.toLowerCase(ch) == ch;
+       }       
+       
+       /**
+        * Line delimiter chars are  '\n' and '\r'.
+        */
+       public static boolean isLineDelimiterChar(char ch) {
+               return ch == '\n' || ch == '\r';
+       }       
 
        public static String removeNewLine(String message) {
                StringBuffer result= new StringBuffer();
@@ -84,7 +110,7 @@ public class Strings {
                int start= size;
                for (int i= 0; i < size; i++) {
                        char c= line.charAt(i);
-                       if (c != '\t' && !Character.isSpaceChar(c)) {
+                       if (!isIndentChar(c)) {
                                start= i;
                                break;
                        }
@@ -102,7 +128,7 @@ public class Strings {
                int end= size;
                for (int i= size - 1; i >= 0; i--) {
                        char c= line.charAt(i);
-                       if (c == '\t' || Character.isSpaceChar(c)) {
+                       if (isIndentChar(c)) {
                                end= i;
                        } else {
                                break;
@@ -131,7 +157,7 @@ public class Strings {
                        if (c == '\t') {
                                result++;
                                blanks= 0;
-                       } else if (Character.isSpaceChar(c)) {
+                       } else if (isIndentChar(c)) {
                                blanks++;
                                if (blanks == tabWidth) {
                                        result++;
@@ -162,14 +188,16 @@ public class Strings {
                        if (c == '\t') {
                                indents++;
                                blanks= 0;
-                       } else if (Character.isSpaceChar(c)) {
+                       } else if (isIndentChar(c)) {
                                        blanks++;
                                        if (blanks == tabWidth) {
                                                indents++;
                                                blanks= 0;
                                        }
                        } else {
-//                             Assert.isTrue(false, "Line does not have requested number of indents"); //$NON-NLS-1$
+                               // Assert.isTrue(false, "Line does not have requested number of indents"); //$NON-NLS-1$
+                               start= i + 1;
+                               break; 
                        }
                        if (indents == indentsToRemove) {
                                start= i + 1;
@@ -198,10 +226,19 @@ public class Strings {
         * only consists out of white space it is ignored.
         */
        public static void trimIndentation(String[] lines, int tabWidth) {
+               trimIndentation(lines, tabWidth, true);
+       }
+       
+       /**
+        * Removes the common number of indents from all lines. If a line
+        * only consists out of white space it is ignored. If <code>
+        * considerFirstLine</code> is false the first line will be ignored.
+        */
+       public static void trimIndentation(String[] lines, int tabWidth, boolean considerFirstLine) {
                String[] toDo= new String[lines.length];
                // find indentation common to all lines
                int minIndent= Integer.MAX_VALUE; // very large
-               for (int i= 0; i < lines.length; i++) {
+               for (int i= considerFirstLine ? 0 : 1; i < lines.length; i++) {
                        String line= lines[i];
                        if (containsOnlyWhitespaces(line))
                                continue;
@@ -214,7 +251,7 @@ public class Strings {
                
                if (minIndent > 0) {
                        // remove this indent from all lines
-                       for (int i= 0; i < toDo.length; i++) {
+                       for (int i= considerFirstLine ? 0 : 1; i < toDo.length; i++) {
                                String s= toDo[i];
                                if (s != null)
                                        lines[i]= trimIndent(s, minIndent, tabWidth);
@@ -237,12 +274,12 @@ public class Strings {
                for (int i= 0; i < size; i++) {
                        char c= line.charAt(i);
                        if (c == '\t') {
-                               end= i;
+                               end= i + 1;
                                blanks= 0;
-                       } else if (Character.isSpaceChar(c)) {
+                       } else if (isIndentChar(c)) {
                                blanks++;
                                if (blanks == tabWidth) {
-                                       end= i;
+                                       end= i + 1;
                                        blanks= 0;
                                }
                        } else {
@@ -254,7 +291,85 @@ public class Strings {
                else if (end == size)
                        return line;
                else
-                       return line.substring(0, end + 1);
+                       return line.substring(0, end);
+       }
+       
+       public static String[] removeTrailingEmptyLines(String[] sourceLines) {
+               int lastNonEmpty= findLastNonEmptyLineIndex(sourceLines);
+               String[] result= new String[lastNonEmpty + 1];
+               for (int i= 0; i < result.length; i++) {
+                       result[i]= sourceLines[i];
+               }
+               return result;
+       }
+
+       private static int findLastNonEmptyLineIndex(String[] sourceLines) {
+               for (int i= sourceLines.length - 1; i >= 0; i--) {
+                       if (! sourceLines[i].trim().equals(""))//$NON-NLS-1$
+                               return i;
+               }
+               return -1;
+       }
+       
+       /**
+        * Change the indent of, possible muti-line, code range. The current indent is removed, a new indent added.
+        * The first line of the code will not be changed. (It is considered to have no indent as it might start in
+        * the middle of a line)
+        */
+       public static String changeIndent(String code, int codeIndentLevel, int tabWidth, String newIndent, String lineDelim) {
+               try {
+                       ILineTracker tracker= new DefaultLineTracker();
+                       tracker.set(code);
+                       int nLines= tracker.getNumberOfLines();
+                       if (nLines == 1) {
+                               return code;
+                       }
+                       
+                       StringBuffer buf= new StringBuffer();
+                       
+                       for (int i= 0; i < nLines; i++) {
+                               IRegion region= tracker.getLineInformation(i);
+                               int start= region.getOffset();
+                               int end= start + region.getLength();
+                               String line= code.substring(start, end);
+                               
+                               if (i == 0) {  // no indent for first line (contained in the formatted string)
+                                       buf.append(line);
+                               } else { // no new line after last line
+                                       buf.append(lineDelim);
+                                       buf.append(newIndent); 
+                                       buf.append(trimIndent(line, codeIndentLevel, tabWidth));
+                               }
+                       }
+                       return buf.toString();
+               } catch (BadLocationException e) {
+                       // can not happen
+                       return code;
+               }
+       }       
+       
+       /**
+        * Concatenate the given strings into one strings using the passed line delimiter as a
+        * delimiter. No delimiter is added to the last line.
+        */
+       public static String concatenate(String[] lines, String delimiter) {
+               StringBuffer buffer= new StringBuffer();
+               for (int i= 0; i < lines.length; i++) {
+                       if (i > 0)
+                               buffer.append(delimiter);
+                       buffer.append(lines[i]);
+               }
+               return buffer.toString();
+       }
+       
+       public static boolean equals(String s, char[] c) {
+               if (s.length() != c.length)
+                       return false;
+
+               for (int i = c.length; --i >= 0;)
+                       if (s.charAt(i) != c[i])
+                               return false;
+               return true;
        }
 }