From 8f523f3a1047919d3563daf1ef47ba87336ebe89 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Tue, 15 Nov 2005 23:20:01 +0000 Subject: Imported GNU Classpath 0.19 + gcj-import-20051115. * sources.am: Regenerated. * Makefile.in: Likewise. * scripts/makemake.tcl: Use glob -nocomplain. From-SVN: r107049 --- libjava/classpath/java/lang/String.java | 207 ++++++++++++++++++++++++++++---- 1 file changed, 186 insertions(+), 21 deletions(-) (limited to 'libjava/classpath/java/lang/String.java') diff --git a/libjava/classpath/java/lang/String.java b/libjava/classpath/java/lang/String.java index b4db850..369d808 100644 --- a/libjava/classpath/java/lang/String.java +++ b/libjava/classpath/java/lang/String.java @@ -98,7 +98,7 @@ public final class String implements Serializable, Comparable, CharSequence /** * Stores unicode multi-character uppercase expansion table. - * @see #toUpperCase(char) + * @see #toUpperCase(Locale) * @see CharData#UPPER_EXPAND */ private static final char[] upperExpand @@ -139,7 +139,7 @@ public final class String implements Serializable, Comparable, CharSequence final int offset; /** - * An implementation for {@link CASE_INSENSITIVE_ORDER}. + * An implementation for {@link #CASE_INSENSITIVE_ORDER}. * This must be {@link Serializable}. The class name is dictated by * compatibility with Sun's JDK. */ @@ -233,6 +233,7 @@ public final class String implements Serializable, Comparable, CharSequence * @param count the number of characters from data to copy * @throws NullPointerException if data is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 + * || offset + count < 0 (overflow) * || offset + count > data.length) * (while unspecified, this is a StringIndexOutOfBoundsException) */ @@ -256,6 +257,7 @@ public final class String implements Serializable, Comparable, CharSequence * @param count the number of characters from ascii to copy * @throws NullPointerException if ascii is null * @throws IndexOutOfBoundsException if (offset < 0 || count < 0 + * || offset + count < 0 (overflow) * || offset + count > ascii.length) * (while unspecified, this is a StringIndexOutOfBoundsException) * @see #String(byte[]) @@ -267,8 +269,13 @@ public final class String implements Serializable, Comparable, CharSequence */ public String(byte[] ascii, int hibyte, int offset, int count) { - if (offset < 0 || count < 0 || offset + count > ascii.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > ascii.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); value = new char[count]; this.offset = 0; this.count = count; @@ -327,8 +334,13 @@ public final class String implements Serializable, Comparable, CharSequence public String(byte[] data, int offset, int count, String encoding) throws UnsupportedEncodingException { - if (offset < 0 || count < 0 || offset + count > data.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > data.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); try { CharsetDecoder csd = Charset.forName(encoding).newDecoder(); @@ -402,8 +414,13 @@ public final class String implements Serializable, Comparable, CharSequence */ public String(byte[] data, int offset, int count) { - if (offset < 0 || count < 0 || offset + count > data.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > data.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); int o, c; char[] v; String encoding; @@ -512,8 +529,13 @@ public final class String implements Serializable, Comparable, CharSequence */ String(char[] data, int offset, int count, boolean dont_copy) { - if (offset < 0 || count < 0 || offset + count > data.length) - throw new StringIndexOutOfBoundsException(); + if (offset < 0) + throw new StringIndexOutOfBoundsException("offset: " + offset); + if (count < 0) + throw new StringIndexOutOfBoundsException("count: " + count); + if (offset + count < 0 || offset + count > data.length) + throw new StringIndexOutOfBoundsException("offset + count: " + + (offset + count)); if (dont_copy) { value = data; @@ -554,6 +576,40 @@ public final class String implements Serializable, Comparable, CharSequence } /** + * Get the code point at the specified index. This is like #charAt(int), + * but if the character is the start of a surrogate pair, and the + * following character completes the pair, then the corresponding + * supplementary code point is returned. + * @param index the index of the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * @since 1.5 + */ + public synchronized int codePointAt(int index) + { + // Use the CharSequence overload as we get better range checking + // this way. + return Character.codePointAt(this, index); + } + + /** + * Get the code point before the specified index. This is like + * #codePointAt(int), but checks the characters at index-1 and + * index-2 to see if they form a supplementary code point. + * @param index the index just past the codepoint to get, starting at 0 + * @return the codepoint at the specified index + * @throws IndexOutOfBoundsException if index is negative or >= length() + * (while unspecified, this is a StringIndexOutOfBoundsException) + * @since 1.5 + */ + public synchronized int codePointBefore(int index) + { + // Use the CharSequence overload as we get better range checking + // this way. + return Character.codePointBefore(this, index); + } + + /** * Copies characters from this String starting at a specified start index, * ending at a specified stop index, to a character array starting at * a specified destination begin index. @@ -628,21 +684,26 @@ public final class String implements Serializable, Comparable, CharSequence ByteBuffer bbuf = cse.encode(CharBuffer.wrap(value, offset, count)); if(bbuf.hasArray()) return bbuf.array(); - + // Doubt this will happen. But just in case. byte[] bytes = new byte[bbuf.remaining()]; bbuf.get(bytes); return bytes; - - } catch(IllegalCharsetNameException e){ - throw new UnsupportedEncodingException("Encoding: "+enc+ - " not found."); - } catch(UnsupportedCharsetException e){ - throw new UnsupportedEncodingException("Encoding: "+enc+ - " not found."); - } catch(CharacterCodingException e){ - // XXX - Ignore coding exceptions? They shouldn't really happen. - return null; + } + catch(IllegalCharsetNameException e) + { + throw new UnsupportedEncodingException("Encoding: " + enc + + " not found."); + } + catch(UnsupportedCharsetException e) + { + throw new UnsupportedEncodingException("Encoding: " + enc + + " not found."); + } + catch(CharacterCodingException e) + { + // This shouldn't ever happen. + throw (InternalError) new InternalError().initCause(e); } } @@ -726,6 +787,26 @@ public final class String implements Serializable, Comparable, CharSequence } /** + * Compares the given CharSequence to this String. This is true if + * the CharSequence has the same content as this String at this + * moment. + * + * @param seq the CharSequence to compare to + * @return true if CharSequence has the same character sequence + * @throws NullPointerException if the given CharSequence is null + * @since 1.5 + */ + public boolean contentEquals(CharSequence seq) + { + if (seq.length() != count) + return false; + for (int i = 0; i < count; ++i) + if (value[offset + i] != seq.charAt(i)) + return false; + return true; + } + + /** * Compares a String to this String, ignoring case. This does not handle * multi-character capitalization exceptions; instead the comparison is * made on a character-by-character basis, and is true if: