changes: now Inference engine works fine with the EyeTracking benchmark.
[IRC.git] / Robust / src / ClassLibrary / SSJava / StringBuffer.java
1 @LATTICE("V<C, V<O,V*")
2 @METHODDEFAULT("G<O,O<V,V<C,C<IN,C*,THISLOC=O,RETURNLOC=O,GLOBALLOC=G")
3 public class StringBuffer {
4   @LOC("V")
5   char value[];
6   @LOC("V")
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   @LATTICE("OUT<THIS,THIS<VAR,VAR<G,G<IN,THISLOC=THIS,GLOBALLOC=G,RETURNLOC=OUT")
41   public StringBuffer append(@LOC("IN") char c) {
42     @LOC("VAR") String str = String.valueOf(c);
43     return append(str);
44   }
45
46   public StringBuffer append(@LOC("IN") String s) {
47     if ((s.count + count) > value.length) {
48       // Need to allocate
49       @LOC("O,StringBuffer.V") char newvalue[] = new char[s.count + count + 16]; // 16
50                                                                                  // is
51       // DEFAULTSIZE
52       for (@LOC("O,StringBuffer.V") int i = 0; i < count; i++)
53         newvalue[i] = value[i];
54       for (@LOC("O,StringBuffer.V") int i = 0; i < s.count; i++)
55         newvalue[i + count] = s.value[i + s.offset];
56       value = newvalue;
57       count += s.count;
58     } else {
59       for (@LOC("O,StringBuffer.V") int i = 0; i < s.count; i++) {
60         value[i + count] = s.value[i + s.offset];
61       }
62       count += s.count;
63     }
64     return this;
65   }
66
67   public void ensureCapacity(int i) {
68     int size = 2 * count;
69     if (i > size)
70       size = i;
71     if (i > value.length) {
72       char newvalue[] = new char[i];
73       for (int ii = 0; ii < count; ii++)
74         newvalue[ii] = value[ii];
75       value = newvalue;
76     }
77   }
78
79   public StringBuffer append(StringBuffer s) {
80     if ((s.count + count) > value.length) {
81       // Need to allocate
82       char newvalue[] = new char[s.count + count + 16]; // 16 is DEFAULTSIZE
83       for (int i = 0; i < count; i++)
84         newvalue[i] = value[i];
85       for (int i = 0; i < s.count; i++)
86         newvalue[i + count] = s.value[i];
87       value = newvalue;
88       count += s.count;
89     } else {
90       for (int i = 0; i < s.count; i++) {
91         value[i + count] = s.value[i];
92       }
93       count += s.count;
94     }
95     return this;
96   }
97
98   public int indexOf(String str) {
99     return indexOf(str, 0);
100   }
101
102   public synchronized int indexOf(String str, int fromIndex) {
103     String vstr = new String(value, 0, count);
104     return vstr.indexOf(str, fromIndex);
105   }
106
107   public String toString() {
108     return new String(this);
109   }
110
111   public synchronized StringBuffer replace(int start, int end, String str) {
112     if (start < 0) {
113       // FIXME
114       System.printString("StringIndexOutOfBoundsException: " + start + "\n");
115     }
116     if (start > count) {
117       // FIXME
118       System.printString("StringIndexOutOfBoundsException: start > length()\n");
119     }
120     if (start > end) {
121       // FIXME
122       System.printString("StringIndexOutOfBoundsException: start > end\n");
123     }
124     if (end > count)
125       end = count;
126
127     if (end > count)
128       end = count;
129     int len = str.length();
130     int newCount = count + len - (end - start);
131     if (newCount > value.length)
132       expandCapacity(newCount);
133
134     System.arraycopy(value, end, value, start + len, count - end);
135     str.getChars(value, start);
136     count = newCount;
137     return this;
138   }
139
140   void expandCapacity(int minimumCapacity) {
141     int newCapacity = (value.length + 1) * 2;
142     if (newCapacity < 0) {
143       newCapacity = 0x7fffffff /* Integer.MAX_VALUE */;
144     } else if (minimumCapacity > newCapacity) {
145       newCapacity = minimumCapacity;
146     }
147     char newValue[] = new char[newCapacity];
148     System.arraycopy(value, 0, newValue, 0, count);
149     value = newValue;
150   }
151 }