diff options
Diffstat (limited to 'java/lang/String.java')
-rw-r--r-- | java/lang/String.java | 128 |
1 files changed, 115 insertions, 13 deletions
diff --git a/java/lang/String.java b/java/lang/String.java index b8a3a6627..74a9a61cb 100644 --- a/java/lang/String.java +++ b/java/lang/String.java @@ -101,7 +101,7 @@ public final class String /** * Stores unicode multi-character uppercase expansion table. - * @see #toUpperCase(char) + * @see #toUpperCase(Locale) * @see CharData#UPPER_EXPAND */ private static final char[] upperExpand @@ -142,7 +142,7 @@ public final class String 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. */ @@ -557,6 +557,40 @@ public final class String } /** + * 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. @@ -631,21 +665,26 @@ public final class String 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); } } @@ -729,6 +768,26 @@ public final class String } /** + * 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> @@ -1664,6 +1723,49 @@ public final class String } /** + * 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). |