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