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