commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r230902 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/text/StrBuilder.java test/org/apache/commons/lang/text/StrBuilderTest.java
Date Mon, 08 Aug 2005 21:43:51 GMT
Author: scolebourne
Date: Mon Aug  8 14:43:39 2005
New Revision: 230902

URL: http://svn.apache.org/viewcvs?rev=230902&view=rev
Log:
Add append(String, int, int)

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java?rev=230902&r1=230901&r2=230902&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrBuilder.java
Mon Aug  8 14:43:39 2005
@@ -393,6 +393,34 @@
     }
 
     /**
+     * Appends a string to the string builder.
+     * Appending null will call {@link #appendNull()}.
+     *
+     * @param str  the string to append
+     * @param startIndex  the start index, inclusive, must be valid
+     * @param length  the length to append, must be valid
+     * @return this, to enable chaining
+     */
+    public StrBuilder append(String str, int startIndex, int length) {
+        if (str == null) {
+            return appendNull();
+        }
+        if (startIndex < 0 || startIndex > str.length()) {
+            throw new StringIndexOutOfBoundsException("startIndex must be valid");
+        }
+        if (length < 0 || (startIndex + length) > str.length()) {
+            throw new StringIndexOutOfBoundsException("length must be valid");
+        }
+        if (length > 0) {
+            int len = length();
+            ensureCapacity(len + length);
+            str.getChars(startIndex, startIndex + length, buffer, len);
+            size += length;
+        }
+        return this;
+    }
+
+    /**
      * Appends a string buffer to the string builder.
      * Appending null will call {@link #appendNull()}.
      *
@@ -477,8 +505,8 @@
         if (startIndex < 0 || startIndex > chars.length) {
             throw new StringIndexOutOfBoundsException("startIndex must be valid");
         }
-        if (length < 0) {
-            throw new StringIndexOutOfBoundsException("length must not be negative");
+        if (length < 0 || (startIndex + length) > chars.length) {
+            throw new StringIndexOutOfBoundsException("length must be valid");
         }
         if (length > 0) {
             int len = length();

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java?rev=230902&r1=230901&r2=230902&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
(original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrBuilderTest.java
Mon Aug  8 14:43:39 2005
@@ -547,6 +547,22 @@
         sb.append(FOO);
         assertEquals("foo", sb.toString());
 
+        sb.append((StringBuffer) null);
+        assertEquals("foo", sb.toString());
+
+        sb.append(new StringBuffer("baz"));
+        assertEquals("foobaz", sb.toString());
+
+        sb.append(new StrBuilder("yes"));
+        assertEquals("foobazyes", sb.toString());
+    }
+
+    public void testAppend_String() {
+        StrBuilder sb = new StrBuilder();
+
+        sb.append("foo");
+        assertEquals("foo", sb.toString());
+
         sb.append((String) null);
         assertEquals("foo", sb.toString());
 
@@ -555,15 +571,64 @@
 
         sb.append("bar");
         assertEquals("foobar", sb.toString());
+    }
 
-        sb.append((StringBuffer) null);
-        assertEquals("foobar", sb.toString());
+    public void testAppend_String_int_int() {
+        StrBuilder sb = new StrBuilder();
+        
+        sb.append("foo", 0, 3);
+        assertEquals("foo", sb.toString());
 
-        sb.append(new StringBuffer("baz"));
-        assertEquals("foobarbaz", sb.toString());
+        sb.append((String) null, 0, 1);
+        assertEquals("foo", sb.toString());
 
-        sb.append(new StrBuilder("yes"));
-        assertEquals("foobarbazyes", sb.toString());
+        try {
+            sb.append("bar", -1, 1);
+            fail("append(char[], -1,) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        try {
+            sb.append("bar", 3, 1);
+            fail("append(char[], 3,) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        try {
+            sb.append("bar", 1, -1);
+            fail("append(char[],, -1) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        try {
+            sb.append("bar", 1, 3);
+            fail("append(char[], 1, 3) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        try {
+            sb.append("bar", -1, 3);
+            fail("append(char[], -1, 3) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        try {
+            sb.append("bar", 4, 0);
+            fail("append(char[], 4, 0) expected IndexOutOfBoundsException");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+
+        sb.append("bar", 3, 0);
+        assertEquals("foo", sb.toString());
+
+        sb.append("abcbardef", 3, 3);
+        assertEquals("foobar", sb.toString());
     }
 
     public void testAppend_CharArray() {
@@ -576,6 +641,13 @@
         assertEquals("", sb.toString());
 
         sb.append(new char[]{'f', 'o', 'o'});
+        assertEquals("foo", sb.toString());
+    }
+
+    public void testAppend_CharArray_int_int() {
+        StrBuilder sb = new StrBuilder();
+        
+        sb.append(new char[]{'f', 'o', 'o'}, 0, 3);
         assertEquals("foo", sb.toString());
 
         sb.append((char[]) null, 0, 1);



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message