]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
StringBuilder.java (appendCodePoint): New method.
authorTom Tromey <tromey@redhat.com>
Mon, 9 Jan 2006 06:42:19 +0000 (06:42 +0000)
committerTom Tromey <tromey@gcc.gnu.org>
Mon, 9 Jan 2006 06:42:19 +0000 (06:42 +0000)
* java/lang/StringBuilder.java (appendCodePoint): New method.
(insert): New overloads.
* java/lang/StringBuffer.java (StringBuffer): New constructor.
(charAt): Remerged javadoc.
(codePointAt, codePointBefore): New methods.
(appendCodePoint): New method.
(append): New overloads.
(insert): Likewise.
(trimToSize, codePointCount, offsetByCodePoints): New methods.
* java/lang/Float.java (SIZE): New field.
(valueOf): New method.
* java/lang/natDouble.cc (initIDs): Removed.
* java/lang/Double.java (static initializer): Removed.
(SIZE): New field.
(valueOf): New method.
(initIDs): Removed.

From-SVN: r109497

libjava/ChangeLog
libjava/java/lang/Double.java
libjava/java/lang/Float.java
libjava/java/lang/StringBuffer.java
libjava/java/lang/StringBuilder.java
libjava/java/lang/natDouble.cc

index 16c256836d0758edacfd26b45667b4e8d5177c63..3770c6716076dda653b29a1c51bce736c75dc672 100644 (file)
@@ -1,3 +1,22 @@
+2006-01-08  Tom Tromey  <tromey@redhat.com>
+
+       * java/lang/StringBuilder.java (appendCodePoint): New method.
+       (insert): New overloads.
+       * java/lang/StringBuffer.java (StringBuffer): New constructor.
+       (charAt): Remerged javadoc.
+       (codePointAt, codePointBefore): New methods.
+       (appendCodePoint): New method.
+       (append): New overloads.
+       (insert): Likewise.
+       (trimToSize, codePointCount, offsetByCodePoints): New methods.
+       * java/lang/Float.java (SIZE): New field.
+       (valueOf): New method.
+       * java/lang/natDouble.cc (initIDs): Removed.
+       * java/lang/Double.java (static initializer): Removed.
+       (SIZE): New field.
+       (valueOf): New method.
+       (initIDs): Removed.
+
 2006-01-07  Jakub Jelinek  <jakub@redhat.com>
 
        PR libgcj/24940
index f6235d5b65c5c877f1d22473f2eb9d43abb0eda5..92f8a230822657eda4c8b1ecb322bc88262a2593 100644 (file)
@@ -1,5 +1,5 @@
 /* Double.java -- object wrapper for double
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -38,7 +38,6 @@ exception statement from your version. */
 
 package java.lang;
 
-import gnu.classpath.Configuration;
 
 /**
  * Instances of class <code>Double</code> represent primitive
@@ -88,6 +87,12 @@ public final class Double extends Number implements Comparable
    */
   public static final double NaN = 0.0 / 0.0;
 
+  /**
+   * The number of bits needed to represent a <code>double</code>.
+   * @since 1.5
+   */
+  public static final int SIZE = 64;
+
   /**
    * The primitive type <code>double</code> is represented by this
    * <code>Class</code> object.
@@ -102,18 +107,6 @@ public final class Double extends Number implements Comparable
    */
   private final double value;
 
-  /**
-   * Load native routines necessary for this class.
-   */
-  static
-  {
-    if (Configuration.INIT_LOAD_LIBRARY)
-      {
-       System.loadLibrary("javalang");
-       initIDs();
-      }
-  }
-
   /**
    * Create a <code>Double</code> from the primitive <code>double</code>
    * specified.
@@ -179,6 +172,22 @@ public final class Double extends Number implements Comparable
     return toString(d, false);
   }
 
+  /**
+   * Returns a <code>Double</code> object wrapping the value.
+   * In contrast to the <code>Double</code> constructor, this method
+   * may cache some values.  It is used by boxing conversion.
+   *
+   * @param val the value to wrap
+   * @return the <code>Double</code>
+   * 
+   * @since 1.5
+   */
+  public static Double valueOf(double val)
+  {
+    // We don't actually cache, but we could.
+    return new Double(val);
+  }
+
   /**
    * Create a new <code>Double</code> object using the <code>String</code>.
    *
@@ -534,10 +543,4 @@ public final class Double extends Number implements Comparable
    */
   // Package visible for use by Float.
   static native String toString(double d, boolean isFloat);
-
-  /**
-   * Initialize JNI cache.  This method is called only by the
-   * static initializer when using JNI.
-   */
-  private static native void initIDs();
 }
