changes
[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     char[] chararray=new char[20];
293     String s=new String();
294     s.offset=0;
295     s.count=convertdoubletochar(val, chararray);
296     s.value=chararray;
297     return s;
298   }
299   
300   public static native int convertdoubletochar(double val, char [] chararray);
301
302   public static String valueOf(long x) {
303     int length=0;
304     long tmp;
305     if (x<0)
306       tmp=-x;
307     else
308       tmp=x;
309     do {
310       tmp=tmp/10;
311       length=length+1;
312     } while(tmp!=0);
313
314     char chararray[];
315     if (x<0)
316       chararray=new char[length+1];
317     else
318       chararray=new char[length];
319     int voffset;
320     if (x<0) {
321       chararray[0]='-';
322       voffset=1;
323       x=-x;
324     } else
325       voffset=0;
326
327     do {
328       chararray[--length+voffset]=(char)(x%10+'0');
329       x=x/10;
330     } while (length!=0);
331     return new String(chararray);
332   }
333
334   public int compareTo(String s) {
335     int smallerlength=count<s.count?count:s.count;
336
337     for( int i = 0; i < smallerlength; i++ ) {
338       int valDiff = this.charAt(i) - s.charAt(i);
339       if( valDiff != 0 ) {
340         return valDiff;
341       }
342     }
343     return count-s.count;
344   }
345
346   public int hashCode() {
347     if (cachedHashcode!=0)
348       return cachedHashcode;
349     int hashcode=0;
350     for(int i=0; i<count; i++)
351       hashcode=hashcode*31+value[i+offset];
352     cachedHashcode=hashcode;
353     return hashcode;
354   }
355
356   public boolean equals(Object o) {
357     if (o.getType()!=getType())
358       return false;
359     String s=(String)o;
360     if (s.count!=count)
361       return false;
362     for(int i=0; i<count; i++) {
363       if (s.value[i+s.offset]!=value[i+offset])
364         return false;
365     }
366     return true;
367   }
368
369   public boolean equalsIgnoreCase(String s) {
370     if (s.count!=count)
371       return false;
372     for(int i=0; i<count; i++) {
373       char l=s.value[i+s.offset];
374       char r=value[i+offset];
375       if (l>='a'&&l<='z')
376         l=(char)((l-'a')+'A');
377       if (r>='a'&&r<='z')
378         r=(char)((r-'a')+'A');
379       if (l!=r)
380         return false;
381     }
382     return true;
383   }
384 }