wrapper classes
[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 lastIndexOf(String str, int fromIndex) {
181     int k=count-str.count;
182     if (k>fromIndex)
183       k=fromIndex;
184     for(; k>=0; k--) {
185       if (regionMatches(k, str, 0, str.count))
186         return k;
187     }
188     return -1;
189   }
190
191   public int lastIndexOf(String str) {
192     return lastIndexOf(str, count-str.count);
193   }
194
195   public boolean startsWith(String str) {
196     return regionMatches(0, str, 0, str.count);
197   }
198
199   public boolean startsWith(String str, int toffset) {
200     return regionMatches(toffset, str, 0, str.count);
201   }
202
203   public boolean regionMatches(int toffset, String other, int ooffset, int len) {
204     if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
205       return false;
206     for(int i=0; i<len; i++)
207       if (other.value[i+other.offset+ooffset]!=
208           this.value[i+this.offset+toffset])
209         return false;
210     return true;
211   }
212
213   public char[] toCharArray() {
214     char str[]=new char[count];
215     for(int i=0; i<count; i++)
216       str[i]=value[i+offset];
217     return str;
218   }
219
220   public byte[] getBytes() {
221     byte str[]=new byte[count];
222     for(int i=0; i<count; i++)
223       str[i]=(byte)value[i+offset];
224     return str;
225   }
226
227   public int length() {
228     return count;
229   }
230
231   public char charAt(int i) {
232     return value[i+offset];
233   }
234
235   public String toString() {
236     return this;
237   }
238
239   public static String valueOf(Object o) {
240     if (o==null)
241       return "null";
242     else
243       return o.toString();
244   }
245
246   public static String valueOf(boolean b) {
247     if (b)
248       return new String("true");
249     else
250       return new String("false");
251   }
252
253   public static String valueOf(char c) {
254     char ar[]=new char[1];
255     ar[0]=c;
256     return new String(ar);
257   }
258
259   public static String valueOf(int x) {
260     int length=0;
261     int tmp;
262     if (x<0)
263       tmp=-x;
264     else
265       tmp=x;
266     do {
267       tmp=tmp/10;
268       length=length+1;
269     } while(tmp!=0);
270
271     char chararray[];
272     if (x<0)
273       chararray=new char[length+1];
274     else
275       chararray=new char[length];
276     int voffset;
277     if (x<0) {
278       chararray[0]='-';
279       voffset=1;
280       x=-x;
281     } else
282       voffset=0;
283
284     do {
285       chararray[--length+voffset]=(char)(x%10+'0');
286       x=x/10;
287     } while (length!=0);
288     return new String(chararray);
289   }
290
291   public static String valueOf(double val) {
292     int i = 0, j = 0, k = 0;
293     long nodecimal = 0;
294     double decimal = 1.0d, valueA = 0.0d;
295     StringBuffer output = new StringBuffer();
296
297     for(i = 0; decimal != nodecimal; i++) {
298       long basePower = 1;
299       for(int x=0; x<i; x++) {
300         basePower*= 10;
301       }
302       nodecimal = (long) (val*basePower);
303       decimal = val*basePower;
304     } //i = place counted from right that decimal point appears
305
306     valueA = nodecimal; //valueA = val with no decimal point (val*10^i)
307
308     for(j = 0; decimal >= 0; j++) {
309       long basePower = 1;
310       for(int x=0; x<j; x++) {
311         basePower*= 10;
312       }
313       nodecimal = (long) (valueA - basePower);
314       decimal = (double) nodecimal;
315     } //j-1 = number of digits
316
317     i--;
318     j--;
319     decimal = 0;
320
321     for(k = j; k > 0; k--) {
322       if(k == i) { //if a decimal point was previously found
323         //insert it where its meant to be
324         output.append((char)46);
325       }
326       long basePower = 1;
327       for(int x=0; x<(k-1); x++) {
328         basePower*= 10;
329       }
330       nodecimal = ((long) (valueA - decimal) / basePower);
331       decimal += nodecimal*basePower;
332       output.append((char)(48 + nodecimal));
333     }
334
335     return output.toString();
336   }
337
338   public static long basePower(int x, int y) {
339     long t = 1;
340     for(int i=0; i<y; i++) {
341       t *= x;
342     }
343     return t;
344   }
345
346   public static String valueOf(long x) {
347     int length=0;
348     long tmp;
349     if (x<0)
350       tmp=-x;
351     else
352       tmp=x;
353     do {
354       tmp=tmp/10;
355       length=length+1;
356     } while(tmp!=0);
357
358     char chararray[];
359     if (x<0)
360       chararray=new char[length+1];
361     else
362       chararray=new char[length];
363     int voffset;
364     if (x<0) {
365       chararray[0]='-';
366       voffset=1;
367       x=-x;
368     } else
369       voffset=0;
370
371     do {
372       chararray[--length+voffset]=(char)(x%10+'0');
373       x=x/10;
374     } while (length!=0);
375     return new String(chararray);
376   }
377
378   public int compareTo(String s) {
379     int lenDiff = this.length() - s.length();
380     if( lenDiff != 0 ) {
381       return lenDiff;
382     }
383     for( int i = 0; i < this.length(); ++i ) {
384       int valDiff = this.charAt(i) - s.charAt(i);
385       if( valDiff != 0 ) {
386         return valDiff;
387       }
388     }
389     return 0;
390   }
391
392   public int hashCode() {
393     if (cachedHashcode!=0)
394       return cachedHashcode;
395     int hashcode=0;
396     for(int i=0; i<count; i++)
397       hashcode=hashcode*31+value[i+offset];
398     cachedHashcode=hashcode;
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 }