adding a test case
[IRC.git] / Robust / src / ClassLibrary / SSJavaInfer / String.java
1 import Vector;
2
3 public class String {
4
5   char value[];
6
7   int count;
8
9   int offset;
10
11   private int cachedHashcode;
12
13   private String() {
14   }
15
16   public String(char c) {
17     char[] str = new char[1];
18     str[0] = c;
19     String(str);
20   }
21
22   public String(char str[]) {
23     char charstr[] = new char[str.length];
24     for (int i = 0; i < str.length; i++)
25       charstr[i] = str[i];
26     this.value = charstr;
27     this.count = str.length;
28     this.offset = 0;
29   }
30
31   public String(byte str[]) {
32     char charstr[] = new char[str.length];
33     for (int i = 0; i < str.length; i++)
34       charstr[i] = (char) str[i];
35     this.value = charstr;
36     this.count = str.length;
37     this.offset = 0;
38   }
39
40   public String(byte str[], int offset, int length) {
41     if (length > (str.length - offset))
42       length = str.length - offset;
43     char charstr[] = new char[length];
44     for (int i = 0; i < length; i++)
45       charstr[i] = (char) str[i + offset];
46     this.value = charstr;
47     this.count = length;
48     this.offset = 0;
49   }
50
51   public String(byte str[], String encoding) {
52     int length = this.count;
53     if (length > (str.length))
54       length = str.length;
55     char charstr[] = new char[length];
56     for (int i = 0; i < length; i++)
57       charstr[i] = (char) str[i];
58     this.value = charstr;
59     this.count = length;
60     this.offset = 0;
61   }
62
63   public String(char str[], int offset, int length) {
64     if (length > (str.length - offset))
65       length = str.length - offset;
66     char charstr[] = new char[length];
67     for (int i = 0; i < length; i++)
68       charstr[i] = str[i + offset];
69     this.value = charstr;
70     this.count = length;
71     this.offset = 0;
72   }
73
74   public String(String str) {
75     this.value = str.value;
76     this.count = str.count;
77     this.offset = str.offset;
78   }
79
80   public String(StringBuffer strbuf) {
81     value = new char[strbuf.length()];
82     count = strbuf.length();
83     offset = 0;
84     for (int i = 0; i < count; i++)
85       value[i] = strbuf.value[i];
86   }
87
88   public boolean endsWith(String suffix) {
89     return regionMatches(count - suffix.count, suffix, 0, suffix.count);
90   }
91
92   public String substring(int beginIndex) {
93     return substring(beginIndex, this.count);
94   }
95
96   public String subString(int beginIndex, int endIndex) {
97     return substring(beginIndex, endIndex);
98   }
99
100   public String substring(int beginIndex, int endIndex) {
101     String str = new String();
102     if (beginIndex > this.count || endIndex > this.count || beginIndex > endIndex) {
103       // FIXME
104       System.printString("Index error: " + beginIndex + " " + endIndex + " " + count + "\n" + this);
105     }
106     str.value = this.value;
107     str.count = endIndex - beginIndex;
108     str.offset = this.offset + beginIndex;
109     return str;
110   }
111
112   public String subString(int beginIndex) {
113     return this.subString(beginIndex, this.count);
114   }
115
116   public int lastindexOf(int ch) {
117     return this.lastindexOf(ch, count - 1);
118   }
119
120   public int lastIndexOf(char ch) {
121     return this.lastindexOf((int) ch, count - 1);
122   }
123
124   public static String concat2(String s1, String s2) {
125     if (s1 == null)
126       return "null".concat(s2);
127     else
128       return s1.concat(s2);
129   }
130
131   public String concat(String str) {
132     String newstr = new String();
133     newstr.count = this.count + str.count;
134     char charstr[] = new char[newstr.count];
135     newstr.offset = 0;
136     for (int i = 0; i < count; i++) {
137       charstr[i] = value[i + offset];
138     }
139     for (int i = 0; i < str.count; i++) {
140       charstr[i + count] = str.value[i + str.offset];
141     }
142     newstr.value = charstr;
143     return newstr;
144   }
145
146   public int lastindexOf(int ch, int fromIndex) {
147     for (int i = fromIndex; i > 0; i--)
148       if (this.charAt(i) == ch)
149         return i;
150     return -1;
151   }
152
153   public String replace(char oldch, char newch) {
154     char[] buffer = new char[count];
155     for (int i = 0; i < count; i++) {
156       char x = charAt(i);
157       if (x == oldch)
158         x = newch;
159       buffer[i] = x;
160     }
161     return new String(buffer);
162   }
163
164   public String toUpperCase() {
165     char[] buffer = new char[count];
166     for (int i = 0; i < count; i++) {
167       char x = charAt(i);
168       if (x >= 'a' && x <= 'z') {
169         x = (char) ((x - 'a') + 'A');
170       }
171       buffer[i] = x;
172     }
173     return new String(buffer);
174   }
175
176   public String toLowerCase() {
177     char[] buffer = new char[count];
178     for (int i = 0; i < count; i++) {
179       char x = charAt(i);
180       if (x >= 'A' && x <= 'Z') {
181         x = (char) ((x - 'A') + 'a');
182       }
183       buffer[i] = x;
184     }
185     return new String(buffer);
186   }
187
188   public int indexOf(int ch) {
189     return this.indexOf(ch, 0);
190   }
191
192   public int indexOf(int ch, int fromIndex) {
193     for (int i = fromIndex; i < count; i++)
194       if (this.charAt(i) == ch)
195         return i;
196     return -1;
197   }
198
199   public int indexOf(String str) {
200     return this.indexOf(str, 0);
201   }
202
203   public int indexOf(String str, int fromIndex) {
204     if (fromIndex < 0)
205       fromIndex = 0;
206     for (int i = fromIndex; i <= (count - str.count); i++)
207       if (regionMatches(i, str, 0, str.count))
208         return i;
209     return -1;
210   }
211
212   public int indexOfIgnoreCase(String str, int fromIndex) {
213     if (fromIndex < 0)
214       fromIndex = 0;
215   }
216
217   public int lastIndexOf(String str, int fromIndex) {
218     int k = count - str.count;
219     if (k > fromIndex)
220       k = fromIndex;
221     for (; k >= 0; k--) {
222       if (regionMatches(k, str, 0, str.count))
223         return k;
224     }
225     return -1;
226   }
227
228   public int lastIndexOf(String str) {
229     return lastIndexOf(str, count - str.count);
230   }
231
232   public boolean startsWith(String str) {
233     return regionMatches(0, str, 0, str.count);
234   }
235
236   public boolean startsWith(String str, int toffset) {
237     return regionMatches(toffset, str, 0, str.count);
238   }
239
240   public boolean regionMatches(int toffset, String other, int ooffset, int len) {
241     if (toffset < 0 || ooffset < 0 || (toffset + len) > count || (ooffset + len) > other.count)
242       return false;
243     for (int i = 0; i < len; i++)
244       if (other.value[i + other.offset + ooffset] != this.value[i + this.offset + toffset])
245         return false;
246     return true;
247   }
248
249   public char[] toCharArray() {
250     char str[] = new char[count];
251     for (int i = 0; i < count; i++)
252       str[i] = value[i + offset];
253     return str;
254   }
255
256   public byte[] getBytes() {
257     byte str[] = new byte[count];
258     for (int i = 0; i < count; i++)
259       str[i] = (byte) value[i + offset];
260     return str;
261   }
262
263   public void getChars(char dst[], int dstBegin) {
264     getChars(0, count, dst, dstBegin);
265   }
266
267   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
268     if ((srcBegin < 0) || (srcEnd > count) || (srcBegin > srcEnd)) {
269       // FIXME
270       System.printString("Index error: " + srcBegin + " " + srcEnd + " " + count + "\n" + this);
271       System.exit(-1);
272     }
273     int len = srcEnd - srcBegin;
274     int j = dstBegin;
275     for (int i = srcBegin; i < srcEnd; i++)
276       dst[j++] = value[i + offset];
277     return;
278   }
279
280   public int length() {
281     return count;
282   }
283
284   public char charAt(int i) {
285     return value[i + offset];
286   }
287
288   public String toString() {
289     return this;
290   }
291
292   public static String valueOf(Object o) {
293     if (o == null)
294       return "null";
295     else
296       return o.toString();
297   }
298
299   public static String valueOf(boolean b) {
300     if (b)
301       return new String("true");
302     else
303       return new String("false");
304   }
305
306   public static String valueOf(char c) {
307     char ar[] = new char[1];
308     ar[0] = c;
309     return new String(ar);
310   }
311
312   public static String valueOf(int x) {
313     int length = 0;
314     int tmp;
315     if (x < 0)
316       tmp = -x;
317     else
318       tmp = x;
319     TERMINATE: do {
320       tmp = tmp / 10;
321       length = length + 1;
322     } while (tmp != 0);
323
324     char[] chararray;
325     if (x < 0)
326       chararray = new char[length + 1];
327     else
328       chararray = new char[length];
329     int voffset;
330     if (x < 0) {
331       chararray[0] = '-';
332       voffset = 1;
333       x = -x;
334     } else
335       voffset = 0;
336
337     TERMINATE: do {
338       chararray[--length + voffset] = (char) (x % 10 + '0');
339       x = x / 10;
340     } while (length != 0);
341     return new String(chararray);
342   }
343
344   public static String valueOf(double val) {
345     char[] chararray = new char[20];
346     String s = new String();
347     s.offset = 0;
348     s.count = convertdoubletochar(val, chararray);
349     s.value = chararray;
350     return s;
351   }
352
353   public static native int convertdoubletochar(double val, char[] chararray);
354
355   public static String valueOf(long x) {
356     int length = 0;
357     long tmp;
358     if (x < 0)
359       tmp = -x;
360     else
361       tmp = x;
362
363     TERMINATE: do {
364       tmp = tmp / 10;
365       length = length + 1;
366     } while (tmp != 0);
367
368     char chararray[];
369     if (x < 0)
370       chararray = new char[length + 1];
371     else
372       chararray = new char[length];
373     int voffset;
374     if (x < 0) {
375       chararray[0] = '-';
376       voffset = 1;
377       x = -x;
378     } else
379       voffset = 0;
380
381     TERMINATE: do {
382       chararray[--length + voffset] = (char) (x % 10 + '0');
383       x = x / 10;
384     } while (length != 0);
385     return new String(chararray);
386   }
387
388   public int compareTo(String s) {
389     int smallerlength = count < s.count ? count : s.count;
390
391     for (int i = 0; i < smallerlength; i++) {
392       int valDiff = this.charAt(i) - s.charAt(i);
393       if (valDiff != 0) {
394         return valDiff;
395       }
396     }
397     return count - s.count;
398   }
399
400   public int hashCode() {
401     if (cachedHashcode != 0)
402       return cachedHashcode;
403     int hashcode = 0;
404     for (int i = 0; i < count; i++)
405       hashcode = hashcode * 31 + value[i + offset];
406     cachedHashcode = hashcode;
407     return hashcode;
408   }
409
410   public boolean equals(Object o) {
411     if (o.getType() != getType())
412       return false;
413     String s = (String) o;
414     if (s.count != count)
415       return false;
416     for (int i = 0; i < count; i++) {
417       if (s.value[i + s.offset] != value[i + offset])
418         return false;
419     }
420     return true;
421   }
422
423   public boolean equalsIgnoreCase(String s) {
424     if (s.count != count)
425       return false;
426     for (int i = 0; i < count; i++) {
427       char l = s.value[i + s.offset];
428       char r = value[i + offset];
429       if (l >= 'a' && l <= 'z')
430         l = (char) ((l - 'a') + 'A');
431       if (r >= 'a' && r <= 'z')
432         r = (char) ((r - 'a') + 'A');
433       if (l != r)
434         return false;
435     }
436     return true;
437   }
438
439   public Vector split() {
440     Vector splitted = new Vector();
441     int i;
442     int cnt = 0;
443
444     // skip first spaces
445     for (i = 0; i < count; i++) {
446       if (value[i + offset] != '\n' && value[i + offset] != '\t' && value[i + offset] != ' ')
447         break;
448     }
449
450     int oldi = i;
451
452     while (i < count) {
453       if (value[i + offset] == '\n' || value[i + offset] == '\t' || value[i + offset] == ' ') {
454         String t = new String();
455         t.value = value;
456         t.offset = oldi;
457         t.count = i - oldi;
458         splitted.addElement(t);
459
460         // skip extra spaces
461         while (i < count && (value[i + offset] == '\n' || value[i + offset] == '\t' || value[i + offset] == ' ')) {
462           i++;
463         }
464         oldi = i;
465       } else {
466         i++;
467       }
468     }
469
470     if (i != oldi) {
471       String t = new String();
472       t.value = value;
473       t.offset = oldi;
474       t.count = i - oldi;
475       splitted.addElement(t);
476     }
477
478     return splitted;
479   }
480
481   public boolean contains(String str) {
482     int i, j;
483     char[] strChar = str.toCharArray();
484     int cnt;
485
486     for (i = 0; i < count; i++) {
487       if (value[i] == strChar[0]) {
488         cnt = 0;
489         for (j = 0; j < str.length() && i + j < count; j++) {
490           if (value[i + j] == strChar[j])
491             cnt++;
492         }
493         if (cnt == str.length())
494           return true;
495       }
496     }
497
498     return false;
499
500   }
501
502   public String trim() {
503     int len = count;
504     int st = 0;
505     int off = offset; /* avoid getfield opcode */
506     char[] val = value; /* avoid getfield opcode */
507
508     while ((st < len) && (val[off + st] <= ' ')) {
509       st++;
510     }
511     while ((st < len) && (val[off + len - 1] <= ' ')) {
512       len--;
513     }
514     return ((st > 0) || (len < count)) ? substring(st, len) : this;
515   }
516
517   public boolean matches(String regex) {
518     System.println("String.matches() is not fully supported");
519     return this.equals(regex);
520   }
521 }