1 package antlr; 2 3 9 10 13 public class ANTLRStringBuffer { 14 protected char[] buffer = null; 15 protected int length = 0; 17 18 public ANTLRStringBuffer() { 19 buffer = new char[50]; 20 } 21 22 public ANTLRStringBuffer(int n) { 23 buffer = new char[n]; 24 } 25 26 public final void append(char c) { 27 if (length >= buffer.length) { 30 int newSize = buffer.length; 32 while (length >= newSize) { 33 newSize *= 2; 34 } 35 char[] newBuffer = new char[newSize]; 37 for (int i = 0; i < length; i++) { 38 newBuffer[i] = buffer[i]; 39 } 40 buffer = newBuffer; 41 } 42 buffer[length] = c; 43 length++; 44 } 45 46 public final void append(String s) { 47 for (int i = 0; i < s.length(); i++) { 48 append(s.charAt(i)); 49 } 50 } 51 52 public final char charAt(int index) { 53 return buffer[index]; 54 } 55 56 final public char[] getBuffer() { 57 return buffer; 58 } 59 60 public final int length() { 61 return length; 62 } 63 64 public final void setCharAt(int index, char ch) { 65 buffer[index] = ch; 66 } 67 68 public final void setLength(int newLength) { 69 if (newLength < length) { 70 length = newLength; 71 } 72 else { 73 while (newLength > length) { 74 append('\0'); 75 } 76 } 77 } 78 79 public final String toString() { 80 return new String (buffer, 0, length); 81 } 82 } 83 | Popular Tags |