diff options
author | Mark Wielaard <mark@gcc.gnu.org> | 2005-11-15 23:20:01 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2005-11-15 23:20:01 +0000 |
commit | 8f523f3a1047919d3563daf1ef47ba87336ebe89 (patch) | |
tree | a5eb7cf42a51869cc8aa1fad7ad6a90cca47fdd8 /libjava/classpath/java/lang/String.java | |
parent | 02e549bfaaec38f68307e7f34e46ea57ea1809af (diff) | |
download | gcc-8f523f3a1047919d3563daf1ef47ba87336ebe89.zip gcc-8f523f3a1047919d3563daf1ef47ba87336ebe89.tar.gz gcc-8f523f3a1047919d3563daf1ef47ba87336ebe89.tar.bz2 |
Imported GNU Classpath 0.19 + gcj-import-20051115.
* sources.am: Regenerated.
* Makefile.in: Likewise.
* scripts/makemake.tcl: Use glob -nocomplain.
From-SVN: r107049
Diffstat (limited to 'libjava/classpath/java/lang/String.java')
-rw-r--r-- | libjava/classpath/java/lang/String.java | 207 |
1 files changed, 186 insertions, 21 deletions
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 <code>index-1</code> and + * <code>index-2</code> 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:<br><ul> @@ -1546,6 +1627,7 @@ public final class String implements Serializable, Comparable, CharSequence * @return String containing the chars from data[offset..offset+count] * @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) * @see #String(char[], int, int) @@ -1566,6 +1648,7 @@ public final class String implements Serializable, Comparable, CharSequence * @return String containing the chars from data[offset..offset+count] * @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) * @see #String(char[], int, int) @@ -1677,6 +1760,49 @@ public final class String implements Serializable, Comparable, CharSequence } /** + * Return the number of code points between two indices in the + * <code>StringBuffer</code>. An unpaired surrogate counts as a + * code point for this purpose. Characters outside the indicated + * range are not examined, even if the range ends in the middle of a + * surrogate pair. + * + * @param start the starting index + * @param end one past the ending index + * @return the number of code points + * @since 1.5 + */ + public synchronized int codePointCount(int start, int end) + { + if (start < 0 || end >= count || start > end) + throw new StringIndexOutOfBoundsException(); + + start += offset; + end += offset; + int count = 0; + while (start < end) + { + char base = value[start]; + if (base < Character.MIN_HIGH_SURROGATE + || base > Character.MAX_HIGH_SURROGATE + || start == end + || start == count + || value[start + 1] < Character.MIN_LOW_SURROGATE + || value[start + 1] > Character.MAX_LOW_SURROGATE) + { + // Nothing. + } + else + { + // Surrogate pair. + ++start; + } + ++start; + ++count; + } + return count; + } + + /** * Helper function used to detect which characters have a multi-character * uppercase expansion. Note that this is only used in locations which * track one-to-many capitalization (java.lang.Character does not do this). @@ -1747,4 +1873,43 @@ public final class String implements Serializable, Comparable, CharSequence return value; } + + /** + * Returns true iff this String contains the sequence of Characters + * described in s. + * @param s the CharSequence + * @return true iff this String contains s + */ + public boolean contains (CharSequence s) + { + return this.indexOf(s.toString()) != -1; + } + + /** + * Returns a string that is this string with all instances of the sequence + * represented by <code>target</code> replaced by the sequence in + * <code>replacement</code>. + * @param target the sequence to be replaced + * @param replacement the sequence used as the replacement + * @return the string constructed as above + */ + public String replace (CharSequence target, CharSequence replacement) + { + String targetString = target.toString(); + String replaceString = replacement.toString(); + int targetLength = target.length(); + int replaceLength = replacement.length(); + + int startPos = this.indexOf(targetString); + StringBuilder result = new StringBuilder(this); + while (startPos != -1) + { + // Replace the target with the replacement + result.replace(startPos, startPos + targetLength, replaceString); + + // Search for a new occurrence of the target + startPos = result.indexOf(targetString, startPos + replaceLength); + } + return result.toString(); + } } |