change
[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 str[]) {
11     char charstr[]=new char[str.length];
12     for(int i=0; i<str.length; i++)
13       charstr[i]=str[i];
14     this.value=charstr;
15     this.count=str.length;
16     this.offset=0;
17   }
18
19   public String(byte str[]) {
20     char charstr[]=new char[str.length];
21     for(int i=0; i<str.length; i++)
22       charstr[i]=(char)str[i];
23     this.value=charstr;
24     this.count=str.length;
25     this.offset=0;
26   }
27
28   public String(byte str[], int offset, int length) {
29     if (length>(str.length-offset))
30       length=str.length-offset;
31     char charstr[]=new char[length];
32     for(int i=0; i<length; i++)
33       charstr[i]=(char)str[i+offset];
34     this.value=charstr;
35     this.count=length;
36     this.offset=0;
37   }
38
39   public String(String str) {
40     this.value=str.value;
41     this.count=str.count;
42     this.offset=str.offset;
43   }
44
45   public String(StringBuffer strbuf) {
46     value=new char[strbuf.length()];
47     count=strbuf.length();
48     offset=0;
49     for(int i=0; i<count; i++)
50       value[i]=strbuf.value[i];
51   }
52
53   public boolean endsWith(String suffix) {
54     return regionMatches(count - suffix.count, suffix, 0, suffix.count);
55   }
56
57
58   public String substring(int beginIndex) {
59     return substring(beginIndex, this.count);
60   }
61
62   public String subString(int beginIndex, int endIndex) {
63     return substring(beginIndex, endIndex);
64   }
65
66   public String substring(int beginIndex, int endIndex) {
67     String str=new String();
68     if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
69       // FIXME
70       System.printString("Index error: "+beginIndex+" "+endIndex+" "+count+"\n"+this);
71     }
72     str.value=this.value;
73     str.count=endIndex-beginIndex;
74     str.offset=this.offset+beginIndex;
75     return str;
76   }
77
78   public String subString(int beginIndex) {
79     return this.subString(beginIndex, this.count);
80   }
81
82   public int lastindexOf(int ch) {
83     return this.lastindexOf(ch, count - 1);
84   }
85
86   public static String concat2(String s1, String s2) {
87     if (s1==null)
88       return "null".concat(s2);
89     else
90       return s1.concat(s2);
91   }
92
93   public String concat(String str) {
94     String newstr=new String();
95     newstr.count=this.count+str.count;
96     char charstr[]=new char[newstr.count];
97     newstr.value=charstr;
98     newstr.offset=0;
99     for(int i=0; i<count; i++) {
100       charstr[i]=value[i+offset];
101     }
102     for(int i=0; i<str.count; i++) {
103       charstr[i+count]=str.value[i+str.offset];
104     }
105     return newstr;
106   }
107
108   public int lastindexOf(int ch, int fromIndex) {
109     for(int i=fromIndex; i>0; i--)
110       if (this.charAt(i)==ch)
111         return i;
112     return -1;
113   }
114
115   public String replace(char oldch, char newch) {
116     char[] buffer=new char[count];
117     for(int i=0; i<count; i++) {
118       char x=charAt(i);
119       if (x==oldch)
120         x=newch;
121       buffer[i]=x;
122     }
123     return new String(buffer);
124   }
125
126   public String toUpperCase() {
127     char[] buffer=new char[count];
128     for(int i=0; i<count; i++) {
129       char x=charAt(i);
130       if (x>='a'&&x<='z') {
131         x=(char) ((x-'a')+'A');
132       }
133       buffer[i]=x;
134     }
135     return new String(buffer);
136   }
137
138   public int indexOf(int ch) {
139     return this.indexOf(ch, 0);
140   }
141
142   public int indexOf(int ch, int fromIndex) {
143     for(int i=fromIndex; i<count; i++)
144       if (this.charAt(i)==ch)
145         return i;
146     return -1;
147   }
148
149   public int indexOf(String str) {
150     return this.indexOf(str, 0);
151   }
152
153   public int indexOf(String str, int fromIndex) {
154     if (fromIndex<0)
155       fromIndex=0;
156     for(int i=fromIndex; i<=(count-str.count); i++)
157       if (regionMatches(i, str, 0, str.count))
158         return i;
159     return -1;
160   }
161
162   public int lastIndexOf(String str, int fromIndex) {
163     int k=count-str.count;
164     if (k>fromIndex)
165       k=fromIndex;
166     for(; k>=0; k--) {
167       if (regionMatches(k, str, 0, str.count))
168         return k;
169     }
170     return -1;
171   }
172
173   public int lastIndexOf(String str) {
174     return lastIndexOf(str, count-str.count);
175   }
176
177   public boolean startsWith(String str) {
178     return regionMatches(0, str, 0, str.count);
179   }
180
181   public boolean startsWith(String str, int toffset) {
182     return regionMatches(toffset, str, 0, str.count);
183   }
184
185   public boolean regionMatches(int toffset, String other, int ooffset, int len) {
186     if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
187       return false;
188     for(int i=0; i<len; i++)
189       if (other.value[i+other.offset+ooffset]!=
190           this.value[i+this.offset+toffset])
191         return false;
192     return true;
193   }
194
195   public char[] toCharArray() {
196     char str[]=new char[count];
197     for(int i=0; i<count; i++)
198       str[i]=value[i+offset];
199     return str;
200   }
201
202   public byte[] getBytes() {
203     byte str[]=new byte[count];
204     for(int i=0; i<count; i++)
205       str[i]=(byte)value[i+offset];
206     return str;
207   }
208
209   public int length() {
210     return count;
211   }
212
213   public char charAt(int i) {
214     return value[i+offset];
215   }
216
217   public String toString() {
218     return this;
219   }
220
221   public static String valueOf(Object o) {
222     if (o==null)
223       return "null";
224     else
225       return o.toString();
226   }
227
228   public static String valueOf(boolean b) {
229     if (b)
230       return new String("true");
231     else
232       return new String("false");
233   }
234
235   public static String valueOf(char c) {
236     char ar[]=new char[1];
237     ar[0]=c;
238     return new String(ar);
239   }
240
241   public static String valueOf(int x) {
242     int length=0;
243     int tmp;
244     if (x<0)
245       tmp=-x;
246     else
247       tmp=x;
248     do {
249       tmp=tmp/10;
250       length=length+1;
251     } while(tmp!=0);
252
253     char chararray[];
254     if (x<0)
255       chararray=new char[length+1];
256     else
257       chararray=new char[length];
258     int voffset;
259     if (x<0) {
260       chararray[0]='-';
261       voffset=1;
262       x=-x;
263     } else
264       voffset=0;
265
266     do {
267       chararray[--length+voffset]=(char)(x%10+'0');
268       x=x/10;
269     } while (length!=0);
270     return new String(chararray);
271   }
272
273   public static String valueOf(double x) {
274       return valueOf((long)x);
275   }
276
277   public static String valueOf(long x) {
278     int length=0;
279     long tmp;
280     if (x<0)
281       tmp=-x;
282     else
283       tmp=x;
284     do {
285       tmp=tmp/10;
286       length=length+1;
287     } while(tmp!=0);
288
289     char chararray[];
290     if (x<0)
291       chararray=new char[length+1];
292     else
293       chararray=new char[length];
294     int voffset;
295     if (x<0) {
296       chararray[0]='-';
297       voffset=1;
298       x=-x;
299     } else
300       voffset=0;
301
302     do {
303       chararray[--length+voffset]=(char)(x%10+'0');
304       x=x/10;
305     } while (length!=0);
306     return new String(chararray);
307   }
308
309   public int hashCode() {
310     if (cachedHashcode!=0)
311       return cachedHashcode;
312     int hashcode=0;
313     for(int i=0; i<count; i++)
314       hashcode=hashcode*31+value[i+offset];
315     cachedHashcode=hashcode;
316     return hashcode;
317   }
318
319   public boolean equals(Object o) {
320     if (o.getType()!=getType())
321       return false;
322     String s=(String)o;
323     if (s.count!=count)
324       return false;
325     for(int i=0; i<count; i++) {
326       if (s.value[i+s.offset]!=value[i+offset])
327         return false;
328     }
329     return true;
330   }
331
332   public boolean equalsIgnoreCase(String s) {
333     if (s.count!=count)
334       return false;
335     for(int i=0; i<count; i++) {
336       char l=s.value[i+s.offset];
337       char r=value[i+offset];
338       if (l>='a'&&l<='z')
339         l=(char)((l-'a')+'A');
340       if (r>='a'&&r<='z')
341         r=(char)((r-'a')+'A');
342       if (l!=r)
343         return false;
344     }
345     return true;
346   }
347 }