OSDN Git Service

54be314b07b66fd7e5fd38e2ebac9ec62495cf65
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / String.java
1 /* Copyright (C) 1998, 1999, 2000  Free Software Foundation
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.lang;
10 import java.io.UnsupportedEncodingException;
11
12 /**
13  * @author Per Bothner <bothner@cygnus.com>
14  * @date September 4, 1998.  
15  */
16 /* Written using "Java Class Libraries", 2nd edition, plus online
17  * API docs for JDK 1.2 beta from http://www.javasoft.com.
18  * Status:  Complete to 1.1, but see FIXMEs. Also see testsuite results.
19  */
20
21 public final class String
22 {
23   private Object data;
24   private int boffset; // Note this is a byte offset - don't use in Java code!
25   private int count;
26
27   public String ()
28   {
29     init();
30   }
31
32   public String (String value)
33   {
34     data = value.data;
35     boffset = value.boffset;
36     count = value.count;
37   }
38
39   public String (StringBuffer buffer)
40   {
41     synchronized (buffer)
42       {
43         buffer.shared = true;
44         init (buffer.value, 0, buffer.count, true);
45       }
46   }
47
48   public String (char[] data)
49   {
50     init(data, 0, data.length, false);
51   }
52
53   public String (char[] data, int offset, int count)
54   {
55     init(data, offset, count, false);
56   }
57
58   public String (byte[] byteArray)
59   {
60     this (byteArray, 0, byteArray.length);
61   }
62
63   public String (byte[] byteArray, int offset, int count)
64   {
65     try
66       {
67         init (byteArray, offset, count,
68               System.getProperty("file.encoding", "8859_1"));
69       }
70     catch (UnsupportedEncodingException x1)
71       {
72         // Maybe the default encoding is bad.
73         try
74           {
75             init (byteArray, offset, count, "8859_1");
76           }
77         catch (UnsupportedEncodingException x2)
78           {
79             // We know this can't happen.
80           }
81       }
82   }
83
84   public String (byte[] byteArray, String enc)
85     throws UnsupportedEncodingException
86   {
87     this (byteArray, 0, byteArray.length, enc);
88   }
89
90   public String (byte[] byteArray, int offset, int count, String enc)
91     throws UnsupportedEncodingException
92   {
93     init (byteArray, offset, count, enc);
94   }
95
96   public static String copyValueOf(char[] data)
97   {
98     return copyValueOf (data, 0, data.length);
99   }
100
101   public static String copyValueOf(char[] data, int offset, int count)
102   {
103     String r = new String ();
104     r.init(data, offset, count, false);
105     return r;
106   }
107
108   /** @deprecated */
109   public String (byte[] ascii, int hibyte)
110   {
111     init(ascii, hibyte, 0, ascii.length);
112   }
113
114   /** @deprecated */
115   public String (byte[] ascii, int hibyte, int offset, int count)
116   {
117     init(ascii, hibyte, offset, count);
118   }
119
120   public String toString ()
121   {
122     return this;
123   }
124
125   public native boolean equals (Object anObject);
126
127   public native int hashCode ();
128
129   public int length ()
130   {
131     return count;
132   }
133
134   public native char charAt (int index);
135
136   public native void getChars (int srcBegin, int srcEnd,
137                                char[] dst, int dstBegin);
138
139   public byte[] getBytes ()
140   {
141     try
142       {
143         return getBytes (System.getProperty("file.encoding", "8859_1"));
144       }
145     catch (UnsupportedEncodingException x)
146       {
147         // This probably shouldn't happen, but could if file.encoding
148         // is somehow changed to a value we don't understand.
149         try
150           {
151             return getBytes ("8859_1");
152           }
153         catch (UnsupportedEncodingException x2)
154           {
155             // This really shouldn't happen, because the 8859_1
156             // encoding should always be available.
157             throw new InternalError ("couldn't find 8859_1 encoder");
158           }
159       }
160   }
161
162   public native byte[] getBytes (String enc)
163     throws UnsupportedEncodingException;
164
165   /** @deprecated */
166   public native void getBytes (int srcBegin, int srcEnd,
167                                 byte[] dst, int dstBegin);
168
169   public native char[] toCharArray ();
170
171   public native boolean equalsIgnoreCase (String anotherString);
172
173   public native int compareTo (String anotherString);
174
175   public native boolean regionMatches (int toffset,
176                                        String other, int ooffset, int len);
177
178   public native boolean regionMatches (boolean ignoreCase, int toffset,
179                                        String other, int ooffset, int len);
180
181   public boolean startsWith (String prefix)
182   {
183     return startsWith (prefix, 0);
184   }
185
186   public native boolean startsWith (String prefix, int toffset);
187
188   public boolean endsWith (String suffix)
189   {
190     return regionMatches (this.count - suffix.count, suffix, 0, suffix.count);
191   }
192
193   // No such method specified in the doc, including JDK 1.2.
194   // public boolean endsWith (String suffix, int toffset)
195   // {
196   //   return regionMatches (toffset, suffix, 0, suffix.count);
197   // }
198
199   // The Language Specification, and the JDK 1.2 API docs say that
200   // index and lastIndex take an int, while the Class Libraries
201   // say they take a char.  The former wins ...
202
203   public int indexOf (int ch)
204   {
205     return indexOf (ch, 0);
206   }
207
208   public native int indexOf (int ch, int fromIndex);
209
210   public int indexOf (String str)
211   {
212     return indexOf (str, 0);
213   }
214
215   public native int indexOf (String str, int fromIndex);
216
217   public int lastIndexOf (int ch)
218   {
219     return lastIndexOf (ch, count - 1);
220   }
221
222   public native int lastIndexOf (int ch, int fromIndex);
223
224   public int lastIndexOf (String str)
225   {
226     return lastIndexOf (str, count - str.count);
227   }
228
229   public int lastIndexOf (String str, int fromIndex)
230   {
231     if (fromIndex >= count)
232       fromIndex = count - str.count;
233     for (;; --fromIndex)
234       {
235         if (fromIndex < 0)
236           return -1;
237         if (startsWith(str, fromIndex))
238           return fromIndex;
239       }
240   }
241
242   public String substring (int beginIndex)
243   {
244     return substring (beginIndex, count);
245   }
246
247   public native String substring (int beginIndex, int endIndex);
248
249   public native String concat (String str);
250
251   public native String replace (char oldChar, char newChar);
252
253   public native String toLowerCase ();
254
255   public native String toUpperCase ();
256
257   public native String trim ();
258
259   public static String valueOf (Object obj)
260   {
261     return obj == null ? "null" : obj.toString();
262   }
263
264   public static String valueOf (char[] data)
265   {
266     return valueOf (data, 0, data.length);
267   }
268
269   public static native String valueOf (char[] data, int offset, int count);
270
271   public static String valueOf (boolean b)
272   {
273     return b ? "true" : "false";
274   }
275
276   public static native String valueOf (char c);
277
278   public static String valueOf (int i)
279   {
280     return Integer.toString(i);
281   }
282
283   public static String valueOf (long l)
284   {
285     return Long.toString(l);
286   }
287
288   public static String valueOf (float f)
289   {
290     return Float.toString(f);
291   }
292
293   public static String valueOf (double d)
294   {
295     return Double.toString(d);
296   }
297
298   public native String intern ();
299
300   private native void init ();
301   private native void init (char[] chars, int offset, int count,
302                             boolean dont_copy);
303   private native void init (byte[] chars, int hibyte, int offset, int count);
304   private native void init (byte[] chars, int offset, int count, String enc)
305     throws UnsupportedEncodingException;
306   private static native void unintern (Object obj);
307   private static native void rehash ();
308 }