Hi,
The following patch adds 1.4 CharSequence support to String and StringBuffer.
It also adds one extra method to String and adds an implements Serializable.
2001-07-10 Mark Wielaard <[EMAIL PROTECTED]>
* java/lang/CharSequence.java: new 1.4 interface
* java/lang/String.java: implements Serializable, CharSequence
(contentEquals StringBuffer): new 1.4 method
(subSequence): new 1.4 method needed for CharSequence
* java/lang/StringBuffer.java: implements CharSequence
(subSequence): new 1.4 method needed for CharSequence
I did not add the new 1.4 split() methods to String. They need
java/util/regexp support. Maybe we can ask Wes Biggs if we may import
part of gnu.regexp to implement it. It is licensed under the LGPL, but
since it seems to be a GNU package we might get it relicensed under
GPL+exception for Classpath/libgcj.
Also not added are the new StringBuffer indexOf and lastIndexOf methods.
We share StringBuffer with libgcj and I couldn't come up with a method
of implementing this both efficiently and transparently since we don't
share the actual String implementation.
Cheers,
Mark
--
Stuff to read:
<http://www.toad.com/gnu/whatswrong.html>
What's Wrong with Copy Protection, by John Gilmore
Index: java/lang/CharSequence.java
===================================================================
RCS file: CharSequence.java
diff -N CharSequence.java
--- /dev/null Sat Apr 14 17:46:23 2001
+++ CharSequence.java Mon Jul 9 16:36:00 2001
@@ -0,0 +1,80 @@
+/* java.lang.CharSequence -- Anything that has an indexed sequence of chars
+ Copyright (C) 2001 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+As a special exception, if you link this library with other files to
+produce an executable, this library does not by itself cause the
+resulting executable to be covered by the GNU General Public License.
+This exception does not however invalidate any other reasons why the
+executable file might be covered by the GNU General Public License. */
+
+
+package java.lang;
+
+/**
+ * General functions on a sequence of chars. This interface is implemented
+ * by <code>String</code>, <code>StringBuffer</code> and
+ * <code>CharBuffer</code> to give a uniform way to get chars at a certain
+ * index, the number of characters in the sequence and a subrange of the
+ * chars. Indexes start at 0 and the last index is <code>length()-1</code>.
+ * <p>
+ * Even when classes implement this interface they are not always
+ * exchangeble because they might implement their compare, equals or hash
+ * function differently. This means that in general one should not use a
+ * <code>CharSequence</code> as keys in collections since two sequences
+ * with the same chars at the same indexes with the same length might not
+ * have the same hash code, be equal or be comparable since the are
+ * represented by different classes.
+ *
+ * @author Mark Wielaard ([EMAIL PROTECTED])
+ *
+ * @since 1.4
+ */
+
+public interface CharSequence {
+
+ /**
+ * Returns the character at the given index.
+ *
+ * @exception IndexOutOfBoundsException when <code>i < 0</code> or
+ * <code>i > length()-1</code>.
+ */
+ char charAt(int i);
+
+ /**
+ * Returns the length of the sequence.
+ */
+ int length();
+
+ /**
+ * Returns a new <code>CharSequence</char> of the indicated range.
+ *
+ * @exception IndexOutOfBoundsException when <code>begin < 0</code>,
+ * <code>end < 0</code>, <code>end > length()</code or
+ * <code>begin > end</code>
+ */
+ CharSequence subSequence(int begin, int end);
+
+ /**
+ * Returns the complete <code>CharSequence</code> as a <code>String</code>.
+ * Classes that implement this interface should return a <code>String</code>
+ * which contains only the characters in the sequence in the correct order.
+ */
+ String toString();
+}
Index: java/lang/String.java
===================================================================
RCS file: /cvs/classpath/java/lang/String.java,v
retrieving revision 1.30
diff -u -u -r1.30 String.java
--- java/lang/String.java 2001/01/07 23:00:31 1.30
+++ java/lang/String.java 2001/07/09 23:36:00
@@ -40,7 +40,7 @@
* @author Paul N. Fisher
* @since JDK1.0
*/
-public final class String implements Comparable {
+public final class String implements Comparable, CharSequence, Serializable {
/**
* Holds the references for each intern()'d String.
* Once a String has been intern()'d it cannot be GC'd.
@@ -355,6 +355,22 @@
}
/**
+ * Compares the given StringBuffer to this String.
+ *
+ * @return true if the given StringBuffer has the same character
+ * sequence as this String, else false
+ * @exception NullPointerException if the given StringBuffer is null
+ *
+ * @since 1.4
+ */
+ public boolean contentEquals(StringBuffer buffer) {
+ if (count != buffer.count) return false;
+ for (int i = 0; i < count; i++)
+ if (value[i] != buffer.value[i]) return false;
+ return true;
+ }
+
+ /**
* Computes the hashcode for this String, according to JLS, Appendix D.
*
* @return hashcode value of this String
@@ -846,6 +862,27 @@
char[] newStr = new char[endIndex-beginIndex];
System.arraycopy(value, beginIndex, newStr, 0, endIndex-beginIndex);
return new String(newStr);
+ }
+
+ /**
+ * Creates a substring of this String, starting at a specified index
+ * and ending at one character before a specified index.
+ * <p>
+ * To implement <code>CharSequence</code>.
+ * Calls <code>substring(beginIndex, endIndex)</code>.
+ *
+ * @param beginIndex index to start substring (base 0)
+ * @param endIndex index after the last character to be
+ * copied into the substring
+ *
+ * @return new String which is a substring of this String
+ *
+ * @exception StringIndexOutOfBoundsException
+ * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
+ */
+ public CharSequence subSequence(int beginIndex, int endIndex)
+ throws IndexOutOfBoundsException {
+ return substring(beginIndex, endIndex);
}
/**
Index: java/lang/StringBuffer.java
===================================================================
RCS file: /cvs/classpath/java/lang/StringBuffer.java,v
retrieving revision 1.14
diff -u -u -r1.14 StringBuffer.java
--- java/lang/StringBuffer.java 2001/01/07 22:47:28 1.14
+++ java/lang/StringBuffer.java 2001/07/09 23:36:01
@@ -1,5 +1,5 @@
/* StringBuffer.java -- Growable strings
- Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -73,7 +73,7 @@
* @author Tom Tromey
* @see java.lang.String
*/
-public final class StringBuffer implements Serializable
+public final class StringBuffer implements Serializable, CharSequence
{
/** Append the <code>String</code> value of the argument to this
<code>StringBuffer</code>.
* Uses <code>String.valueOf()</code> to convert to
@@ -217,7 +217,7 @@
* @param index the index of the character to get, starting at 0.
* @return the character at the specified index.
* @exception IndexOutOfBoundsException if the desired character index
- * is not between 0 and length() - 1 (inclusive).
+ * is negative or greater then length() - 1.
*/
public synchronized char charAt (int index)
{
@@ -671,6 +671,28 @@
// enable sharing here.
return new String (value, beginIndex, endIndex - beginIndex);
}
+
+ /**
+ * Creates a substring of this StringBuffer, starting at a specified index
+ * and ending at one character before a specified index.
+ * <p>
+ * To implement <code>CharSequence</code>.
+ * Calls <code>substring(beginIndex, endIndex)</code>.
+ *
+ * @param beginIndex index to start substring (base 0)
+ * @param endIndex index after the last character to be
+ * copied into the substring
+ *
+ * @return new String which is a substring of this StringBuffer
+ *
+ * @exception StringIndexOutOfBoundsException
+ * if (beginIndex < 0 || endIndex > this.length() || beginIndex > endIndex)
+ */
+ public CharSequence subSequence (int beginIndex, int endIndex)
+ {
+ return substring(beginIndex, endIndex);
+ }
+
/** Convert this <code>StringBuffer</code> to a <code>String</code>.
* @return the characters in this StringBuffer