Here is a patch against the latest revision of StringUtils

john mcnally

Index: src/java/org/apache/velocity/util/StringUtils.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-velocity/src/java/org/apache/velocity/util/StringUtils.java,v
retrieving revision 1.11
diff -u -r1.11 StringUtils.java
--- src/java/org/apache/velocity/util/StringUtils.java  2001/05/02 00:06:31     
1.11
+++ src/java/org/apache/velocity/util/StringUtils.java  2001/05/09 01:32:29
@@ -173,9 +173,9 @@
 
     /**
      * Chop i characters off the end of a string.
-     * This method is sensitive to the EOL for a
-     * particular platform. The EOL character will
-     * considered a single character.
+     * This method assumes that any EOL characters in String s 
+     * and the platform EOL will be the same.
+     * A 2 character EOL will count as 1 character. 
      *
      * @param string String to chop.
      * @param i Number of characters to chop.
@@ -183,15 +183,45 @@
      */
     public static String chop(String s, int i)
     {
-        for (int j = 0; j < i; j++)
+        return chop(s, i, EOL);
+    }
+
+    /**
+     * Chop i characters off the end of a string. 
+     * A 2 character EOL will count as 1 character. 
+     *
+     * @param string String to chop.
+     * @param i Number of characters to chop.
+     * @param eol A String representing the EOL (end of line).
+     * @return String with processed answer.
+     */
+    public static String chop(String s, int i, String eol)
+    {        
+        char[] sa = s.toCharArray();
+        int length = sa.length;
+
+        if ( eol.length() == 2 ) 
+        {
+            char eol1 = eol.charAt(0);
+            char eol2 = eol.charAt(1);
+            for (; i>0; i--)
+            {
+                if ( sa[length-1] == eol2 && sa[length-2] == eol1 ) 
+                {
+                    length -= 2;
+                }
+                else 
+                {
+                    length--;
+                }
+            }
+        }
+        else
         {
-            if (EOL.indexOf(s.substring(s.length() - 1)) > 0 && EOL_LENGTH == 2)
-                s = s.substring(0, s.length() - 2);
-            else
-                s = s.substring(0, s.length() - 1);
-        }            
-    
-        return s;
+            length -= i;
+        }
+
+        return new String(sa, 0, length);
     }

Reply via email to