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