checking in code
[IRC.git] / Robust / src / ClassLibrary / String.java
1 public class String {
2     char value[];
3     int count;
4     int offset;
5
6     private String() {
7     }
8
9     public String(char str[]) {
10         char charstr[]=new char[str.length];
11         for(int i=0;i<str.length;i++)
12             charstr[i]=str[i];
13         this.value=charstr;
14         this.count=str.length;
15         this.offset=0;
16     }
17
18     public String(byte str[]) {
19         char charstr[]=new char[str.length];
20         for(int i=0;i<str.length;i++)
21             charstr[i]=(char)str[i];
22         this.value=charstr;
23         this.count=str.length;
24         this.offset=0;
25     }
26
27     public String(String str) {
28         this.value=str.value;
29         this.count=str.count;
30         this.offset=str.offset;
31     }
32
33     public String(StringBuffer strbuf) {
34         value=new char[strbuf.length()];
35         count=strbuf.length();
36         offset=0;
37         for(int i=0;i<count;i++)
38             value[i]=strbuf.value[i];
39     }
40
41     public String subString(int beginIndex, int endIndex) {
42         String str=new String();
43         if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
44             // FIXME
45         }
46         str.value=this.value;
47         str.count=endIndex-beginIndex;
48         str.offset=this.offset+beginIndex;
49         return str;
50     }
51
52     public String subString(int beginIndex) {
53         return this.subString(beginIndex, this.count);
54     }
55
56     public int indexOf(int ch) {
57         return this.indexOf(ch, 0);
58     }
59
60     public int indexOf(int ch, int fromIndex) {
61         for(int i=fromIndex;i<count;i++)
62             if (this.charAt(i)==ch)
63                 return i;
64         return -1;
65     }
66
67     public int indexOf(String str) {
68         return this.indexOf(str, 0);
69     }
70
71     public int indexOf(String str, int fromIndex) {
72         if (fromIndex<0)
73             fromIndex=0;
74         for(int i=fromIndex;i<=(count-str.count);i++)
75             if (regionMatches(i, str, 0, str.count))
76                 return i;
77         return -1;
78     }
79
80     public boolean startsWith(String str) {
81         return regionMatches(0, str, 0, str.count);
82     }
83
84     public boolean regionMatches(int toffset, String other, int ooffset, int len) {
85         if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
86             return false;
87         for(int i=0;i<len;i++)
88             if (other.value[i+other.offset+ooffset]!=
89                 this.value[i+this.offset+toffset])
90                 return false;
91         return true;
92     }
93
94     public char[] toCharArray() {
95         char str[]=new char[count];
96         for(int i=0;i<count;i++)
97             str[i]=value[i+offset];
98         return str;
99     }
100
101     public byte[] getBytes() {
102         byte str[]=new byte[count];
103         for(int i=0;i<count;i++)
104             str[i]=(byte)value[i+offset];
105         return str;
106     }
107
108     public int length() {
109         return count;
110     }
111
112     public char charAt(int i) {
113         return value[i+offset];
114     }
115
116     public String toString() {
117         return this;
118     }
119
120     public static String valueOf(Object o) {
121         return o.toString();
122     }
123
124     public static String valueOf(int x) {
125         int length=0;
126         int tmp;
127         if (x<0)
128             tmp=-x;
129         else
130             tmp=x;
131         do {
132             tmp=tmp/10;
133             length=length+1;
134         } while(tmp!=0);
135         
136         char chararray[];
137         if (x<0)
138             chararray=new char[length+1];
139         else
140             chararray=new char[length];
141         int voffset;
142         if (x<0) {
143             chararray[0]='-';
144             voffset=1;
145             x=-x;
146         } else
147             voffset=0;
148         
149         do {
150             chararray[--length+voffset]=(char)(x%10+'0');
151             x=x/10;
152         } while (length!=0);
153         return new String(chararray);
154     }
155 }