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