[Bug 35966] - [lang] StrBuilderTest#testReplaceStringString fails. From Nathan Beyer nbeyer@kc.rr.com.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@226922 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2005-08-02 01:03:45 +00:00
parent 23845c1751
commit 93b5f2a7ac
2 changed files with 24 additions and 7 deletions

View File

@ -1031,7 +1031,7 @@ public StrBuilder delete(String str) {
public StrBuilder replace(int startIndex, int endIndex, String str) { public StrBuilder replace(int startIndex, int endIndex, String str) {
endIndex = validateRange(startIndex, endIndex); endIndex = validateRange(startIndex, endIndex);
int insertLen = str.length(); int insertLen = str.length();
int removeLen = endIndex = startIndex; int removeLen = endIndex - startIndex;
int newSize = size - removeLen + insertLen; int newSize = size - removeLen + insertLen;
if (insertLen > removeLen) { if (insertLen > removeLen) {
ensureCapacity(newSize); ensureCapacity(newSize);
@ -1462,12 +1462,15 @@ public int lastIndexOf(String str, int startIndex) {
* @return this, to enable chaining * @return this, to enable chaining
*/ */
public StrBuilder reverse() { public StrBuilder reverse() {
if (size == 0) {
return this;
}
int half = size / 2; int half = size / 2;
char swap; for (int leftIdx = 0, rightIdx = size - 1; leftIdx < half; leftIdx++,rightIdx--) {
for (int i = 0; i < half; i++) { char swap = buf[leftIdx];
swap = buf[i]; buf[leftIdx] = buf[rightIdx];
buf[i] = buf[size - i]; buf[rightIdx] = swap;
buf[size - i] = swap;
} }
return this; return this;
} }

View File

@ -1324,11 +1324,25 @@ public void testAppendStrBuilder() {
assertEquals("", sb.toString()); assertEquals("", sb.toString());
} }
public void toStringBuffer() { public void testStringBuffer() {
StrBuilder sb = new StrBuilder(); StrBuilder sb = new StrBuilder();
assertEquals (new StringBuffer().toString(), sb.toStringBuffer().toString()); assertEquals (new StringBuffer().toString(), sb.toStringBuffer().toString());
sb.append("junit"); sb.append("junit");
assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString()); assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString());
} }
public void testReverse() {
StrBuilder sb = new StrBuilder();
String actual = sb.reverse().toString();
assertEquals ("", actual);
sb.append(true);
actual = sb.reverse().toString();
assertEquals("eurt", actual);
actual = sb.reverse().toString();
assertEquals("true", actual);
}
} }