This update:
[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 String subString(int beginIndex, int endIndex) {
43         String str=new String();
44         if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
45             // FIXME
46         }
47         str.value=this.value;
48         str.count=endIndex-beginIndex;
49         str.offset=this.offset+beginIndex;
50         return str;
51     }
52
53     public String subString(int beginIndex) {
54         return this.subString(beginIndex, this.count);
55     }
56
57     public int lastindexOf(int ch) {
58         return this.lastindexOf(ch, count - 1);
59     }
60
61     public String concat(String str) {
62         String newstr=new String();
63         newstr.count=this.count+str.count;
64         char charstr[]=new char[newstr.count];
65         newstr.value=charstr;
66         newstr.offset=0;
67         for(int i=0;i<count;i++) {
68             charstr[i]=value[i+offset];
69         }
70         for(int i=0;i<str.count;i++) {
71             charstr[i+count]=str.value[i+str.offset];
72         }
73         return newstr;
74     }
75
76     public int lastindexOf(int ch, int fromIndex) {
77         for(int i=fromIndex;i>0;i--)
78             if (this.charAt(i)==ch)
79                 return i;
80         return -1;
81     }
82
83     public String replace(char oldch, char newch) {
84         char[] buffer=new char[count];
85         for(int i=0;i<count;i++) {
86             char x=charAt(i);
87             if (x==oldch)
88                 x=newch;
89             buffer[i]=x;
90         }
91         return new String(buffer);
92     }
93
94     public int indexOf(int ch) {
95         return this.indexOf(ch, 0);
96     }
97
98     public int indexOf(int ch, int fromIndex) {
99         for(int i=fromIndex;i<count;i++)
100             if (this.charAt(i)==ch)
101                 return i;
102         return -1;
103     }
104
105     public int indexOf(String str) {
106         return this.indexOf(str, 0);
107     }
108
109     public int indexOf(String str, int fromIndex) {
110         if (fromIndex<0)
111             fromIndex=0;
112         for(int i=fromIndex;i<=(count-str.count);i++)
113             if (regionMatches(i, str, 0, str.count))
114                 return i;
115         return -1;
116     }
117
118     public boolean startsWith(String str) {
119         return regionMatches(0, str, 0, str.count);
120     }
121
122     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
123         if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
124             return false;
125         for(int i=0;i<len;i++)
126             if (other.value[i+other.offset+ooffset]!=
127                 this.value[i+this.offset+toffset])
128                 return false;
129         return true;
130     }
131
132     public char[] toCharArray() {
133         char str[]=new char[count];
134         for(int i=0;i<count;i++)
135             str[i]=value[i+offset];
136         return str;
137     }
138
139     public byte[] getBytes() {
140         byte str[]=new byte[count];
141         for(int i=0;i<count;i++)
142             str[i]=(byte)value[i+offset];
143         return str;
144     }
145
146     public int length() {
147         return count;
148     }
149
150     public char charAt(int i) {
151         return value[i+offset];
152     }
153
154     public String toString() {
155         return this;
156     }
157
158     public static String valueOf(Object o) {
159         if (o==null)
160             return "null";
161         else
162             return o.toString();
163     }
164
165     public static String valueOf(int x) {
166         int length=0;
167         int tmp;
168         if (x<0)
169             tmp=-x;
170         else
171             tmp=x;
172         do {
173             tmp=tmp/10;
174             length=length+1;
175         } while(tmp!=0);
176         
177         char chararray[];
178         if (x<0)
179             chararray=new char[length+1];
180         else
181             chararray=new char[length];
182         int voffset;
183         if (x<0) {
184             chararray[0]='-';
185             voffset=1;
186             x=-x;
187         } else
188             voffset=0;
189         
190         do {
191             chararray[--length+voffset]=(char)(x%10+'0');
192             x=x/10;
193         } while (length!=0);
194         return new String(chararray);
195     }
196
197     public int hashCode() {
198         if (cachedHashcode!=0)
199             return cachedHashcode;
200         int hashcode=0;
201         for(int i=0;i<count;i++)
202             hashcode=hashcode*31+value[i+offset];
203         cachedHashcode=hashcode;
204         return hashcode;
205     }
206
207     public boolean equals(Object o) {
208         if (o.getType()!=getType())
209             return false;
210         String s=(String)o;
211         if (s.count!=count)
212             return false;
213         for(int i=0;i<count;i++) {
214             if (s.value[i+s.offset]!=value[i+offset])
215                 return false;
216         }
217         return true;
218     }
219 }