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