lots of new files
[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 String concat(String str) {
75         String newstr=new String();
76         newstr.count=this.count+str.count;
77         char charstr[]=new char[newstr.count];
78         newstr.value=charstr;
79         newstr.offset=0;
80         for(int i=0;i<count;i++) {
81             charstr[i]=value[i+offset];
82         }
83         for(int i=0;i<str.count;i++) {
84             charstr[i+count]=str.value[i+str.offset];
85         }
86         return newstr;
87     }
88
89     public int lastindexOf(int ch, int fromIndex) {
90         for(int i=fromIndex;i>0;i--)
91             if (this.charAt(i)==ch)
92                 return i;
93         return -1;
94     }
95
96     public String replace(char oldch, char newch) {
97         char[] buffer=new char[count];
98         for(int i=0;i<count;i++) {
99             char x=charAt(i);
100             if (x==oldch)
101                 x=newch;
102             buffer[i]=x;
103         }
104         return new String(buffer);
105     }
106
107     public int indexOf(int ch) {
108         return this.indexOf(ch, 0);
109     }
110
111     public int indexOf(int ch, int fromIndex) {
112         for(int i=fromIndex;i<count;i++)
113             if (this.charAt(i)==ch)
114                 return i;
115         return -1;
116     }
117
118     public int indexOf(String str) {
119         return this.indexOf(str, 0);
120     }
121
122     public int indexOf(String str, int fromIndex) {
123         if (fromIndex<0)
124             fromIndex=0;
125         for(int i=fromIndex;i<=(count-str.count);i++)
126             if (regionMatches(i, str, 0, str.count))
127                 return i;
128         return -1;
129     }
130
131     public int lastIndexOf(String str, int fromIndex) {
132         int k=count-str.count;
133         if (k>fromIndex)
134             k=fromIndex;
135         for(;k>=0;k--) {
136             if (regionMatches(fromIndex, str, 0, str.count))
137                 return k;
138         }
139         return -1;
140     }
141
142     public int lastIndexOf(String str) {
143         return lastIndexOf(str, count-str.count);
144     }
145     
146     public boolean startsWith(String str) {
147         return regionMatches(0, str, 0, str.count);
148     }
149
150     public boolean startsWith(String str, int toffset) {
151         return regionMatches(toffset, str, 0, str.count);
152     }
153
154     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
155         if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
156             return false;
157         for(int i=0;i<len;i++)
158             if (other.value[i+other.offset+ooffset]!=
159                 this.value[i+this.offset+toffset])
160                 return false;
161         return true;
162     }
163
164     public char[] toCharArray() {
165         char str[]=new char[count];
166         for(int i=0;i<count;i++)
167             str[i]=value[i+offset];
168         return str;
169     }
170
171     public byte[] getBytes() {
172         byte str[]=new byte[count];
173         for(int i=0;i<count;i++)
174             str[i]=(byte)value[i+offset];
175         return str;
176     }
177
178     public int length() {
179         return count;
180     }
181
182     public char charAt(int i) {
183         return value[i+offset];
184     }
185
186     public String toString() {
187         return this;
188     }
189
190     public static String valueOf(Object o) {
191         if (o==null)
192             return "null";
193         else
194             return o.toString();
195     }
196
197     public static String valueOf(char c) {
198         char ar[]=new char[1];
199         ar[0]=c;
200         return new String(ar);
201     }
202
203     public static String valueOf(int x) {
204         int length=0;
205         int tmp;
206         if (x<0)
207             tmp=-x;
208         else
209             tmp=x;
210         do {
211             tmp=tmp/10;
212             length=length+1;
213         } while(tmp!=0);
214         
215         char chararray[];
216         if (x<0)
217             chararray=new char[length+1];
218         else
219             chararray=new char[length];
220         int voffset;
221         if (x<0) {
222             chararray[0]='-';
223             voffset=1;
224             x=-x;
225         } else
226             voffset=0;
227         
228         do {
229             chararray[--length+voffset]=(char)(x%10+'0');
230             x=x/10;
231         } while (length!=0);
232         return new String(chararray);
233     }
234
235     public int hashCode() {
236         if (cachedHashcode!=0)
237             return cachedHashcode;
238         int hashcode=0;
239         for(int i=0;i<count;i++)
240             hashcode=hashcode*31+value[i+offset];
241         cachedHashcode=hashcode;
242         return hashcode;
243     }
244
245     public boolean equals(Object o) {
246         if (o.getType()!=getType())
247             return false;
248         String s=(String)o;
249         if (s.count!=count)
250             return false;
251         for(int i=0;i<count;i++) {
252             if (s.value[i+s.offset]!=value[i+offset])
253                 return false;
254         }
255         return true;
256     }
257
258     public boolean equalsIgnoreCase(String s) {
259         if (s.count!=count)
260             return false;
261         for(int i=0;i<count;i++) {
262             char l=s.value[i+s.offset];
263             char r=value[i+offset];
264             if (l>='a'&&l<='z')
265                 l+='A'-'a';
266             if (r>='a'&&r<='z')
267                 r+='A'-'a';
268             if (l!=r)
269                 return false;
270         }
271         return true;
272     }
273 }