This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
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(long x) {
274         int length=0;
275         long tmp;
276         if (x<0)
277             tmp=-x;
278         else
279             tmp=x;
280         do {
281             tmp=tmp/10;
282             length=length+1;
283         } while(tmp!=0);
284         
285         char chararray[];
286         if (x<0)
287             chararray=new char[length+1];
288         else
289             chararray=new char[length];
290         int voffset;
291         if (x<0) {
292             chararray[0]='-';
293             voffset=1;
294             x=-x;
295         } else
296             voffset=0;
297         
298         do {
299             chararray[--length+voffset]=(char)(x%10+'0');
300             x=x/10;
301         } while (length!=0);
302         return new String(chararray);
303     }
304
305     public int hashCode() {
306         if (cachedHashcode!=0)
307             return cachedHashcode;
308         int hashcode=0;
309         for(int i=0;i<count;i++)
310             hashcode=hashcode*31+value[i+offset];
311         cachedHashcode=hashcode;
312         return hashcode;
313     }
314
315     public boolean equals(Object o) {
316         if (o.getType()!=getType())
317             return false;
318         String s=(String)o;
319         if (s.count!=count)
320             return false;
321         for(int i=0;i<count;i++) {
322             if (s.value[i+s.offset]!=value[i+offset])
323                 return false;
324         }
325         return true;
326     }
327
328     public boolean equalsIgnoreCase(String s) {
329         if (s.count!=count)
330             return false;
331         for(int i=0;i<count;i++) {
332             char l=s.value[i+s.offset];
333             char r=value[i+offset];
334             if (l>='a'&&l<='z')
335                 l=(char)((l-'a')+'A');
336             if (r>='a'&&r<='z')
337                 r=(char)((r-'a')+'A');
338             if (l!=r)
339                 return false;
340         }
341         return true;
342     }
343 }