1 | /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 | import java.io.Serializable;
|
---|
12 | import java.lang.Comparable;
|
---|
13 | import java.util.Comparator;
|
---|
14 | import java.util.Locale;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @author Per Bothner <bothner@cygnus.com>
|
---|
18 | * @date September 4, 1998.
|
---|
19 | */
|
---|
20 | /* Written using "Java Class Libraries", 2nd edition, plus online
|
---|
21 | * API docs for JDK 1.2 beta from http://www.javasoft.com.
|
---|
22 | * Status: Complete to 1.3.
|
---|
23 | */
|
---|
24 |
|
---|
25 | public final class String implements Serializable, Comparable, CharSequence
|
---|
26 | {
|
---|
27 | private Object data;
|
---|
28 | private int boffset; // Note this is a byte offset - don't use in Java code!
|
---|
29 | private int count;
|
---|
30 |
|
---|
31 | // This is probably not necessary because this class is special cased already
|
---|
32 | // but it will avoid showing up as a discrepancy when comparing SUIDs.
|
---|
33 | private static final long serialVersionUID = -6849794470754667710L;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * An implementation for {@link CASE_INSENSITIVE_ORDER}.
|
---|
37 | * This must be {@link Serializable}.
|
---|
38 | */
|
---|
39 | private static final class CaseInsensitiveComparator
|
---|
40 | implements Comparator, Serializable
|
---|
41 | {
|
---|
42 | /**
|
---|
43 | * The default private constructor generates unnecessary overhead
|
---|
44 | */
|
---|
45 | CaseInsensitiveComparator() {}
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Compares two Strings, using
|
---|
49 | * <code>String.compareToIgnoreCase(String)</code>.
|
---|
50 | *
|
---|
51 | * @param o1 the first string
|
---|
52 | * @param o2 the second string
|
---|
53 | * @return < 0, 0, or > 0 depending on the case-insensitive
|
---|
54 | * comparison of the two strings.
|
---|
55 | * @throws NullPointerException if either argument is null
|
---|
56 | * @throws ClassCastException if either argument is not a String
|
---|
57 | * @see #compareToIgnoreCase(String)
|
---|
58 | */
|
---|
59 | public int compare(Object o1, Object o2)
|
---|
60 | {
|
---|
61 | return ((String) o1).compareToIgnoreCase((String) o2);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * A Comparator that uses <code>String.compareToIgnoreCase(String)</code>.
|
---|
67 | * This comparator is {@link Serializable}.
|
---|
68 | *
|
---|
69 | * @since 1.2
|
---|
70 | */
|
---|
71 | public static final Comparator CASE_INSENSITIVE_ORDER
|
---|
72 | = new CaseInsensitiveComparator();
|
---|
73 |
|
---|
74 | public String ()
|
---|
75 | {
|
---|
76 | init();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public String (String value)
|
---|
80 | {
|
---|
81 | data = value.data;
|
---|
82 | boffset = value.boffset;
|
---|
83 | count = value.count;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public String (StringBuffer buffer)
|
---|
87 | {
|
---|
88 | synchronized (buffer)
|
---|
89 | {
|
---|
90 | buffer.shared = true;
|
---|
91 | init (buffer.value, 0, buffer.count, true);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | // This is used by gnu.gcj.runtime.StringBuffer, so it must have
|
---|
96 | // package-private protection. It is accessed via CNI and so avoids
|
---|
97 | // ordinary protection mechanisms.
|
---|
98 | String (gnu.gcj.runtime.StringBuffer buffer)
|
---|
99 | {
|
---|
100 | // No need to synchronize or mark the buffer, since we know it is
|
---|
101 | // only used once.
|
---|
102 | init (buffer.value, 0, buffer.count, true);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public String (char[] data)
|
---|
106 | {
|
---|
107 | init(data, 0, data.length, false);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public String (char[] data, int offset, int count)
|
---|
111 | {
|
---|
112 | init(data, offset, count, false);
|
---|
113 | }
|
---|
114 |
|
---|
115 | // This is used by Integer.toString(int,int).
|
---|
116 | String (char[] data, int offset, int count, boolean dont_copy)
|
---|
117 | {
|
---|
118 | init(data, offset, count, dont_copy);
|
---|
119 | }
|
---|
120 |
|
---|
121 | public String (byte[] byteArray)
|
---|
122 | {
|
---|
123 | this (byteArray, 0, byteArray.length);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public String (byte[] byteArray, int offset, int count)
|
---|
127 | {
|
---|
128 | try
|
---|
129 | {
|
---|
130 | init (byteArray, offset, count,
|
---|
131 | System.getProperty("file.encoding", "8859_1"));
|
---|
132 | }
|
---|
133 | catch (UnsupportedEncodingException x1)
|
---|
134 | {
|
---|
135 | // Maybe the default encoding is bad.
|
---|
136 | try
|
---|
137 | {
|
---|
138 | init (byteArray, offset, count, "8859_1");
|
---|
139 | }
|
---|
140 | catch (UnsupportedEncodingException x2)
|
---|
141 | {
|
---|
142 | // We know this can't happen.
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | public String (byte[] byteArray, String enc)
|
---|
148 | throws UnsupportedEncodingException
|
---|
149 | {
|
---|
150 | this (byteArray, 0, byteArray.length, enc);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public String (byte[] byteArray, int offset, int count, String enc)
|
---|
154 | throws UnsupportedEncodingException
|
---|
155 | {
|
---|
156 | init (byteArray, offset, count, enc);
|
---|
157 | }
|
---|
158 |
|
---|
159 | public static String copyValueOf(char[] data)
|
---|
160 | {
|
---|
161 | return copyValueOf (data, 0, data.length);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public static String copyValueOf(char[] data, int offset, int count)
|
---|
165 | {
|
---|
166 | String r = new String ();
|
---|
167 | r.init(data, offset, count, false);
|
---|
168 | return r;
|
---|
169 | }
|
---|
170 |
|
---|
171 | /** @deprecated */
|
---|
172 | public String (byte[] ascii, int hibyte)
|
---|
173 | {
|
---|
174 | init(ascii, hibyte, 0, ascii.length);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /** @deprecated */
|
---|
178 | public String (byte[] ascii, int hibyte, int offset, int count)
|
---|
179 | {
|
---|
180 | init(ascii, hibyte, offset, count);
|
---|
181 | }
|
---|
182 |
|
---|
183 | public String toString ()
|
---|
184 | {
|
---|
185 | return this;
|
---|
186 | }
|
---|
187 |
|
---|
188 | public native boolean equals (Object anObject);
|
---|
189 |
|
---|
190 | public native int hashCode ();
|
---|
191 |
|
---|
192 | public int length ()
|
---|
193 | {
|
---|
194 | return count;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public native char charAt (int index);
|
---|
198 |
|
---|
199 | public native void getChars (int srcBegin, int srcEnd,
|
---|
200 | char[] dst, int dstBegin);
|
---|
201 |
|
---|
202 | public byte[] getBytes ()
|
---|
203 | {
|
---|
204 | try
|
---|
205 | {
|
---|
206 | return getBytes (System.getProperty("file.encoding", "8859_1"));
|
---|
207 | }
|
---|
208 | catch (UnsupportedEncodingException x)
|
---|
209 | {
|
---|
210 | // This probably shouldn't happen, but could if file.encoding
|
---|
211 | // is somehow changed to a value we don't understand.
|
---|
212 | try
|
---|
213 | {
|
---|
214 | return getBytes ("8859_1");
|
---|
215 | }
|
---|
216 | catch (UnsupportedEncodingException x2)
|
---|
217 | {
|
---|
218 | // This really shouldn't happen, because the 8859_1
|
---|
219 | // encoding should always be available.
|
---|
220 | throw new InternalError ("couldn't find 8859_1 encoder");
|
---|
221 | }
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | public native byte[] getBytes (String enc)
|
---|
226 | throws UnsupportedEncodingException;
|
---|
227 |
|
---|
228 | /** @deprecated */
|
---|
229 | public native void getBytes (int srcBegin, int srcEnd,
|
---|
230 | byte[] dst, int dstBegin);
|
---|
231 |
|
---|
232 | public native char[] toCharArray ();
|
---|
233 |
|
---|
234 | public native boolean equalsIgnoreCase (String anotherString);
|
---|
235 |
|
---|
236 | public native int compareTo (String anotherString);
|
---|
237 |
|
---|
238 | public int compareTo (Object obj)
|
---|
239 | {
|
---|
240 | return compareTo ((String)obj);
|
---|
241 | }
|
---|
242 |
|
---|
243 | public int compareToIgnoreCase (String str)
|
---|
244 | {
|
---|
245 | return this.toUpperCase().toLowerCase().compareTo(
|
---|
246 | str.toUpperCase().toLowerCase());
|
---|
247 | }
|
---|
248 |
|
---|
249 | public native boolean regionMatches (int toffset,
|
---|
250 | String other, int ooffset, int len);
|
---|
251 |
|
---|
252 | public native boolean regionMatches (boolean ignoreCase, int toffset,
|
---|
253 | String other, int ooffset, int len);
|
---|
254 |
|
---|
255 | public boolean startsWith (String prefix)
|
---|
256 | {
|
---|
257 | return startsWith (prefix, 0);
|
---|
258 | }
|
---|
259 |
|
---|
260 | public native boolean startsWith (String prefix, int toffset);
|
---|
261 |
|
---|
262 | public boolean endsWith (String suffix)
|
---|
263 | {
|
---|
264 | return regionMatches (this.count - suffix.count, suffix, 0, suffix.count);
|
---|
265 | }
|
---|
266 |
|
---|
267 | // No such method specified in the doc, including JDK 1.2.
|
---|
268 | // public boolean endsWith (String suffix, int toffset)
|
---|
269 | // {
|
---|
270 | // return regionMatches (toffset, suffix, 0, suffix.count);
|
---|
271 | // }
|
---|
272 |
|
---|
273 | // The Language Specification, and the JDK 1.2 API docs say that
|
---|
274 | // index and lastIndex take an int, while the Class Libraries
|
---|
275 | // say they take a char. The former wins ...
|
---|
276 |
|
---|
277 | public int indexOf (int ch)
|
---|
278 | {
|
---|
279 | return indexOf (ch, 0);
|
---|
280 | }
|
---|
281 |
|
---|
282 | public native int indexOf (int ch, int fromIndex);
|
---|
283 |
|
---|
284 | public int indexOf (String str)
|
---|
285 | {
|
---|
286 | return indexOf (str, 0);
|
---|
287 | }
|
---|
288 |
|
---|
289 | public native int indexOf (String str, int fromIndex);
|
---|
290 |
|
---|
291 | public int lastIndexOf (int ch)
|
---|
292 | {
|
---|
293 | return lastIndexOf (ch, count - 1);
|
---|
294 | }
|
---|
295 |
|
---|
296 | public native int lastIndexOf (int ch, int fromIndex);
|
---|
297 |
|
---|
298 | public int lastIndexOf (String str)
|
---|
299 | {
|
---|
300 | return lastIndexOf (str, count - str.count);
|
---|
301 | }
|
---|
302 |
|
---|
303 | public int lastIndexOf (String str, int fromIndex)
|
---|
304 | {
|
---|
305 | if (fromIndex >= count)
|
---|
306 | fromIndex = count - str.count;
|
---|
307 | for (;; --fromIndex)
|
---|
308 | {
|
---|
309 | if (fromIndex < 0)
|
---|
310 | return -1;
|
---|
311 | if (startsWith(str, fromIndex))
|
---|
312 | return fromIndex;
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Creates a substring of this String, starting at a specified index
|
---|
318 | * and ending at one character before a specified index.
|
---|
319 | * <p>
|
---|
320 | * To implement <code>CharSequence</code>.
|
---|
321 | * Calls <code>substring(beginIndex, endIndex)</code>.
|
---|
322 | *
|
---|
323 | * @param beginIndex index to start substring (base 0)
|
---|
324 | * @param endIndex index after the last character to be
|
---|
325 | * copied into the substring
|
---|
326 | *
|
---|
327 | * @return new String which is a substring of this String
|
---|
328 | *
|
---|
329 | * @exception StringIndexOutOfBoundsException
|
---|
330 | * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
|
---|
331 | */
|
---|
332 | public CharSequence subSequence(int beginIndex, int endIndex)
|
---|
333 | throws IndexOutOfBoundsException
|
---|
334 | {
|
---|
335 | return substring(beginIndex, endIndex);
|
---|
336 | }
|
---|
337 |
|
---|
338 | public String substring (int beginIndex)
|
---|
339 | {
|
---|
340 | return substring (beginIndex, count);
|
---|
341 | }
|
---|
342 |
|
---|
343 | public native String substring (int beginIndex, int endIndex);
|
---|
344 |
|
---|
345 | public native String concat (String str);
|
---|
346 |
|
---|
347 | public native String replace (char oldChar, char newChar);
|
---|
348 |
|
---|
349 | public native String toLowerCase (Locale locale);
|
---|
350 | public native String toUpperCase (Locale locale);
|
---|
351 |
|
---|
352 | public String toLowerCase ()
|
---|
353 | {
|
---|
354 | // The JDK is a bit confused about what to do here. If we pass in
|
---|
355 | // the default Locale then special Locale handling might be
|
---|
356 | // invoked. However, the docs also say that Character.toLowerCase
|
---|
357 | // rules here. We go with the latter.
|
---|
358 | return toLowerCase (null);
|
---|
359 | }
|
---|
360 |
|
---|
361 | public String toUpperCase ()
|
---|
362 | {
|
---|
363 | // The JDK is a bit confused about what to do here. If we pass in
|
---|
364 | // the default Locale then special Locale handling might be
|
---|
365 | // invoked. However, the docs also say that Character.toLowerCase
|
---|
366 | // rules here. We go with the latter.
|
---|
367 | return toUpperCase (null);
|
---|
368 | }
|
---|
369 |
|
---|
370 | public native String trim ();
|
---|
371 |
|
---|
372 | public static String valueOf (Object obj)
|
---|
373 | {
|
---|
374 | return obj == null ? "null" : obj.toString();
|
---|
375 | }
|
---|
376 |
|
---|
377 | public static String valueOf (char[] data)
|
---|
378 | {
|
---|
379 | return valueOf (data, 0, data.length);
|
---|
380 | }
|
---|
381 |
|
---|
382 | public static native String valueOf (char[] data, int offset, int count);
|
---|
383 |
|
---|
384 | public static String valueOf (boolean b)
|
---|
385 | {
|
---|
386 | return b ? "true" : "false";
|
---|
387 | }
|
---|
388 |
|
---|
389 | public static native String valueOf (char c);
|
---|
390 |
|
---|
391 | public static native String valueOf (int i);
|
---|
392 |
|
---|
393 | public static String valueOf (long l)
|
---|
394 | {
|
---|
395 | return Long.toString(l);
|
---|
396 | }
|
---|
397 |
|
---|
398 | public static String valueOf (float f)
|
---|
399 | {
|
---|
400 | return Float.toString(f);
|
---|
401 | }
|
---|
402 |
|
---|
403 | public static String valueOf (double d)
|
---|
404 | {
|
---|
405 | return Double.toString(d);
|
---|
406 | }
|
---|
407 |
|
---|
408 | public native String intern ();
|
---|
409 |
|
---|
410 | private native void init ();
|
---|
411 | private native void init (char[] chars, int offset, int count,
|
---|
412 | boolean dont_copy);
|
---|
413 | private native void init (byte[] chars, int hibyte, int offset, int count);
|
---|
414 | private native void init (byte[] chars, int offset, int count, String enc)
|
---|
415 | throws UnsupportedEncodingException;
|
---|
416 | private static native void rehash ();
|
---|
417 | }
|
---|