directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r414216 - in /directory/trunks/shared/ldap/src: main/java/org/apache/directory/shared/ldap/util/StringTools.java test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
Date Wed, 14 Jun 2006 12:19:19 GMT
Author: elecharny
Date: Wed Jun 14 05:19:18 2006
New Revision: 414216

URL: http://svn.apache.org/viewvc?rev=414216&view=rev
Log:
- Modified the trim() functions to avoid useless garbage collecting,
and to avoid stupid rewriting of String.trim() method, which is 3x
time faster than the insane method I wrote 9 months ago...
- Added some tests of those methods
Special thanks to Stephane Bailliez for the heads up !

Modified:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
    directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java?rev=414216&r1=414215&r2=414216&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
(original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
Wed Jun 14 05:19:18 2006
@@ -1673,26 +1673,7 @@
      */
     public static String trim( String str )
     {
-        if ( isEmpty( str ) )
-        {
-            return "";
-        }
-
-        char[] array = str.toCharArray();
-        int start = 0;
-        int end = array.length;
-
-        while ( ( start < end ) && ( array[start] == ' ' ) )
-        {
-            start++;
-        }
-
-        while ( ( end > start ) && ( array[end - 1] == ' ' ) )
-        {
-            end--;
-        }
-
-        return new String( array, start, ( end - start ) );
+    	return ( isEmpty( str ) ? "" : str.trim() );
     }
 
 
@@ -1768,15 +1749,15 @@
             return "";
         }
 
-        char[] array = str.toCharArray();
         int start = 0;
-
-        while ( ( start < array.length ) && ( array[start] == ' ' ) )
+        int end = str.length();
+        
+        while ( ( start < end ) && ( str.charAt( start ) == ' ' ) )
         {
             start++;
         }
 
-        return new String( array, start, array.length - start );
+        return ( start == 0 ? str : str.substring( start ) );
     }
 
 
@@ -1878,16 +1859,15 @@
             return "";
         }
 
-        char[] array = str.toCharArray();
-        int start = 0;
-        int end = array.length;
-
-        while ( ( start < end ) && ( array[start] == ' ' ) )
+        int length = str.length();
+        int end = length;
+        
+        while ( ( end > 0 ) && ( str.charAt( end - 1 ) == ' ' ) )
         {
-            start++;
+            end--;
         }
 
-        return new String( array, start, ( end - start ) );
+        return ( end == length ? str : str.substring( 0, end ) );
     }
 
 

Modified: directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java?rev=414216&r1=414215&r2=414216&view=diff
==============================================================================
--- directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
(original)
+++ directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
Wed Jun 14 05:19:18 2006
@@ -280,4 +280,37 @@
         assertEquals( "a b", StringTools.deepTrim( " a b", false ) );
         assertEquals( "a b", StringTools.deepTrim( "a b ", false ) );
     }
-}
+
+    public void testTrim()
+    {
+        assertEquals( "", StringTools.trim( (String)null ) );
+        assertEquals( "", StringTools.trim( "" ) );
+        assertEquals( "", StringTools.trim( " " ) );
+        assertEquals( "", StringTools.trim( "  " ) );
+        assertEquals( "a", StringTools.trim( "a  " ) );
+        assertEquals( "a", StringTools.trim( "  a" ) );
+        assertEquals( "a", StringTools.trim( "  a  " ) );
+    }
+
+    public void testTrimLeft()
+    {
+        assertEquals( "", StringTools.trimLeft( (String)null ) );
+        assertEquals( "", StringTools.trimLeft( "" ) );
+        assertEquals( "", StringTools.trimLeft( " " ) );
+        assertEquals( "", StringTools.trimLeft( "  " ) );
+        assertEquals( "a  ", StringTools.trimLeft( "a  " ) );
+        assertEquals( "a", StringTools.trimLeft( "  a" ) );
+        assertEquals( "a  ", StringTools.trimLeft( "  a  " ) );
+    }
+
+    public void testTrimRight()
+    {
+        assertEquals( "", StringTools.trimRight( (String)null ) );
+        assertEquals( "", StringTools.trimRight( "" ) );
+        assertEquals( "", StringTools.trimRight( " " ) );
+        assertEquals( "", StringTools.trimRight( "  " ) );
+        assertEquals( "a", StringTools.trimRight( "a  " ) );
+        assertEquals( "  a", StringTools.trimRight( "  a" ) );
+        assertEquals( "  a", StringTools.trimRight( "  a  " ) );
+    }
+}
\ No newline at end of file



Mime
View raw message