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