index b2c84c7565590d553fdec9b0372ec4516cb75e65..7677ca4132ee57799354da74f600712434c4aba7 100644 (file)
@@ -1,5 +1,5 @@
 /* Float.java -- object wrapper for float
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -93,6 +93,12 @@ public final class Float extends Number implements Comparable
    */
   public static final Class TYPE = VMClassLoader.getPrimitiveClass('F');
 
+  /**
+   * The number of bits needed to represent a <code>float</code>.
+   * @since 1.5
+   */
+  public static final int SIZE = 32;
+
   /**
    * The immutable value of this Float.
    *
@@ -191,6 +197,22 @@ public final class Float extends Number implements Comparable
     return new Float(parseFloat(s));
   }
 
+  /**
+   * Returns a <code>Float</code> object wrapping the value.
+   * In contrast to the <code>Float</code> constructor, this method
+   * may cache some values.  It is used by boxing conversion.
+   *
+   * @param val the value to wrap
+   * @return the <code>Float</code>
+   * 
+   * @since 1.5
+   */
+  public static Float valueOf(float val)
+  {
+    // We don't actually cache, but we could.
+    return new Float(val);
+  }
+
   /**
    * Parse the specified <code>String</code> as a <code>float</code>. The
    * extended BNF grammar is as follows:<br>
index d93fed5fd40ac7115a82f313511021aecb0906c8..c3f112967c415dec847d5e523e747bedb1e77b45 100644 (file)
@@ -1,5 +1,5 @@
 /* StringBuffer.java -- Growable strings
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -147,6 +147,24 @@ public final class StringBuffer implements Serializable, CharSequence
     str.getChars(0, count, value, 0);
   }
 
+  /**
+   * Create a new <code>StringBuffer</code> with the characters from the
+   * specified <code>CharSequence</code>. Initial capacity will be the
+   * size of the CharSequence plus 16.
+   *
+   * @param sequence the <code>String</code> to convert
+   * @throws NullPointerException if str is null
+   *
+   * @since 1.5
+   */
+  public StringBuffer(CharSequence sequence)
+  {
+    count = Math.max(0, sequence.length());
+    value = new char[count + DEFAULT_CAPACITY];
+    for (int i = 0; i < count; ++i)
+      value[i] = sequence.charAt(i);
+  }
+
   /**
    * Get the length of the <code>String</code> this <code>StringBuffer</code>
    * would create. Not to be confused with the <em>capacity</em> of the
@@ -234,7 +252,6 @@ public final class StringBuffer implements Serializable, CharSequence
    * @param index the index of the character to get, starting at 0
    * @return the character at the specified index
    * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
-   *         (while unspecified, this is a StringIndexOutOfBoundsException)
    */
   public synchronized char charAt(int index)
   {
@@ -243,6 +260,39 @@ public final class StringBuffer implements Serializable, CharSequence
     return value[index];
   }
 
