Bug fix
[IRC.git] / Robust / src / ClassLibrary / StringBuffer.java
1 public class StringBuffer {
2     char value[];
3     int count;
4     int offset;
5     //    private static final int DEFAULTSIZE=16;
6
7     public StringBuffer(String str) {
8         value=new char[str.count+16];//16 is DEFAULTSIZE
9         count=str.count;
10         offset=0;
11         for(int i=0;i<count;i++)
12             value[i]=str.value[i+str.offset];
13     }
14
15     public int length() {
16         return count;
17     }
18
19     public int capacity() {
20         return value.length;
21     }
22
23     public char charAt(int x) {
24         return value[x+offset];
25     }
26
27     public void append(String s) {
28         if ((s.count+count+offset)>value.length) {
29             // Need to allocate
30             char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
31             for(int i=0;i<count;i++)
32                 newvalue[i]=value[i+offset];
33             for(int i=0;i<s.count;i++)
34                 newvalue[i+count]=s.value[i+s.offset];
35             value=newvalue;
36             count+=s.count;
37             offset=0;
38         } else {
39             for(int i=0;i<s.count;i++) {
40                 value[i+count+offset]=s.value[i+s.offset];
41             }
42             count+=s.count;
43         }
44     }
45
46     public void append(StringBuffer s) {
47         if ((s.count+count+offset)>value.length) {
48             // Need to allocate
49             char newvalue[]=new char[s.count+count+16]; //16 is DEFAULTSIZE
50             for(int i=0;i<count;i++)
51                 newvalue[i]=value[i+offset];
52             for(int i=0;i<s.count;i++)
53                 newvalue[i+count]=s.value[i+s.offset];
54             value=newvalue;
55             count+=s.count;
56             offset=0;
57         } else {
58             for(int i=0;i<s.count;i++) {
59                 value[i+count+offset]=s.value[i+s.offset];
60             }
61             count+=s.count;
62         }
63     }
64
65     public String toString() {
66         return new String(this);
67     }
68 }