diff options
author | Tom Tromey <tromey@redhat.com> | 2001-12-11 18:01:40 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2001-12-11 18:01:40 +0000 |
commit | 50cc5dd29b71f5fcbe10cb424177ecc4d72e95d7 (patch) | |
tree | 036eb3d4668ff454b9f53f21377f4d5495ee21da /libjava/gnu | |
parent | 34026199b380249a3de9661cfea29e5e90252d4d (diff) | |
download | gcc-50cc5dd29b71f5fcbe10cb424177ecc4d72e95d7.zip gcc-50cc5dd29b71f5fcbe10cb424177ecc4d72e95d7.tar.gz gcc-50cc5dd29b71f5fcbe10cb424177ecc4d72e95d7.tar.bz2 |
String.java (String): New constructor.
* java/lang/String.java (String): New constructor.
* gnu/gcj/runtime/natStringBuffer.cc: New file.
* gnu/gcj/runtime/StringBuffer.java: New file.
* Makefile.in: Rebuilt.
* Makefile.am (ordinary_java_source_files): Added
gnu/gcj/runtime/StringBuffer.java.
(nat_source_files): Added gnu/gcj/runtime/natStringBuffer.cc.
From-SVN: r47883
Diffstat (limited to 'libjava/gnu')
-rw-r--r-- | libjava/gnu/gcj/runtime/StringBuffer.java | 188 | ||||
-rw-r--r-- | libjava/gnu/gcj/runtime/natStringBuffer.cc | 36 |
2 files changed, 224 insertions, 0 deletions
diff --git a/libjava/gnu/gcj/runtime/StringBuffer.java b/libjava/gnu/gcj/runtime/StringBuffer.java new file mode 100644 index 0000000..403f952 --- /dev/null +++ b/libjava/gnu/gcj/runtime/StringBuffer.java @@ -0,0 +1,188 @@ +// This is a simplified copy of java.lang.StringBuffer with +// `synchronized' removed. + +/* StringBuffer.java -- Growable strings + Copyright (C) 1998, 1999, 2000, 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 gnu.gcj.runtime; + +public final class StringBuffer +{ + /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>. + * Uses <code>String.valueOf()</code> to convert to + * <code>String</code>. + * @param bool the <code>boolean</code> to convert and append. + * @return this <code>StringBuffer</code>. + * @see java.lang.String#valueOf(boolean) + */ + public StringBuffer append (boolean bool) + { + return append (bool ? "true" : "false"); + } + + /** Append the <code>char</code> to this <code>StringBuffer</code>. + * @param c the <code>char</code> to append. + * @return this <code>StringBuffer</code>. + */ + public StringBuffer append (char ch) + { + ensureCapacity_unsynchronized (count + 1); + value[count++] = ch; + return this; + } + + /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>. + * Uses <code>String.valueOf()</code> to convert to + * <code>String</code>. + * @param inum the <code>int</code> to convert and append. + * @return this <code>StringBuffer</code>. + * @see java.lang.String#valueOf(int) + */ + public native StringBuffer append (int inum); + + /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>. + * Uses <code>String.valueOf()</code> to convert to + * <code>String</code>. + * @param lnum the <code>long</code> to convert and append. + * @return this <code>StringBuffer</code>. + * @see java.lang.String#valueOf(long) + */ + public StringBuffer append (long lnum) + { + return append (Long.toString (lnum)); + } + + /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>. + * Uses <code>String.valueOf()</code> to convert to + * <code>String</code>. + * @param fnum the <code>float</code> to convert and append. + * @return this <code>StringBuffer</code>. + * @see java.lang.String#valueOf(float) + */ + public StringBuffer append (float fnum) + { + return append (Float.toString (fnum)); + } + + /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>. + * Uses <code>String.valueOf()</code> to convert to + * <code>String</code>. + * @param dnum the <code>double</code> to convert and append. + * @return this <code>StringBuffer</code>. + * @see java.lang.String#valueOf(double) + */ + public StringBuffer append (double dnum) + { + return append (Double.toString (dnum)); + } + + /** Append the <code>String</code> value of the argument to this <code>StringBuffer</code>. + * Uses <code>String.valueOf()</code> to convert to + * <code>String</code>. + * @param obj the <code>Object</code> to convert and append. + * @return this <code>StringBuffer</code>. + * @see java.lang.String#valueOf(java.lang.Object) + */ + public StringBuffer append (Object obj) + { + return append (String.valueOf(obj)); + } + + /** Append the <code>String</code> to this <code>StringBuffer</code>. + * @param str the <code>String</code> to append. + * @return this <code>StringBuffer</code>. + */ + public StringBuffer append (String str) + { + if (str == null) + str = "null"; + int len = str.length(); + ensureCapacity_unsynchronized (count + len); + str.getChars(0, len, value, count); + count += len; + return this; + } + + private void ensureCapacity_unsynchronized (int minimumCapacity) + { + if (minimumCapacity > value.length) + { + minimumCapacity = value.length * 2 + 2; + char[] nb = new char[minimumCapacity]; + System.arraycopy(value, 0, nb, 0, count); + value = nb; + } + } + + /** Create a new StringBuffer with default capacity 16. + * @see JLS 20.13.1 + */ + public StringBuffer () + { + this (DEFAULT_CAPACITY); + } + + /** Create an empty <code>StringBuffer</code> with the specified initial capacity. + * @param capacity the initial capacity. + */ + public StringBuffer (int capacity) + { + count = 0; + value = new char[capacity]; + } + + /** Create a new <code>StringBuffer</code> with the characters in the specified <code>String</code>. + * Initial capacity will be the size of the String plus 16. + * @param str the <code>String</code> to make a <code>StringBuffer</code> out of. + */ + public StringBuffer (String str) + { + if (str == null) + str = "null"; + count = str.length(); + // JLS: The initial capacity of the string buffer is 16 plus the + // length of the argument string. + value = new char[count + DEFAULT_CAPACITY]; + str.getChars(0, count, value, 0); + } + + /** Convert this <code>StringBuffer</code> to a <code>String</code>. + * @return the characters in this StringBuffer + */ + // This is native because efficient implementation requires avoiding + // the Java protection mechanism. + public native String toString (); + + // Index of next available character. Note that this has + // permissions set this way so that String can get the value. + int count; + + // The buffer. Note that this has permissions set this way so that + // String can get the value. + char[] value; + + private final static int DEFAULT_CAPACITY = 16; // JLS 20.13.1 +} diff --git a/libjava/gnu/gcj/runtime/natStringBuffer.cc b/libjava/gnu/gcj/runtime/natStringBuffer.cc new file mode 100644 index 0000000..2777b9e --- /dev/null +++ b/libjava/gnu/gcj/runtime/natStringBuffer.cc @@ -0,0 +1,36 @@ +// natStringBuffer.cc - Implementation of java.lang.StringBuffer native methods. + +/* Copyright (C) 2001 Free Software Foundation + + This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +#include <config.h> +#include <gcj/cni.h> +#include <gnu/gcj/runtime/StringBuffer.h> +#include <java/lang/String.h> + +gnu::gcj::runtime::StringBuffer * +gnu::gcj::runtime::StringBuffer::append (jint num) +{ + // Use an array large enough for "-2147483648"; i.e. 11 chars. + jchar buffer[11]; + int i = _Jv_FormatInt (buffer+11, num); + jint needed = count + i; + ensureCapacity_unsynchronized (needed); + jchar* dst = elements (value) + count; + jchar* src = buffer+11-i; + while (--i >= 0) + *dst++ = *src++; + count = needed; + return this; +} + +java::lang::String * +gnu::gcj::runtime::StringBuffer::toString () +{ + return new java::lang::String (this); +} |