diff options
author | Eric Blake <ebb9@email.byu.edu> | 2003-03-30 06:43:45 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2003-03-30 06:43:45 +0000 |
commit | 7270451f1955f9b853ec4f57649a315e8a96c516 (patch) | |
tree | 2743db1170c10ebb6bc778029f9a50d57990a31e /libjava/java/lang/String.java | |
parent | 9ba99c63ab5d93e5ab12888cfb44beb9940b234f (diff) | |
download | gcc-7270451f1955f9b853ec4f57649a315e8a96c516.zip gcc-7270451f1955f9b853ec4f57649a315e8a96c516.tar.gz gcc-7270451f1955f9b853ec4f57649a315e8a96c516.tar.bz2 |
natString.cc (hashCode): Use cachedHashCode.
2003-03-29 Eric Blake <ebb9@email.byu.edu>
Tom Tromey <tromey@redhat.com>
* java/lang/natString.cc (hashCode): Use cachedHashCode.
(init()): Removed.
(charAt): Put index in exception.
(contentEquals): New method.
Include StringBuffer.h.
* java/lang/String.java (cachedHashCode): New field.
(String()): Follow classpath implementation.
(init()): Removed.
(contentEquals): Declare.
(subSequence): Don't declare IndexOutIfBoundsException in throws
clause.
(matches, replaceFirst, replaceAll, split): New methods from
Classpath.
Co-Authored-By: Tom Tromey <tromey@redhat.com>
From-SVN: r65037
Diffstat (limited to 'libjava/java/lang/String.java')
-rw-r--r-- | libjava/java/lang/String.java | 146 |
1 files changed, 142 insertions, 4 deletions
diff --git a/libjava/java/lang/String.java b/libjava/java/lang/String.java index e10e7dd..29b9c45 100644 --- a/libjava/java/lang/String.java +++ b/libjava/java/lang/String.java @@ -44,6 +44,8 @@ import java.io.Serializable; import java.lang.Comparable; import java.util.Comparator; import java.util.Locale; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; /** * Strings represent an immutable set of characters. All String literals @@ -87,6 +89,12 @@ public final class String implements Serializable, Comparable, CharSequence int count; /** + * Caches the result of hashCode(). If this value is zero, the hashcode + * is considered uncached (even if 0 is the correct hash value). + */ + private int cachedHashCode; + + /** * An implementation for {@link CASE_INSENSITIVE_ORDER}. * This must be {@link Serializable}. The class name is dictated by * compatibility with Sun's JDK. @@ -137,9 +145,11 @@ public final class String implements Serializable, Comparable, CharSequence * Creates an empty String (length 0). Unless you really need a new object, * consider using <code>""</code> instead. */ - public String () + public String() { - init(); + data = "".data; + boffset = 0; + count = 0; } /** @@ -154,6 +164,7 @@ public final class String implements Serializable, Comparable, CharSequence data = str.data; boffset = str.boffset; count = str.count; + cachedHashCode = str.cachedHashCode; } /** @@ -511,6 +522,17 @@ public final class String implements Serializable, Comparable, CharSequence public native boolean equals (Object anObject); /** + * Compares the given StringBuffer to this String. This is true if the + * StringBuffer has the same content as this String at this moment. + * + * @param buffer the StringBuffer to compare to + * @return true if StringBuffer has the same character sequence + * @throws NullPointerException if the given StringBuffer is null + * @since 1.4 + */ + public native boolean contentEquals(StringBuffer buffer); + + /** * 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> @@ -815,7 +837,6 @@ public final class String implements Serializable, Comparable, CharSequence * @since 1.4 */ public CharSequence subSequence(int beginIndex, int endIndex) - throws IndexOutOfBoundsException { return substring(beginIndex, endIndex); } @@ -841,6 +862,124 @@ public final class String implements Serializable, Comparable, CharSequence public native String replace (char oldChar, char newChar); /** + * Test if this String matches a regular expression. This is shorthand for + * <code>{@link Pattern}.matches(regex, this)</code>. + * + * @param regex the pattern to match + * @return true if the pattern matches + * @throws NullPointerException if regex is null + * @throws PatternSyntaxException if regex is invalid + * @see Pattern#matches(String, CharSequence) + * @since 1.4 + */ + public boolean matches(String regex) + { + return Pattern.matches(regex, this); + } + + /** + * Replaces the first substring match of the regular expression with a + * given replacement. This is shorthand for <code>{@link Pattern} + * .compile(regex).matcher(this).replaceFirst(replacement)</code>. + * + * @param regex the pattern to match + * @param replacement the replacement string + * @return the modified string + * @throws NullPointerException if regex or replacement is null + * @throws PatternSyntaxException if regex is invalid + * @see #replaceAll(String, String) + * @see Pattern#compile(String) + * @see Pattern#matcher(CharSequence) + * @see Matcher#replaceFirst(String) + * @since 1.4 + */ + public String replaceFirst(String regex, String replacement) + { + return Pattern.compile(regex).matcher(this).replaceFirst(replacement); + } + + /** + * Replaces all matching substrings of the regular expression with a + * given replacement. This is shorthand for <code>{@link Pattern} + * .compile(regex).matcher(this).replaceAll(replacement)</code>. + * + * @param regex the pattern to match + * @param replacement the replacement string + * @return the modified string + * @throws NullPointerException if regex or replacement is null + * @throws PatternSyntaxException if regex is invalid + * @see #replaceFirst(String, String) + * @see Pattern#compile(String) + * @see Pattern#matcher(CharSequence) + * @see Matcher#replaceAll(String) + * @since 1.4 + */ + public String replaceAll(String regex, String replacement) + { + return Pattern.compile(regex).matcher(this).replaceAll(replacement); + } + + /** + * Split this string around the matches of a regular expression. Each + * element of the returned array is the largest block of characters not + * terminated by the regular expression, in the order the matches are found. + * + * <p>The limit affects the length of the array. If it is positive, the + * array will contain at most n elements (n - 1 pattern matches). If + * negative, the array length is unlimited, but there can be trailing empty + * entries. if 0, the array length is unlimited, and trailing empty entries + * are discarded. + * + * <p>For example, splitting "boo:and:foo" yields:<br> + * <table border=0> + * <th><td>Regex</td> <td>Limit</td> <td>Result</td></th> + * <tr><td>":"</td> <td>2</td> <td>{ "boo", "and:foo" }</td></tr> + * <tr><td>":"</td> <td>t</td> <td>{ "boo", "and", "foo" }</td></tr> + * <tr><td>":"</td> <td>-2</td> <td>{ "boo", "and", "foo" }</td></tr> + * <tr><td>"o"</td> <td>5</td> <td>{ "b", "", ":and:f", "", "" }</td></tr> + * <tr><td>"o"</td> <td>-2</td> <td>{ "b", "", ":and:f", "", "" }</td></tr> + * <tr><td>"o"</td> <td>0</td> <td>{ "b", "", ":and:f" }</td></tr> + * </table> + * + * <p>This is shorthand for + * <code>{@link Pattern}.compile(regex).split(this, limit)</code>. + * + * @param regex the pattern to match + * @param limit the limit threshold + * @return the array of split strings + * @throws NullPointerException if regex or replacement is null + * @throws PatternSyntaxException if regex is invalid + * @see Pattern#compile(String) + * @see Pattern#split(CharSequence, int) + * @since 1.4 + */ + public String[] split(String regex, int limit) + { + return Pattern.compile(regex).split(this, limit); + } + + /** + * Split this string around the matches of a regular expression. Each + * element of the returned array is the largest block of characters not + * terminated by the regular expression, in the order the matches are found. + * The array length is unlimited, and trailing empty entries are discarded, + * as though calling <code>split(regex, 0)</code>. + * + * @param regex the pattern to match + * @return the array of split strings + * @throws NullPointerException if regex or replacement is null + * @throws PatternSyntaxException if regex is invalid + * @see #split(String, int) + * @see Pattern#compile(String) + * @see Pattern#split(CharSequence, int) + * @since 1.4 + */ + public String[] split(String regex) + { + return Pattern.compile(regex).split(this, 0); + } + + /** * Lowercases this String according to a particular locale. This uses * Unicode's special case mappings, as applied to the given Locale, so the * resulting string may be a different length. @@ -1088,7 +1227,6 @@ public final class String implements Serializable, Comparable, CharSequence public native String intern (); - private native void init (); private native void init (char[] chars, int offset, int count, boolean dont_copy); private native void init (byte[] chars, int hibyte, int offset, int count); |