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