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 2007/01/14 00:00:28 UTC

svn commit: r495995 - in /directory/trunks/shared/ldap/src: main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java

Author: elecharny
Date: Sat Jan 13 15:00:26 2007
New Revision: 495995

URL: http://svn.apache.org/viewvc?view=rev&rev=495995
Log:
Fixed bug DIRSERVER-827, and added some tests. The method is now public

Modified:
    directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
    directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java

Modified: directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java?view=diff&rev=495995&r1=495994&r2=495995
==============================================================================
--- directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java (original)
+++ directory/trunks/shared/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifUtils.java Sat Jan 13 15:00:26 2007
@@ -191,39 +191,56 @@
 	 * @param nbChars the number of characters
 	 * @return the stripped String
 	 */
-	private static String stripLineToNChars( String str, int nbChars)
+	public static String stripLineToNChars( String str, int nbChars)
 	{
-		if ( str.length() <= nbChars )
+        int strLength = str.length();
+
+        if ( strLength <= nbChars )
 		{
 			return str;
 		}
+        
+        if ( nbChars < 2 )
+        {
+            throw new IllegalArgumentException( "The length of each line must be at least 2 chars long" );
+        }
 		
-		StringBuffer sb = new StringBuffer();
-		String substr;
-		int i = 0;
-		boolean firstPass = true;
-		
-		while ( i < (str.length() - nbChars) ) {
-			if ( firstPass )
-			{
-				substr = str.substring( i, i + nbChars);
-				firstPass = false;
-				// Since we add a space at the beginning of the next line,
-				// we need to update nbChars
-				nbChars--;
-			}
-			else
-			{
-				substr = str.substring( i, i + nbChars );
-			}
-			
-			sb.append( substr + "\n " );
-			i = i + nbChars;
-		}
-		// Adding the last characters
-		sb.append( str.substring(i, str.length() ) );
-		
-		return sb.toString();
+        // We will first compute the new size of the LDIF result
+        // It's at least nbChars chars plus one for \n
+        int charsPerLine = nbChars - 1;
+
+        int remaining = ( strLength - nbChars ) % charsPerLine;
+
+        int nbLines = 1 + ( ( strLength - nbChars ) / charsPerLine ) +
+                        ( remaining == 0 ? 0 : 1 );
+
+        int nbCharsTotal = strLength + nbLines + nbLines - 2;
+
+        char[] buffer = new char[ nbCharsTotal ];
+        char[] orig = str.toCharArray();
+        
+        int posSrc = 0;
+        int posDst = 0;
+        
+        System.arraycopy( orig, posSrc, buffer, posDst, nbChars );
+        posSrc += nbChars;
+        posDst += nbChars;
+        
+        for ( int i = 0; i < nbLines - 2; i ++ )
+        {
+            buffer[posDst++] = '\n';
+            buffer[posDst++] = ' ';
+            
+            System.arraycopy( orig, posSrc, buffer, posDst, charsPerLine );
+            posSrc += charsPerLine;
+            posDst += charsPerLine;
+        }
+
+        buffer[posDst++] = '\n';
+        buffer[posDst++] = ' ';
+        System.arraycopy( orig, posSrc, buffer, posDst, remaining == 0 ? charsPerLine : remaining );
+        
+        return new String( buffer );
 	}
 }
 

Modified: directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java?view=diff&rev=495995&r1=495994&r2=495995
==============================================================================
--- directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java (original)
+++ directory/trunks/shared/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifUtilsTest.java Sat Jan 13 15:00:26 2007
@@ -186,4 +186,43 @@
     {		
 		assertTrue( LdifUtils.isLDIFSafe( testString ) );
     }
+    
+    public void testStripLineToNChars()
+    {
+        String line = "abc";
+        
+        try
+        {
+            LdifUtils.stripLineToNChars( line, 1 );
+            fail();
+        }
+        catch ( IllegalArgumentException iae )
+        {
+            // This is correct
+        }
+        
+        String res = LdifUtils.stripLineToNChars( line, 2 );
+        assertEquals( "ab\n c", res );
+        assertEquals( "abc", LdifUtils.stripLineToNChars( line, 3 ) );
+    }
+
+    public void testStripLineTo5Chars()
+    {
+        assertEquals( "a", LdifUtils.stripLineToNChars( "a", 5 ) );
+        assertEquals( "ab", LdifUtils.stripLineToNChars( "ab", 5 ) );
+        assertEquals( "abc", LdifUtils.stripLineToNChars( "abc", 5 ) );
+        assertEquals( "abcd", LdifUtils.stripLineToNChars( "abcd", 5 ) );
+        assertEquals( "abcde", LdifUtils.stripLineToNChars( "abcde", 5 ) );
+        assertEquals( "abcde\n f", LdifUtils.stripLineToNChars( "abcdef", 5 ) );
+        assertEquals( "abcde\n fg", LdifUtils.stripLineToNChars( "abcdefg", 5 ) );
+        assertEquals( "abcde\n fgh", LdifUtils.stripLineToNChars( "abcdefgh", 5 ) );
+        assertEquals( "abcde\n fghi", LdifUtils.stripLineToNChars( "abcdefghi", 5 ) );
+        assertEquals( "abcde\n fghi\n j", LdifUtils.stripLineToNChars( "abcdefghij", 5 ) );
+        assertEquals( "abcde\n fghi\n jk", LdifUtils.stripLineToNChars( "abcdefghijk", 5 ) );
+        assertEquals( "abcde\n fghi\n jkl", LdifUtils.stripLineToNChars( "abcdefghijkl", 5 ) );
+        assertEquals( "abcde\n fghi\n jklm", LdifUtils.stripLineToNChars( "abcdefghijklm", 5 ) );
+        assertEquals( "abcde\n fghi\n jklm\n n", LdifUtils.stripLineToNChars( "abcdefghijklmn", 5 ) );
+        assertEquals( "abcde\n fghi\n jklm\n no", LdifUtils.stripLineToNChars( "abcdefghijklmno", 5 ) );
+        assertEquals( "abcde\n fghi\n jklm\n nop", LdifUtils.stripLineToNChars( "abcdefghijklmnop", 5 ) );
+    }
 }