| 1 | /* Copyright (C) 1998, 1999, 2000, 2001 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 <[email protected]>
|
|---|
| 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 | public String (byte[] byteArray)
|
|---|
| 116 | {
|
|---|
| 117 | this (byteArray, 0, byteArray.length);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | public String (byte[] byteArray, int offset, int count)
|
|---|
| 121 | {
|
|---|
| 122 | try
|
|---|
| 123 | {
|
|---|
| 124 | init (byteArray, offset, count,
|
|---|
| 125 | System.getProperty("file.encoding", "8859_1"));
|
|---|
| 126 | }
|
|---|
| 127 | catch (UnsupportedEncodingException x1)
|
|---|
| 128 | {
|
|---|
| 129 | // Maybe the default encoding is bad.
|
|---|
| 130 | try
|
|---|
| 131 | {
|
|---|
| 132 | init (byteArray, offset, count, "8859_1");
|
|---|
| 133 | }
|
|---|
| 134 | catch (UnsupportedEncodingException x2)
|
|---|
| 135 | {
|
|---|
| 136 | // We know this can't happen.
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public String (byte[] byteArray, String enc)
|
|---|
| 142 | throws UnsupportedEncodingException
|
|---|
| 143 | {
|
|---|
| 144 | this (byteArray, 0, byteArray.length, enc);
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | public String (byte[] byteArray, int offset, int count, String enc)
|
|---|
| 148 | throws UnsupportedEncodingException
|
|---|
| 149 | {
|
|---|
| 150 | init (byteArray, offset, count, enc);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | public static String copyValueOf(char[] data)
|
|---|
| 154 | {
|
|---|
| 155 | return copyValueOf (data, 0, data.length);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | public static String copyValueOf(char[] data, int offset, int count)
|
|---|
| 159 | {
|
|---|
| 160 | String r = new String ();
|
|---|
| 161 | r.init(data, offset, count, false);
|
|---|
| 162 | return r;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** @deprecated */
|
|---|
| 166 | public String (byte[] ascii, int hibyte)
|
|---|
| 167 | {
|
|---|
| 168 | init(ascii, hibyte, 0, ascii.length);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** @deprecated */
|
|---|
| 172 | public String (byte[] ascii, int hibyte, int offset, int count)
|
|---|
| 173 | {
|
|---|
| 174 | init(ascii, hibyte, offset, count);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | public String toString ()
|
|---|
| 178 | {
|
|---|
| 179 | return this;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | public native boolean equals (Object anObject);
|
|---|
| 183 |
|
|---|
| 184 | public native int hashCode ();
|
|---|
| 185 |
|
|---|
| 186 | public int length ()
|
|---|
| 187 | {
|
|---|
| 188 | return count;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | public native char charAt (int index);
|
|---|
| 192 |
|
|---|
| 193 | public native void getChars (int srcBegin, int srcEnd,
|
|---|
| 194 | char[] dst, int dstBegin);
|
|---|
| 195 |
|
|---|
| 196 | public byte[] getBytes ()
|
|---|
| 197 | {
|
|---|
| 198 | try
|
|---|
| 199 | {
|
|---|
| 200 | return getBytes (System.getProperty("file.encoding", "8859_1"));
|
|---|
| 201 | }
|
|---|
| 202 | catch (UnsupportedEncodingException x)
|
|---|
| 203 | {
|
|---|
| 204 | // This probably shouldn't happen, but could if file.encoding
|
|---|
| 205 | // is somehow changed to a value we don't understand.
|
|---|
| 206 | try
|
|---|
| 207 | {
|
|---|
| 208 | return getBytes ("8859_1");
|
|---|
| 209 | }
|
|---|
| 210 | catch (UnsupportedEncodingException x2)
|
|---|
| 211 | {
|
|---|
| 212 | // This really shouldn't happen, because the 8859_1
|
|---|
| 213 | // encoding should always be available.
|
|---|
| 214 | throw new InternalError ("couldn't find 8859_1 encoder");
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | public native byte[] getBytes (String enc)
|
|---|
| 220 | throws UnsupportedEncodingException;
|
|---|
| 221 |
|
|---|
| 222 | /** @deprecated */
|
|---|
| 223 | public native void getBytes (int srcBegin, int srcEnd,
|
|---|
| 224 | byte[] dst, int dstBegin);
|
|---|
| 225 |
|
|---|
| 226 | public native char[] toCharArray ();
|
|---|
| 227 |
|
|---|
| 228 | public native boolean equalsIgnoreCase (String anotherString);
|
|---|
| 229 |
|
|---|
| 230 | public native int compareTo (String anotherString);
|
|---|
| 231 |
|
|---|
| 232 | public int compareTo (Object obj)
|
|---|
| 233 | {
|
|---|
| 234 | return compareTo ((String)obj);
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | public int compareToIgnoreCase (String str)
|
|---|
| 238 | {
|
|---|
| 239 | return this.toUpperCase().toLowerCase().compareTo(
|
|---|
| 240 | str.toUpperCase().toLowerCase());
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | public native boolean regionMatches (int toffset,
|
|---|
| 244 | String other, int ooffset, int len);
|
|---|
| 245 |
|
|---|
| 246 | public native boolean regionMatches (boolean ignoreCase, int toffset,
|
|---|
| 247 | String other, int ooffset, int len);
|
|---|
| 248 |
|
|---|
| 249 | public boolean startsWith (String prefix)
|
|---|
| 250 | {
|
|---|
| 251 | return startsWith (prefix, 0);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | public native boolean startsWith (String prefix, int toffset);
|
|---|
| 255 |
|
|---|
| 256 | public boolean endsWith (String suffix)
|
|---|
| 257 | {
|
|---|
| 258 | return regionMatches (this.count - suffix.count, suffix, 0, suffix.count);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | // No such method specified in the doc, including JDK 1.2.
|
|---|
| 262 | // public boolean endsWith (String suffix, int toffset)
|
|---|
| 263 | // {
|
|---|
| 264 | // return regionMatches (toffset, suffix, 0, suffix.count);
|
|---|
| 265 | // }
|
|---|
| 266 |
|
|---|
| 267 | // The Language Specification, and the JDK 1.2 API docs say that
|
|---|
| 268 | // index and lastIndex take an int, while the Class Libraries
|
|---|
| 269 | // say they take a char. The former wins ...
|
|---|
| 270 |
|
|---|
| 271 | public int indexOf (int ch)
|
|---|
| 272 | {
|
|---|
| 273 | return indexOf (ch, 0);
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | public native int indexOf (int ch, int fromIndex);
|
|---|
| 277 |
|
|---|
| 278 | public int indexOf (String str)
|
|---|
| 279 | {
|
|---|
| 280 | return indexOf (str, 0);
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | public native int indexOf (String str, int fromIndex);
|
|---|
| 284 |
|
|---|
| 285 | public int lastIndexOf (int ch)
|
|---|
| 286 | {
|
|---|
| 287 | return lastIndexOf (ch, count - 1);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | public native int lastIndexOf (int ch, int fromIndex);
|
|---|
| 291 |
|
|---|
| 292 | public int lastIndexOf (String str)
|
|---|
| 293 | {
|
|---|
| 294 | return lastIndexOf (str, count - str.count);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | public int lastIndexOf (String str, int fromIndex)
|
|---|
| 298 | {
|
|---|
| 299 | if (fromIndex >= count)
|
|---|
| 300 | fromIndex = count - str.count;
|
|---|
| 301 | for (;; --fromIndex)
|
|---|
| 302 | {
|
|---|
| 303 | if (fromIndex < 0)
|
|---|
| 304 | return -1;
|
|---|
| 305 | if (startsWith(str, fromIndex))
|
|---|
| 306 | return fromIndex;
|
|---|
| 307 | }
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | /**
|
|---|
| 311 | * Creates a substring of this String, starting at a specified index
|
|---|
| 312 | * and ending at one character before a specified index.
|
|---|
| 313 | * <p>
|
|---|
| 314 | * To implement <code>CharSequence</code>.
|
|---|
| 315 | * Calls <code>substring(beginIndex, endIndex)</code>.
|
|---|
| 316 | *
|
|---|
| 317 | * @param beginIndex index to start substring (base 0)
|
|---|
| 318 | * @param endIndex index after the last character to be
|
|---|
| 319 | * copied into the substring
|
|---|
| 320 | *
|
|---|
| 321 | * @return new String which is a substring of this String
|
|---|
| 322 | *
|
|---|
| 323 | * @exception StringIndexOutOfBoundsException
|
|---|
| 324 | * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
|
|---|
| 325 | */
|
|---|
| 326 | public CharSequence subSequence(int beginIndex, int endIndex)
|
|---|
| 327 | throws IndexOutOfBoundsException
|
|---|
| 328 | {
|
|---|
| 329 | return substring(beginIndex, endIndex);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | public String substring (int beginIndex)
|
|---|
| 333 | {
|
|---|
| 334 | return substring (beginIndex, count);
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | public native String substring (int beginIndex, int endIndex);
|
|---|
| 338 |
|
|---|
| 339 | public native String concat (String str);
|
|---|
| 340 |
|
|---|
| 341 | public native String replace (char oldChar, char newChar);
|
|---|
| 342 |
|
|---|
| 343 | public native String toLowerCase (Locale locale);
|
|---|
| 344 | public native String toUpperCase (Locale locale);
|
|---|
| 345 |
|
|---|
| 346 | public String toLowerCase ()
|
|---|
| 347 | {
|
|---|
| 348 | // The JDK is a bit confused about what to do here. If we pass in
|
|---|
| 349 | // the default Locale then special Locale handling might be
|
|---|
| 350 | // invoked. However, the docs also say that Character.toLowerCase
|
|---|
| 351 | // rules here. We go with the latter.
|
|---|
| 352 | return toLowerCase (null);
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | public String toUpperCase ()
|
|---|
| 356 | {
|
|---|
| 357 | // The JDK is a bit confused about what to do here. If we pass in
|
|---|
| 358 | // the default Locale then special Locale handling might be
|
|---|
| 359 | // invoked. However, the docs also say that Character.toLowerCase
|
|---|
| 360 | // rules here. We go with the latter.
|
|---|
| 361 | return toUpperCase (null);
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | public native String trim ();
|
|---|
| 365 |
|
|---|
| 366 | public static String valueOf (Object obj)
|
|---|
| 367 | {
|
|---|
| 368 | return obj == null ? "null" : obj.toString();
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | public static String valueOf (char[] data)
|
|---|
| 372 | {
|
|---|
| 373 | return valueOf (data, 0, data.length);
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | public static native String valueOf (char[] data, int offset, int count);
|
|---|
| 377 |
|
|---|
| 378 | public static String valueOf (boolean b)
|
|---|
| 379 | {
|
|---|
| 380 | return b ? "true" : "false";
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | public static native String valueOf (char c);
|
|---|
| 384 |
|
|---|
| 385 | public static native String valueOf (int i);
|
|---|
| 386 |
|
|---|
| 387 | public static String valueOf (long l)
|
|---|
| 388 | {
|
|---|
| 389 | return Long.toString(l);
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | public static String valueOf (float f)
|
|---|
| 393 | {
|
|---|
| 394 | return Float.toString(f);
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | public static String valueOf (double d)
|
|---|
| 398 | {
|
|---|
| 399 | return Double.toString(d);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | public native String intern ();
|
|---|
| 403 |
|
|---|
| 404 | private native void init ();
|
|---|
| 405 | private native void init (char[] chars, int offset, int count,
|
|---|
| 406 | boolean dont_copy);
|
|---|
| 407 | private native void init (byte[] chars, int hibyte, int offset, int count);
|
|---|
| 408 | private native void init (byte[] chars, int offset, int count, String enc)
|
|---|
| 409 | throws UnsupportedEncodingException;
|
|---|
| 410 | private static native void rehash ();
|
|---|
| 411 | }
|
|---|