directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r414764 - in /directory/branches/shared/optimization/ldap/src: main/java/org/apache/directory/shared/ldap/util/StringTools.java test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
Date Fri, 16 Jun 2006 07:30:03 GMT
Author: elecharny
Date: Fri Jun 16 00:30:03 2006
New Revision: 414764

URL: http://svn.apache.org/viewvc?rev=414764&view=rev
Log:
- Improved trim() functions 
- added some tests
- fixed a few bugs in trimRight() functions

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

Modified: directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java?rev=414764&r1=414763&r2=414764&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
(original)
+++ directory/branches/shared/optimization/ldap/src/main/java/org/apache/directory/shared/ldap/util/StringTools.java
Fri Jun 16 00:30:03 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 ) );
     }
 
 
@@ -1918,7 +1898,7 @@
             return pos;
         }
 
-        while ( ( pos > 0 ) && ( chars[pos - 1] == ' ' ) )
+        while ( ( pos >= 0 ) && ( chars[pos - 1] == ' ' ) )
         {
             pos--;
         }

Modified: directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
URL: http://svn.apache.org/viewvc/directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java?rev=414764&r1=414763&r2=414764&view=diff
==============================================================================
--- directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
(original)
+++ directory/branches/shared/optimization/ldap/src/test/java/org/apache/directory/shared/ldap/util/StringToolsTest.java
Fri Jun 16 00:30:03 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  " ) );
+    }
 }



Mime
View raw message