take pointer analysis out of the build for now
[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 static String concat2(String s1, String s2) {
116     if (s1==null)
117       return "null".concat(s2);
118     else
119       return s1.concat(s2);
120   }
121
122   public String concat(String str) {
123     String newstr=new String();
124     newstr.count=this.count+str.count;
125     char charstr[]=new char[newstr.count];
126     newstr.value=charstr;
127     newstr.offset=0;
128     for(int i=0; i<count; i++) {
129       charstr[i]=value[i+offset];
130     }
131     for(int i=0; i<str.count; i++) {
132       charstr[i+count]=str.value[i+str.offset];
133     }
134     return newstr;
135   }
136
137   public int lastindexOf(int ch, int fromIndex) {
138     for(int i=fromIndex; i>0; i--)
139       if (this.charAt(i)==ch)
140         return i;
141     return -1;
142   }
143
144   public String replace(char oldch, char newch) {
145     char[] buffer=new char[count];
146     for(int i=0; i<count; i++) {
147       char x=charAt(i);
148       if (x==oldch)
149         x=newch;
150       buffer[i]=x;
151     }
152     return new String(buffer);
153   }
154
155   public String toUpperCase() {
156     char[] buffer=new char[count];
157     for(int i=0; i<count; i++) {
158       char x=charAt(i);
159       if (x>='a'&&x<='z') {
160         x=(char) ((x-'a')+'A');
161       }
162       buffer[i]=x;
163     }
164     return new String(buffer);
165   }
166
167   public String toLowerCase() {
168     char[] buffer=new char[count];
169     for(int i=0; i<count; i++) {
170       char x=charAt(i);
171       if (x>='A'&&x<='Z') {
172         x=(char) ((x-'A')+'a');
173       }
174       buffer[i]=x;
175     }
176     return new String(buffer);
177   }
178
179   public int indexOf(int ch) {
180     return this.indexOf(ch, 0);
181   }
182
183   public int indexOf(int ch, int fromIndex) {
184     for(int i=fromIndex; i<count; i++)
185       if (this.charAt(i)==ch)
186         return i;
187     return -1;
188   }
189
190   public int indexOf(String str) {
191     return this.indexOf(str, 0);
192   }
193
194   public int indexOf(String str, int fromIndex) {
195     if (fromIndex<0)
196       fromIndex=0;
197     for(int i=fromIndex; i<=(count-str.count); i++)
198       if (regionMatches(i, str, 0, str.count))
199         return i;
200     return -1;
201   }
202
203         public int indexOfIgnoreCase(String str, int fromIndex) {
204                 if (fromIndex < 0) 
205                         fromIndex = 0;
206         }
207
208   public int lastIndexOf(String str, int fromIndex) {
209     int k=count-str.count;
210     if (k>fromIndex)
211       k=fromIndex;
212     for(; k>=0; k--) {
213       if (regionMatches(k, str, 0, str.count))
214         return k;
215     }
216     return -1;
217   }
218
219   public int lastIndexOf(String str) {
220     return lastIndexOf(str, count-str.count);
221   }
222
223   public boolean startsWith(String str) {
224     return regionMatches(0, str, 0, str.count);
225   }
226
227   public boolean startsWith(String str, int toffset) {
228     return regionMatches(toffset, str, 0, str.count);
229   }
230
231   public boolean regionMatches(int toffset, String other, int ooffset, int len) {
232     if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
233       return false;
234     for(int i=0; i<len; i++)
235       if (other.value[i+other.offset+ooffset]!=
236           this.value[i+this.offset+toffset])
237         return false;
238     return true;
239   }
240
241   public char[] toCharArray() {
242     char str[]=new char[count];
243     for(int i=0; i<count; i++)
244       str[i]=value[i+offset];
245     return str;
246   }
247
248   public byte[] getBytes() {
249     byte str[]=new byte[count];
250     for(int i=0; i<count; i++)
251       str[i]=(byte)value[i+offset];
252     return str;
253   }
254   
255   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
256     if((srcBegin < 0) || (srcEnd > count) || (srcBegin > srcEnd)) {
257       // FIXME
258       System.printString("Index error: "+srcBegin+" "+srcEnd+" "+count+"\n"+this);
259       System.exit(-1);
260     }
261     int len = srcEnd - srcBegin;
262     int j = dstBegin;
263     for(int i=srcBegin; i<srcEnd; i++)
264       dst[j++]=value[i+offset];
265     return;
266   }
267
268   public int length() {
269     return count;
270   }
271
272   public char charAt(int i) {
273     return value[i+offset];
274   }
275
276   public String toString() {
277     return this;
278   }
279
280   public static String valueOf(Object o) {
281     if (o==null)
282       return "null";
283     else
284       return o.toString();
285   }
286
287   public static String valueOf(boolean b) {
288     if (b)
289       return new String("true");
290     else
291       return new String("false");
292   }
293
294   public static String valueOf(char c) {
295     char ar[]=new char[1];
296     ar[0]=c;
297     return new String(ar);
298   }
299
300   public static String valueOf(int x) {
301     int length=0;
302     int tmp;
303     if (x<0)
304       tmp=-x;
305     else
306       tmp=x;
307     do {
308       tmp=tmp/10;
309       length=length+1;
310     } while(tmp!=0);
311
312     char chararray[];
313     if (x<0)
314       chararray=new char[length+1];
315     else
316       chararray=new char[length];
317     int voffset;
318     if (x<0) {
319       chararray[0]='-';
320       voffset=1;
321       x=-x;
322     } else
323       voffset=0;
324
325     do {
326       chararray[--length+voffset]=(char)(x%10+'0');
327       x=x/10;
328     } while (length!=0);
329     return new String(chararray);
330   }
331
332   public static String valueOf(double val) {
333     char[] chararray=new char[20];
334     String s=new String();
335     s.offset=0;
336     s.count=convertdoubletochar(val, chararray);
337     s.value=chararray;
338     return s;
339   }
340   
341   public static native int convertdoubletochar(double val, char [] chararray);
342
343   public static String valueOf(long x) {
344     int length=0;
345     long tmp;
346     if (x<0)
347       tmp=-x;
348     else
349       tmp=x;
350     do {
351       tmp=tmp/10;
352       length=length+1;
353     } while(tmp!=0);
354
355     char chararray[];
356     if (x<0)
357       chararray=new char[length+1];
358     else
359       chararray=new char[length];
360     int voffset;
361     if (x<0) {
362       chararray[0]='-';
363       voffset=1;
364       x=-x;
365     } else
366       voffset=0;
367
368     do {
369       chararray[--length+voffset]=(char)(x%10+'0');
370       x=x/10;
371     } while (length!=0);
372     return new String(chararray);
373   }
374
375   public int compareTo(String s) {
376     int smallerlength=count<s.count?count:s.count;
377
378     for( int i = 0; i < smallerlength; i++ ) {
379       int valDiff = this.charAt(i) - s.charAt(i);
380       if( valDiff != 0 ) {
381         return valDiff;
382       }
383     }
384     return count-s.count;
385   }
386
387   public int hashCode() {
388     if (cachedHashcode!=0)
389       return cachedHashcode;
390     int hashcode=0;
391     for(int i=0; i<count; i++)
392       hashcode=hashcode*31+value[i+offset];
393     cachedHashcode=hashcode;
394     return hashcode;
395   }
396
397   public boolean equals(Object o) {
398     if (o.getType()!=getType())
399       return false;
400     String s=(String)o;
401     if (s.count!=count)
402       return false;
403     for(int i=0; i<count; i++) {
404       if (s.value[i+s.offset]!=value[i+offset])
405         return false;
406     }
407     return true;
408   }
409
410   public boolean equalsIgnoreCase(String s) {
411     if (s.count!=count)
412       return false;
413     for(int i=0; i<count; i++) {
414       char l=s.value[i+s.offset];
415       char r=value[i+offset];
416       if (l>='a'&&l<='z')
417         l=(char)((l-'a')+'A');
418       if (r>='a'&&r<='z')
419         r=(char)((r-'a')+'A');
420       if (l!=r)
421         return false;
422     }
423     return true;
424   }
425
426   public Vector split() {
427     Vector splitted = new Vector();
428     int i;
429     int cnt =0;
430
431     // skip first spaces
432     for(i = 0; i< count;i++) {
433       if(value[i+offset] != '\n' && value[i+offset] != '\t' && value[i+offset] != ' ') 
434           break;
435     }
436
437     int oldi=i;
438
439     while(i<count) {
440       if(value[i+offset] == '\n' || value[i+offset] == '\t' || value[i+offset] == ' ') {
441           String t=new String();
442           t.value=value;
443           t.offset=oldi;
444           t.count=i-oldi;
445           splitted.addElement(t);
446
447           // skip extra spaces
448           while( i < count && ( value[i+offset] == '\n' || value[i+offset] == '\t' || value[i+offset] == ' ')) {
449               i++;
450           }
451           oldi=i;
452       } else {
453           i++;
454       }
455     }
456
457     if(i!=oldi) {
458         String t=new String();
459         t.value=value;
460         t.offset=oldi;
461         t.count=i-oldi;
462         splitted.addElement(t);
463     }
464     
465     return splitted;
466   }
467
468   public boolean contains(String str)
469   {
470     int i,j;
471     char[] strChar = str.toCharArray();
472     int cnt;
473
474     for(i = 0; i < count; i++) {
475       if(value[i] == strChar[0]) {
476         cnt=0;
477         for(j=0; j < str.length() && i+j < count;j++) {
478           if(value[i+j] == strChar[j])
479             cnt++;
480         }
481         if(cnt == str.length())
482           return true;
483       }
484     }
485
486     return false;
487
488   }
489 }