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