Forgot to add the OoOJava-specific class directory
[IRC.git] / Robust / src / ClassLibrary / OoOJava / 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(char dst[], int dstBegin) {
260     getChars(0, count, dst, dstBegin);
261   }
262
263   public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
264     if((srcBegin < 0) || (srcEnd > count) || (srcBegin > srcEnd)) {
265       // FIXME
266       System.printString("Index error: "+srcBegin+" "+srcEnd+" "+count+"\n"+this);
267       System.exit(-1);
268     }
269     int len = srcEnd - srcBegin;
270     int j = dstBegin;
271     for(int i=srcBegin; i<srcEnd; i++)
272       dst[j++]=value[i+offset];
273     return;
274   }
275
276   public int length() {
277     return count;
278   }
279
280   public char charAt(int i) {
281     return value[i+offset];
282   }
283
284   public String toString() {
285     return this;
286   }
287
288   public static String valueOf(Object o) {
289     if (o==null)
290       return "null";
291     else
292       return o.toString();
293   }
294
295   public static String valueOf(boolean b) {
296     if (b)
297       return new String("true");
298     else
299       return new String("false");
300   }
301
302   public static String valueOf(char c) {
303     char ar[]=new char[1];
304     ar[0]=c;
305     return new String(ar);
306   }
307
308   public static String valueOf(int x) {
309     int length=0;
310     int tmp;
311     if (x<0)
312       tmp=-x;
313     else
314       tmp=x;
315     do {
316       tmp=tmp/10;
317       length=length+1;
318     } while(tmp!=0);
319
320     char chararray[];
321     if (x<0)
322       chararray=new char[length+1];
323     else
324       chararray=new char[length];
325     int voffset;
326     if (x<0) {
327       chararray[0]='-';
328       voffset=1;
329       x=-x;
330     } else
331       voffset=0;
332
333     do {
334       chararray[--length+voffset]=(char)(x%10+'0');
335       x=x/10;
336     } while (length!=0);
337     return new String(chararray);
338   }
339
340   public static String valueOf(double val) {
341     char[] chararray=new char[20];
342     String s=new String();
343     s.offset=0;
344     s.count=convertdoubletochar(val, chararray);
345     s.value=chararray;
346     return s;
347   }
348
349   public static native int convertdoubletochar(double val, char [] chararray);
350
351   public static String valueOf(long x) {
352     int length=0;
353     long tmp;
354     if (x<0)
355       tmp=-x;
356     else
357       tmp=x;
358     do {
359       tmp=tmp/10;
360       length=length+1;
361     } while(tmp!=0);
362
363     char chararray[];
364     if (x<0)
365       chararray=new char[length+1];
366     else
367       chararray=new char[length];
368     int voffset;
369     if (x<0) {
370       chararray[0]='-';
371       voffset=1;
372       x=-x;
373     } else
374       voffset=0;
375
376     do {
377       chararray[--length+voffset]=(char)(x%10+'0');
378       x=x/10;
379     } while (length!=0);
380     return new String(chararray);
381   }
382
383   public int compareTo(String s) {
384     int smallerlength=count<s.count?count:s.count;
385
386     for( int i = 0; i < smallerlength; i++ ) {
387       int valDiff = this.charAt(i) - s.charAt(i);
388       if( valDiff != 0 ) {
389         return valDiff;
390       }
391     }
392     return count-s.count;
393   }
394
395   public int hashCode() {
396     int hashcode=0;
397     for(int i=0; i<count; i++)
398       hashcode=hashcode*31+value[i+offset];
399     return hashcode;
400   }
401
402   public boolean equals(Object o) {
403     if (o.getType()!=getType())
404       return false;
405     String s=(String)o;
406     if (s.count!=count)
407       return false;
408     for(int i=0; i<count; i++) {
409       if (s.value[i+s.offset]!=value[i+offset])
410         return false;
411     }
412     return true;
413   }
414
415   public boolean equalsIgnoreCase(String s) {
416     if (s.count!=count)
417       return false;
418     for(int i=0; i<count; i++) {
419       char l=s.value[i+s.offset];
420       char r=value[i+offset];
421       if (l>='a'&&l<='z')
422         l=(char)((l-'a')+'A');
423       if (r>='a'&&r<='z')
424         r=(char)((r-'a')+'A');
425       if (l!=r)
426         return false;
427     }
428     return true;
429   }
430
431   public Vector split() {
432     Vector splitted = new Vector();
433     int i;
434     int cnt =0;
435
436     // skip first spaces
437     for(i = 0; i< count; i++) {
438       if(value[i+offset] != '\n' && value[i+offset] != '\t' && value[i+offset] != ' ')
439         break;
440     }
441
442     int oldi=i;
443
444     while(i<count) {
445       if(value[i+offset] == '\n' || value[i+offset] == '\t' || value[i+offset] == ' ') {
446         String t=new String();
447         t.value=value;
448         t.offset=oldi;
449         t.count=i-oldi;
450         splitted.addElement(t);
451
452         // skip extra spaces
453         while( i < count && ( value[i+offset] == '\n' || value[i+offset] == '\t' || value[i+offset] == ' ')) {
454           i++;
455         }
456         oldi=i;
457       } else {
458         i++;
459       }
460     }
461
462     if(i!=oldi) {
463       String t=new String();
464       t.value=value;
465       t.offset=oldi;
466       t.count=i-oldi;
467       splitted.addElement(t);
468     }
469
470     return splitted;
471   }
472
473   public boolean contains(String str) {
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
509   public boolean matches(String regex) {
510     System.println("String.matches() is not fully supported");
511     return this.equals(regex);
512   }
513 }