2007-07-04 22:27:08 +00:00
|
|
|
package java.lang;
|
|
|
|
|
|
|
|
public class StringBuilder {
|
|
|
|
private Cell chain;
|
|
|
|
private int length;
|
|
|
|
|
|
|
|
public StringBuilder append(String s) {
|
|
|
|
chain = new Cell(s, chain);
|
|
|
|
length += s.length();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2007-07-20 03:18:25 +00:00
|
|
|
public StringBuilder append(Object o) {
|
|
|
|
return append(o == null ? "null" : o.toString());
|
|
|
|
}
|
|
|
|
|
2007-07-04 22:27:08 +00:00
|
|
|
public StringBuilder append(int v) {
|
2007-07-20 03:18:25 +00:00
|
|
|
return append(String.valueOf(v));
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public StringBuilder append(long v) {
|
2007-07-20 03:18:25 +00:00
|
|
|
return append(String.valueOf(v));
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-29 01:29:01 +00:00
|
|
|
public int length() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getChars(int srcOffset, int srcLength, char[] dst, int dstOffset)
|
|
|
|
{
|
|
|
|
if (srcOffset + srcLength > length) {
|
|
|
|
throw new IndexOutOfBoundsException();
|
|
|
|
}
|
|
|
|
|
2007-07-04 22:27:08 +00:00
|
|
|
int index = length;
|
|
|
|
for (Cell c = chain; c != null; c = c.next) {
|
2007-07-29 01:29:01 +00:00
|
|
|
int start = index - c.value.length();
|
|
|
|
int end = index;
|
|
|
|
index = start;
|
|
|
|
|
|
|
|
if (start < srcOffset) {
|
|
|
|
start = srcOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end > srcOffset + srcLength) {
|
|
|
|
end = srcOffset + srcLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start < end) {
|
|
|
|
c.value.getChars(start - index, end - start,
|
|
|
|
dst, dstOffset + (start - srcOffset));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
char[] array = new char[length];
|
|
|
|
getChars(0, length, array, 0);
|
2007-07-11 01:38:06 +00:00
|
|
|
return new String(array, 0, length, false);
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static class Cell {
|
|
|
|
public final String value;
|
|
|
|
public final Cell next;
|
|
|
|
|
|
|
|
public Cell(String value, Cell next) {
|
|
|
|
this.value = value;
|
|
|
|
this.next = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|