Outgoing socket I/O
[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 int lastindexOf(int ch, int fromIndex) {
62         for(int i=fromIndex;i>0;i--)
63             if (this.charAt(i)==ch)
64                 return i;
65         return -1;
66     }
67
68     public int indexOf(int ch) {
69         return this.indexOf(ch, 0);
70     }
71
72     public int indexOf(int ch, int fromIndex) {
73         for(int i=fromIndex;i<count;i++)
74             if (this.charAt(i)==ch)
75                 return i;
76         return -1;
77     }
78
79     public int indexOf(String str) {
80         return this.indexOf(str, 0);
81     }
82
83     public int indexOf(String str, int fromIndex) {
84         if (fromIndex<0)
85             fromIndex=0;
86         for(int i=fromIndex;i<=(count-str.count);i++)
87             if (regionMatches(i, str, 0, str.count))
88                 return i;
89         return -1;
90     }
91
92     public boolean startsWith(String str) {
93         return regionMatches(0, str, 0, str.count);
94     }
95
96     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
97         if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
98             return false;
99         for(int i=0;i<len;i++)
100             if (other.value[i+other.offset+ooffset]!=
101                 this.value[i+this.offset+toffset])
102                 return false;
103         return true;
104     }
105
106     public char[] toCharArray() {
107         char str[]=new char[count];
108         for(int i=0;i<count;i++)
109             str[i]=value[i+offset];
110         return str;
111     }
112
113     public byte[] getBytes() {
114         byte str[]=new byte[count];
115         for(int i=0;i<count;i++)
116             str[i]=(byte)value[i+offset];
117         return str;
118     }
119
120     public int length() {
121         return count;
122     }
123
124     public char charAt(int i) {
125         return value[i+offset];
126     }
127
128     public String toString() {
129         return this;
130     }
131
132     public static String valueOf(Object o) {
133         return o.toString();
134     }
135
136     public static String valueOf(int x) {
137         int length=0;
138         int tmp;
139         if (x<0)
140             tmp=-x;
141         else
142             tmp=x;
143         do {
144             tmp=tmp/10;
145             length=length+1;
146         } while(tmp!=0);
147         
148         char chararray[];
149         if (x<0)
150             chararray=new char[length+1];
151         else
152             chararray=new char[length];
153         int voffset;
154         if (x<0) {
155             chararray[0]='-';
156             voffset=1;
157             x=-x;
158         } else
159             voffset=0;
160         
161         do {
162             chararray[--length+voffset]=(char)(x%10+'0');
163             x=x/10;
164         } while (length!=0);
165         return new String(chararray);
166     }
167
168     public int hashCode() {
169         if (cachedHashcode!=0)
170             return cachedHashcode;
171         int hashcode=0;
172         for(int i=0;i<count;i++)
173             hashcode=hashcode*31+value[i+offset];
174         cachedHashcode=hashcode;
175         return hashcode;
176     }
177
178     public boolean equals(Object o) {
179         if (o.getType()!=getType())
180             return false;
181         String s=(String)o;
182         if (s.count!=count)
183             return false;
184         for(int i=0;i<count;i++) {
185             if (s.value[i+s.offset]!=value[i+offset])
186                 return false;
187         }
188         return true;
189     }
190 }