+  /**
+   * 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 &gt;= length()
+   * @since 1.5
+   */
+  public synchronized int codePointAt(int index)
+  {
+    return Character.codePointAt(value, index, count);
+  }
+
+  /**
+   * 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 &gt;= length()
+   * @since 1.5
+   */
+  public synchronized int codePointBefore(int index)
+  {
+    // Character.codePointBefore() doesn't perform this check.  We
+    // could use the CharSequence overload, but this is just as easy.
+    if (index >= count)
+      throw new IndexOutOfBoundsException();
+    return Character.codePointBefore(value, index, 1);
+  }
+
   /**
    * Get the specified array of characters. <code>srcOffset - srcEnd</code>
    * characters will be copied into the array you pass in.
@@ -340,6 +390,46 @@ public final class StringBuffer implements Serializable, CharSequence
     return this;
   }
 
+  /**
+   * Append the <code>CharSequence</code> value of the argument to this
+   * <code>StringBuffer</code>.
+   *
+   * @param sequence the <code>CharSequence</code> to append
+   * @return this <code>StringBuffer</code>
+   * @see #append(Object)
+   * @since 1.5
+   */
+  public synchronized StringBuffer append(CharSequence sequence)
+  {
+    if (sequence == null)
+      sequence = "null";
+    return append(sequence, 0, sequence.length());
+  }
+
+  /**
+   * Append the specified subsequence of the <code>CharSequence</code>
+   * argument to this <code>StringBuffer</code>.
+   *
+   * @param sequence the <code>CharSequence</code> to append
+   * @param start the starting index
+   * @param end one past the ending index
+   * @return this <code>StringBuffer</code>
+   * @see #append(Object)
+   * @since 1.5
+   */
+  public synchronized StringBuffer append(CharSequence sequence,
+                                         int start, int end)
+  {
+    if (sequence == null)
+      sequence = "null";
+    if (start < 0 || end < 0 || start > end || end > sequence.length())
+      throw new IndexOutOfBoundsException();
+    ensureCapacity_unsynchronized(this.count + end - start);
+    for (int i = start; i < end; ++i)
+      value[count++] = sequence.charAt(i);
+    return this;
+  }
+
   /**
    * Append the <code>char</code> array to this <code>StringBuffer</code>.
    * This is similar (but more efficient) than
@@ -406,6 +496,25 @@ public final class StringBuffer implements Serializable, CharSequence
     return this;
   }
 
+  /**
+   * Append the code point to this <code>StringBuffer</code>.
+   * This is like #append(char), but will append two characters
+   * if a supplementary code point is given.
+   *
+   * @param code the code point to append
+   * @return this <code>StringBuffer</code>
+   * @see Character#toChars(int, char[], int)
+   * @since 1.5
+   */
+  public synchronized StringBuffer appendCodePoint(int code)
+  {
+    int len = Character.charCount(code);
+    ensureCapacity_unsynchronized(count + len);
+    Character.toChars(code, value, count);
+    count += len;
+    return this;
+  }
+
   /**
    * Append the <code>String</code> value of the argument to this
    * <code>StringBuffer</code>. Uses <code>String.valueOf()</code> to convert
@@ -656,6 +765,54 @@ public final class StringBuffer implements Serializable, CharSequence
     return this;
   }
 
+  /**
+   * Insert the <code>CharSequence</code> argument into this
+   * <code>StringBuffer</code>.  If the sequence is null, the String
+   * "null" is used instead.
+   *
+   * @param offset the place to insert in this buffer
+   * @param sequence the <code>CharSequence</code> to insert
+   * @return this <code>StringBuffer</code>
+   * @throws IndexOutOfBoundsException if offset is out of bounds
+   * @since 1.5
+   */
+  public synchronized StringBuffer insert(int offset, CharSequence sequence)
+  {
+    if (sequence == null)
+      sequence = "null";
+    return insert(offset, sequence, 0, sequence.length());
+  }
+
+  /**
+   * Insert a subsequence of the <code>CharSequence</code> argument into this
+   * <code>StringBuffer</code>.  If the sequence is null, the String
+   * "null" is used instead.
+   *
+   * @param offset the place to insert in this buffer
+   * @param sequence the <code>CharSequence</code> to insert
+   * @param start the starting index of the subsequence
+   * @param end one past the ending index of the subsequence
+   * @return this <code>StringBuffer</code>
+   * @throws IndexOutOfBoundsException if offset, start,
+   * or end are out of bounds
+   * @since 1.5
+   */
+  public synchronized StringBuffer insert(int offset, CharSequence sequence,
+                                         int start, int end)
+  {
+    if (sequence == null)
+      sequence = "null";
+    if (start < 0 || end < 0 || start > end || end > sequence.length())
+      throw new IndexOutOfBoundsException();
+    int len = end - start;
+    ensureCapacity_unsynchronized(count + len);
+    System.arraycopy(value, offset, value, offset + len, count - offset);
+    for (int i = start; i < end; ++i)
+      value[offset++] = sequence.charAt(i);
+    count += len;
+    return this;
+  }
+
   /**
    * Insert the <code>char[]</code> argument into this
    * <code>StringBuffer</code>.
@@ -876,6 +1033,106 @@ public final class StringBuffer implements Serializable, CharSequence
     return new String(this);
   }
 
+  /**
+   * This may reduce the amount of memory used by the StringBuffer,
+   * by resizing the internal array to remove unused space.  However,
+   * this method is not required to resize, so this behavior cannot
+   * be relied upon.
+   * @since 1.5
+   */
+  public synchronized void trimToSize()
+  {
+    int wouldSave = value.length - count;
+    // Some random heuristics: if we save less than 20 characters, who
+    // cares.
+    if (wouldSave < 20)
+      return;
+    // If we save more than 200 characters, shrink.
+    // If we save more than 1/4 of the buffer, shrink.
+    if (wouldSave > 200 || wouldSave * 4 > value.length)
+      {
+       char[] newValue = new char[count];
+       System.arraycopy(value, 0, newValue, 0, count);
+       value = newValue;
+      }
+  }
+
+  /**
+   * 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();
+
+    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;
+  }
+
+  /**
+   * Starting at the given index, this counts forward by the indicated
+   * number of code points, and then returns the resulting index.  An
+   * unpaired surrogate counts as a single code point for this
+   * purpose.
+   *
+   * @param start the starting index
+   * @param codePoints the number of code points
+   * @return the resulting index
+   * @since 1.5
+   */
+  public synchronized int offsetByCodePoints(int start, int codePoints)
+  {
+    while (codePoints > 0)
+      {
+       char base = value[start];
+       if (base < Character.MIN_HIGH_SURROGATE
+           || base > Character.MAX_HIGH_SURROGATE
+           || start == count
+           || value[start + 1] < Character.MIN_LOW_SURROGATE
+           || value[start + 1] > Character.MAX_LOW_SURROGATE)
+         {
+           // Nothing.
+         }
+       else
+         {
+           // Surrogate pair.
+           ++start;
+         }
+       ++start;
+       --codePoints;
+      }
+    return start;
+  }
+
   /**
    * An unsynchronized version of ensureCapacity, used internally to avoid
    * the cost of a second lock on the same object. This also has the side
index 51df8826416ff4f09db41b5156f339d233e83cac..5990a6d8dd5e6c6cdda2b86c3e9ec1035d72a1e6 100644 (file)
@@ -1,5 +1,5 @@
 /* StringBuilder.java -- Unsynchronized growable strings
-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
    Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
@@ -463,6 +463,25 @@ public final class StringBuilder
     return this;
   }
 
+  /**
+   * Append the code point to this <code>StringBuilder</code>.
+   * This is like #append(char), but will append two characters
+   * if a supplementary code point is given.
+   *
+   * @param code the code point to append
+   * @return this <code>StringBuilder</code>
+   * @see Character#toChars(int, char[], int)
+   * @since 1.5
+   */
+  public synchronized StringBuilder appendCodePoint(int code)
+  {
+    int len = Character.charCount(code);
+    ensureCapacity(count + len);
+    Character.toChars(code, value, count);
+    count += len;
+    return this;
+  }
+
   /**
    * Append the <code>String</code> value of the argument to this
    * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
@@ -704,6 +723,52 @@ public final class StringBuilder
     return this;
   }
 
+  /**
+   * Insert the <code>CharSequence</code> argument into this
+   * <code>StringBuilder</code>.  If the sequence is null, the String
+   * "null" is used instead.
+   *
+   * @param offset the place to insert in this buffer
+   * @param sequence the <code>CharSequence</code> to insert
+   * @return this <code>StringBuilder</code>
+   * @throws IndexOutOfBoundsException if offset is out of bounds
+   */
+  public synchronized StringBuilder insert(int offset, CharSequence sequence)
+  {
+    if (sequence == null)
+      sequence = "null";
+    return insert(offset, sequence, 0, sequence.length());
+  }
+
+  /**
+   * Insert a subsequence of the <code>CharSequence</code> argument into this
+   * <code>StringBuilder</code>.  If the sequence is null, the String
+   * "null" is used instead.
+   *
+   * @param offset the place to insert in this buffer
+   * @param sequence the <code>CharSequence</code> to insert
+   * @param start the starting index of the subsequence
+   * @param end one past the ending index of the subsequence
+   * @return this <code>StringBuilder</code>
+   * @throws IndexOutOfBoundsException if offset, start,
+   * or end are out of bounds
+   */
+  public synchronized StringBuilder insert(int offset, CharSequence sequence,
+                      int start, int end)
+  {
+    if (sequence == null)
+      sequence = "null";
+    if (start < 0 || end < 0 || start > end || end > sequence.length())
+      throw new IndexOutOfBoundsException();
+    int len = end - start;
+    ensureCapacity(count + len);
+    System.arraycopy(value, offset, value, offset + len, count - offset);
+    for (int i = start; i < end; ++i)
+      value[offset++] = sequence.charAt(i);
+    count += len;
+    return this;
+  }
+
   /**
    * Insert the <code>char[]</code> argument into this
    * <code>StringBuilder</code>.
index 72fe5fbe1c64f38f240a17c6f85f087eaa801dfe..24dad8a6318b3a5ce98ded727240491bdeb37099 100644 (file)
@@ -1,6 +1,6 @@
 // natDouble.cc - Implementation of java.lang.Double native methods.
 
-/* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -211,9 +211,3 @@ java::lang::Double::parseDouble(jstring str)
     }
   throw new NumberFormatException(str);
 }
-
-void
-java::lang::Double::initIDs()
-{
-  // Not used in libgcj
-}