bebe02f0322d9f334ad6abf32333baddf4b4c3ac
[IRC.git] / Robust / src / ClassLibrary / SSJava / StringBuffer.java
1 @LATTICE("V<C, V<O")
2 @METHODDEFAULT("O<V,V<C,C<IN,C*,THISLOC=O,RETURNLOC=O")
3 public class StringBuffer {
4   @LOC("V")
5   char value[];
6   @LOC("C")
7   int count;
8
9   // private static final int DEFAULTSIZE=16;
10
11   public StringBuffer(String str) {
12     value = new char[str.count + 16]; // 16 is DEFAULTSIZE
13     count = str.count;
14     for (int i = 0; i < count; i++)
15       value[i] = str.value[i + str.offset];
16   }
17
18   public StringBuffer() {
19     value = new char[16]; // 16 is DEFAULTSIZE
20     count = 0;
21   }
22
23   public StringBuffer(int i) {
24     value = new char[i];
25     count = 0;
26   }
27
28   public int length() {
29     return count;
30   }
31
32   public int capacity() {
33     return value.length;
34   }
35
36   public char charAt(int x) {
37     return value[x];
38   }
39
40   public StringBuffer append(@LOC("IN") char c) {
41     return append(String.valueOf(c));
42   }
43
44   public StringBuffer append(@LOC("IN") String s) {
45     if ((s.count + count) > value.length) {
46       // Need to allocate
47       @LOC("C") char newvalue[] = new char[s.count + count + 16]; // 16 is
48                                                                   // DEFAULTSIZE
49       for (@LOC("C") int i = 0; i < count; i++)
50         newvalue[i] = value[i];
51       for (@LOC("C") int i = 0; i < s.count; i++)
52         newvalue[i + count] = s.value[i + s.offset];
53       value = newvalue;
54       count += s.count;
55     } else {
56       for (@LOC("C") int i = 0; i < s.count; i++) {
57         value[i + count] = s.value[i + s.offset];
58       }
59       count += s.count;
60     }
61     return this;
62   }
63
64   public void ensureCapacity(int i) {
65     int size = 2 * count;
66     if (i > size)
67       size = i;
68     if (i > value.length) {
69       char newvalue[] = new char[i];
70       for (int ii = 0; ii < count; ii++)
71         newvalue[ii] = value[ii];
72       value = newvalue;
73     }
74   }
75
76   public StringBuffer append(StringBuffer s) {
77     if ((s.count + count) > value.length) {
78       // Need to allocate
79       char newvalue[] = new char[s.count + count + 16]; // 16 is DEFAULTSIZE
80       for (int i = 0; i < count; i++)
81         newvalue[i] = value[i];
82       for (int i = 0; i < s.count; i++)
83         newvalue[i + count] = s.value[i];
84       value = newvalue;
85       count += s.count;
86     } else {
87       for (int i = 0; i < s.count; i++) {
88         value[i + count] = s.value[i];
89       }
90       count += s.count;
91     }
92     return this;
93   }
94
95   public int indexOf(String str) {
96     return indexOf(str, 0);
97   }
98
99   public synchronized int indexOf(String str, int fromIndex) {
100     String vstr = new String(value, 0, count);
101     return vstr.indexOf(str, fromIndex);
102   }
103
104   public String toString() {
105     return new String(this);
106   }
107
108   public synchronized StringBuffer replace(int start, int end, String str) {
109     if (start < 0) {
110       // FIXME
111       System.printString("StringIndexOutOfBoundsException: " + start + "\n");
112     }
113     if (start > count) {
114       // FIXME
115       System.printString("StringIndexOutOfBoundsException: start > length()\n");
116     }
117     if (start > end) {
118       // FIXME
119       System.printString("StringIndexOutOfBoundsException: start > end\n");
120     }
121     if (end > count)
122       end = count;
123
124     if (end > count)
125       end = count;
126     int len = str.length();
127     int newCount = count + len - (end - start);
128     if (newCount > value.length)
129       expandCapacity(newCount);
130
131     System.arraycopy(value, end, value, start + len, count - end);
132     str.getChars(value, start);
133     count = newCount;
134     return this;
135   }
136
137   void expandCapacity(int minimumCapacity) {
138     int newCapacity = (value.length + 1) * 2;
139     if (newCapacity < 0) {
140       newCapacity = 0x7fffffff /* Integer.MAX_VALUE */;
141     } else if (minimumCapacity > newCapacity) {
142       newCapacity = minimumCapacity;
143     }
144     char newValue[] = new char[newCapacity];
145     System.arraycopy(value, 0, newValue, 0, count);
146     value = newValue;
147   }
148 }