fix length tracking bugs in StringBuilder

This commit is contained in:
Joel Dice 2007-12-13 09:19:58 -07:00
parent fe24005ff0
commit 216224dcc4

View File

@ -20,12 +20,9 @@ public class StringBuilder {
private void flush() { private void flush() {
if (position > 0) { if (position > 0) {
char[] b = buffer; chain = new Cell(new String(buffer, 0, position, false), chain);
int p = position;
buffer = null; buffer = null;
position = 0; position = 0;
append(new String(b, 0, p, false));
length -= p;
} }
} }
@ -98,7 +95,6 @@ public class StringBuilder {
flush(); flush();
int index = length; int index = length;
-- length;
Cell p = null; Cell p = null;
for (Cell c = chain; c != null; c = c.next) { for (Cell c = chain; c != null; c = c.next) {
int start = index - c.value.length(); int start = index - c.value.length();
@ -123,7 +119,6 @@ public class StringBuilder {
flush(); flush();
int index = length; int index = length;
-- length;
for (Cell c = chain; c != null; c = c.next) { for (Cell c = chain; c != null; c = c.next) {
int start = index - c.value.length(); int start = index - c.value.length();
index = start; index = start;
@ -139,6 +134,8 @@ public class StringBuilder {
break; break;
} }
} }
length += s.length();
} }
return this; return this;
@ -160,7 +157,6 @@ public class StringBuilder {
flush(); flush();
int index = length; int index = length;
-- length;
Cell p = null; Cell p = null;
for (Cell c = chain; c != null; c = c.next) { for (Cell c = chain; c != null; c = c.next) {
int e = index; int e = index;
@ -190,6 +186,8 @@ public class StringBuilder {
} }
} }
length -= (end - start);
return this; return this;
} }