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