01: package persistence.antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.jGuru.com
05: * Software rights: http://www.antlr.org/license.html
06: *
07: */
08:
09: // Implementation of a StringBuffer-like object that does not have the
10: // unfortunate side-effect of creating Strings with very large buffers.
11: public class ANTLRStringBuffer {
12: protected char[] buffer = null;
13: protected int length = 0; // length and also where to store next char
14:
15: public ANTLRStringBuffer() {
16: buffer = new char[50];
17: }
18:
19: public ANTLRStringBuffer(int n) {
20: buffer = new char[n];
21: }
22:
23: public final void append(char c) {
24: // This would normally be an "ensureCapacity" method, but inlined
25: // here for speed.
26: if (length >= buffer.length) {
27: // Compute a new length that is at least double old length
28: int newSize = buffer.length;
29: while (length >= newSize) {
30: newSize *= 2;
31: }
32: // Allocate new array and copy buffer
33: char[] newBuffer = new char[newSize];
34: for (int i = 0; i < length; i++) {
35: newBuffer[i] = buffer[i];
36: }
37: buffer = newBuffer;
38: }
39: buffer[length] = c;
40: length++;
41: }
42:
43: public final void append(String s) {
44: for (int i = 0; i < s.length(); i++) {
45: append(s.charAt(i));
46: }
47: }
48:
49: public final char charAt(int index) {
50: return buffer[index];
51: }
52:
53: final public char[] getBuffer() {
54: return buffer;
55: }
56:
57: public final int length() {
58: return length;
59: }
60:
61: public final void setCharAt(int index, char ch) {
62: buffer[index] = ch;
63: }
64:
65: public final void setLength(int newLength) {
66: if (newLength < length) {
67: length = newLength;
68: } else {
69: while (newLength > length) {
70: append('\0');
71: }
72: }
73: }
74:
75: public final String toString() {
76: return new String(buffer, 0, length);
77: }
78: }
|