Add append(String, int, int)

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230902 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2005-08-08 21:43:39 +00:00
parent 5346b0d03c
commit ff6bf42b60
2 changed files with 109 additions and 9 deletions

View File

@ -392,6 +392,34 @@ public StrBuilder append(String str) {
return this;
}
/**
* 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 @@ public StrBuilder append(char[] chars, int startIndex, int length) {
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();

View File

@ -547,6 +547,22 @@ public void testAppend_Object() {
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 @@ public void testAppend_Object() {
sb.append("bar");
assertEquals("foobar", sb.toString());
}
sb.append((StringBuffer) null);
public void testAppend_String_int_int() {
StrBuilder sb = new StrBuilder();
sb.append("foo", 0, 3);
assertEquals("foo", sb.toString());
sb.append((String) null, 0, 1);
assertEquals("foo", 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());
sb.append(new StringBuffer("baz"));
assertEquals("foobarbaz", sb.toString());
sb.append(new StrBuilder("yes"));
assertEquals("foobarbazyes", sb.toString());
}
public void testAppend_CharArray() {
@ -577,6 +642,13 @@ public void testAppend_CharArray() {
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);
assertEquals("foo", sb.toString());