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(char c) {
229         char ar[]=new char[1];
230         ar[0]=c;
231         return new String(ar);
232     }
233
234     public static String valueOf(int x) {
235         int length=0;
236         int tmp;
237         if (x<0)
238             tmp=-x;
239         else
240             tmp=x;
241         do {
242             tmp=tmp/10;
243             length=length+1;
244         } while(tmp!=0);
245         
246         char chararray[];
247         if (x<0)
248             chararray=new char[length+1];
249         else
250             chararray=new char[length];
251         int voffset;
252         if (x<0) {
253             chararray[0]='-';
254             voffset=1;
255             x=-x;
256         } else
257             voffset=0;
258         
259         do {
260             chararray[--length+voffset]=(char)(x%10+'0');
261             x=x/10;
262         } while (length!=0);
263         return new String(chararray);
264     }
265
266     public static String valueOf(long x) {
267         int length=0;
268         long tmp;
269         if (x<0)
270             tmp=-x;
271         else
272             tmp=x;
273         do {
274             tmp=tmp/10;
275             length=length+1;
276         } while(tmp!=0);
277         
278         char chararray[];
279         if (x<0)
280             chararray=new char[length+1];
281         else
282             chararray=new char[length];
283         int voffset;
284         if (x<0) {
285             chararray[0]='-';
286             voffset=1;
287             x=-x;
288         } else
289             voffset=0;
290         
291         do {
292             chararray[--length+voffset]=(char)(x%10+'0');
293             x=x/10;
294         } while (length!=0);
295         return new String(chararray);
296     }
297
298     public int hashCode() {
299         if (cachedHashcode!=0)
300             return cachedHashcode;
301         int hashcode=0;
302         for(int i=0;i<count;i++)
303             hashcode=hashcode*31+value[i+offset];
304         cachedHashcode=hashcode;
305         return hashcode;
306     }
307
308     public boolean equals(Object o) {
309         if (o.getType()!=getType())
310             return false;
311         String s=(String)o;
312         if (s.count!=count)
313             return false;
314         for(int i=0;i<count;i++) {
315             if (s.value[i+s.offset]!=value[i+offset])
316                 return false;
317         }
318         return true;
319     }
320
321     public boolean equalsIgnoreCase(String s) {
322         if (s.count!=count)
323             return false;
324         for(int i=0;i<count;i++) {
325             char l=s.value[i+s.offset];
326             char r=value[i+offset];
327             if (l>='a'&&l<='z')
328                 l=(char)((l-'a')+'A');
329             if (r>='a'&&r<='z')
330                 r=(char)((r-'a')+'A');
331             if (l!=r)
332                 return false;
333         }
334         return true;
335     }
336 }