You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/09/19 01:13:05 UTC

svn commit: r290000 - /directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/StringTools.java

Author: elecharny
Date: Sun Sep 18 16:13:00 2005
New Revision: 290000

URL: http://svn.apache.org/viewcvs?rev=290000&view=rev
Log:
Improved the trimConsecutiveToOne and deepTrim functions speed : up to x4.5 for the first one, up to x15 for the second 
(depending of course on the input size)

Modified:
    directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/StringTools.java

Modified: directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/StringTools.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/StringTools.java?rev=290000&r1=289999&r2=290000&view=diff
==============================================================================
--- directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/StringTools.java (original)
+++ directory/shared/ldap/trunk/common/src/java/org/apache/ldap/common/util/StringTools.java Sun Sep 18 16:13:00 2005
@@ -37,7 +37,7 @@
 public class StringTools
 {
     /**
-     * Trims several consecutive characters into one.
+     * Trims several consecutive characters into one. 
      *
      * @param str the string to trim consecutive characters of
      * @param ch the character to trim down
@@ -45,21 +45,35 @@
      */
     public static String trimConsecutiveToOne( String str, char ch )
     {
-        StringBuffer buf = new StringBuffer();
-        buf.append( str );
+        char[] buffer = str.toCharArray();
+        char[] newbuf = new char[buffer.length];
+        int pos = 0;
+        boolean same = false;
 
-        for ( int ii = 0; ii < buf.length(); ii++ )
+        for ( int i = 0; i < buffer.length; i++ )
         {
-            if ( buf.charAt( ii ) == ch )
+            char car = buffer[i];
+            
+            if ( car == ch )
             {
-                while ( ii+1 < buf.length() && buf.charAt( ii + 1 ) == ch )
+                if ( same )
                 {
-                    buf.deleteCharAt( ii + 1 );
+                    continue;
                 }
+                else
+                { 
+                    same = true;
+                    newbuf[pos++] = car ;
+                }
+            }
+            else
+            {
+                same = false;
+                newbuf[pos++] = car ;
             }
         }
 
-        return buf.toString();
+        return new String(newbuf, 0, pos);
     }
 
 
@@ -70,12 +84,12 @@
      * down to a single space to perserve the whitespace separated tokenization
      * order of the String.
      *
-     * @param a_string the string to deep trim.
+     * @param string the string to deep trim.
      * @return the trimmed string.
      */
-    public static String deepTrim( String a_string )
+    public static String deepTrim( String string )
     {
-        return deepTrim( a_string, false ) ;
+        return deepTrim( string, false ) ;
     }
 
 
@@ -87,9 +101,9 @@
      * 
      * @see StringTools#deepTrim( String )
      */
-    public static String deepTrimToLower( String a_string )
+    public static String deepTrimToLower( String string )
     {
-        return deepTrim( a_string, true ) ;
+        return deepTrim( string, true ) ;
     }
 
 
@@ -109,16 +123,22 @@
         }
 
         char ch ;
-        StringBuffer buf = new StringBuffer() ;
+        char[] buf = str.toCharArray();
+        char[] newbuf = new char[buf.length];
+        boolean wsSeen = true;
+        int pos = 0;
 
-        for ( int ii = 0; ii < str.length(); ii++ )
+        for ( int i = 0; i < str.length(); i++ )
         {
-            ch = str.charAt( ii ) ;
+            ch = buf[i] ;
 
             // filter out all uppercase characters
-            if ( Character.isUpperCase( ( char ) ch ) && toLowerCase )
+            if ( toLowerCase)
             {
-                ch = Character.toLowerCase( ( char ) ch ) ;
+                if ( Character.isUpperCase( ch ) ) 
+                {
+                    ch = Character.toLowerCase( ch ) ;
+                }
             }
 
             // Check to see if we should add space
@@ -127,29 +147,25 @@
                 // If the buffer has had characters added already check last
                 // added character.  Only append a spc if last character was
                 // not whitespace.
-                if ( buf.length() - 1 >= 0 )
+                if ( wsSeen) 
                 {
-                    char lastChar = buf.charAt( buf.length() - 1 ) ;
-                    if ( lastChar != ' ' )
-                    {
-                        buf.append( ' ' ) ;
-                    }
+                    continue;
+                }
+                else
+                {
+                    wsSeen = true;
+                    newbuf[pos++] = ch;
                 }
             } 
             else 
             {
                 // Add all non-whitespace
-                buf.append( ch ) ;
+                wsSeen = false;
+                newbuf[pos++] = ch;
             }
         }
 
-        int ii = buf.length() - 1 ;
-        if ( ii >= 0 && Character.isWhitespace( buf.charAt( ii ) ) )
-        {
-            buf.deleteCharAt( ii ) ;
-        }
-
-        return buf.toString() ;
+        return new String( newbuf, 0, (wsSeen ? pos - 1 : pos) );
     }