Changeset 11527


Ignore:
Timestamp:
01/03/09 12:30:16 (6 years ago)
Author:
mevenson
Message:

(Phil Hudson) Make FastStringBuffer? an adapter to java-1.5's StringBuilder?.

JUnit tests integrated into 'build.xml', run via 'ant abcl.test'.

Further integration with other build systems (Lisp and Netbeans) has not been done.

Location:
trunk/abcl
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/abcl/build.xml

    r11515 r11527  
    1818    <property name="abcl.jar.path"
    1919        value="${dist.dir}/abcl.jar"/>
     20    <property name="abcl.ext.dir"
     21        value="${basedir}/ext"/>
     22
    2023
    2124    <target name="help">
    2225      <echo>Main Ant targets:
    2326 abcl.compile 
    24    -- compile ABCL to ${build.classes.dir}
     27   -- compile ABCL to ${build.classes.dir}.
    2528 abcl.jar     
    26    -- create packaged ${abcl.jar.path}
     29   -- create packaged ${abcl.jar.path}.
    2730 abcl.wrapper 
    28    -- create executable wrapper for ABCL
     31   -- create executable wrapper for ABCL.
    2932 abcl.source.zip abcl.source.tar
    30     -- create source distributions in ${dist.dir}
     33   -- create source distributions in ${dist.dir}.
     34 acbl.test.java
     35   -- Run junit tests under ${abcl.test.src.dir}.
    3136 abcl.clean
    32     -- remove ABCL intermediate files</echo>
     37   -- remove ABCL intermediate files</echo>
    3338      <echo>Corresponding targets for J have been removed.</echo>
    3439    </target>
     
    394399    </target>
    395400
     401    <property name="abcl.test.classes.dir"
     402        value="${build.dir}/classes-test"/>
     403
     404    <property name="abcl.test.src.dir"
     405        value="${basedir}/test/src"/>
     406
     407    <patternset id="abcl.test.source.java">
     408      <!-- For now, we list tests explicitly, because we have to
     409           enumerate them later to the JUnit test runner. -->
     410      <include name="org/armedbear/lisp/FastStringBufferTest.java"/>
     411    </patternset>
     412
     413    <property name="junit-4.5.path"
     414        value="${abcl.ext.dir}/junit-4.5.jar"/>
     415
     416    <path id="abcl.test.compile.classpath">
     417      <pathelement location="${junit-4.5.path}"/>
     418      <pathelement location="${build.classes.dir}"/>
     419    </path>
     420
     421    <target name="abcl.test.pre-compile">
     422      <mkdir dir="${abcl.ext.dir}"/>
     423      <get src="http://downloads.sourceforge.net/junit/junit-4.5.jar?modtime=1218209625"
     424    dest="${junit-4.5.path}"/>
     425    </target>
     426 
     427    <target name="abcl.test.compile" depends="abcl.test.pre-compile,abcl.compile">
     428      <mkdir dir="${abcl.test.classes.dir}"/>
     429      <javac destdir="${abcl.test.classes.dir}"
     430       classpathref="abcl.test.compile.classpath"
     431       debug="true"
     432       target="1.5">
     433  <src path="${abcl.test.src.dir}"/>
     434  <patternset refid="abcl.test.source.java"/>
     435      </javac>
     436    </target>
     437
     438    <path id="abcl.test.run.classpath">
     439      <path refid="abcl.test.compile.classpath"/>
     440      <pathelement location="${abcl.test.classes.dir}"/>
     441    </path>
     442 
     443    <target name="abcl.test.java" depends="abcl.test.compile">
     444      <java fork="true"
     445      classpathref="abcl.test.run.classpath"
     446      classname="org.junit.runner.JUnitCore">
     447  <arg value="org.armedbear.lisp.FastStringBufferTest"/>
     448      </java>
     449    </target>
     450
    396451    <import file="netbeans-build.xml" optional="true"/>
    397452<!--    <import file="j-build.xml" optional="true"/>  -->
  • trunk/abcl/src/org/armedbear/lisp/FastStringBuffer.java

    r11488 r11527  
    33 *
    44 * Copyright (C) 1998-2005 Peter Graves
     5 * Copyright (C) 2008 Phil Hudson
    56 * $Id$
    67 *
     
    3435package org.armedbear.lisp;
    3536
    36 public final class FastStringBuffer
     37/**
     38 * An adaptor of the Java 1.5 java.lang.StringBuilder.
     39 *
     40 * "This class should be removed with all references to it replaced
     41 *  with java.lang.StringBuilder once enough confidence in this change
     42 *  has been gained." -- Phil Hudson 20090202 via <armedbear-j-devel>.
     43 */
     44public final class FastStringBuffer implements Appendable, CharSequence
    3745{
    3846  private static final int SPARE_CAPACITY = 128;
    3947
    40   private char[] buffer;
    41   private int used;
     48  private final StringBuilder builder;
    4249
    4350  public FastStringBuffer()
    4451  {
    45     buffer = new char[SPARE_CAPACITY];
     52    this(SPARE_CAPACITY);
    4653  }
    4754
    48   public FastStringBuffer(String s)
     55  public FastStringBuffer(String s) 
    4956  {
    50     used = s.length();
    51     buffer = new char[used + SPARE_CAPACITY];
    52     s.getChars(0, used, buffer, 0);
     57    builder = new StringBuilder(s);
    5358  }
    5459
    5560  public FastStringBuffer(char c)
    5661  {
    57     used = 1;
    58     buffer = new char[1 + SPARE_CAPACITY];
    59     buffer[0] = c;
     62    this(String.valueOf(c));
    6063  }
    6164
    6265  public FastStringBuffer(int length) throws NegativeArraySizeException
    6366  {
    64     if (length < 0)
    65       throw new NegativeArraySizeException();
    66     buffer = new char[length];
     67    builder = new StringBuilder(length);
    6768  }
    6869
    6970  public final int length()
    7071  {
    71     return used;
     72    return builder.length();
    7273  }
    7374
    7475  public final int capacity()
    7576  {
    76     return buffer.length;
     77    return builder.capacity();
    7778  }
    7879
    7980  public final char charAt(int index)
    8081  {
    81     try
    82       {
    83         return buffer[index];
    84       }
    85     catch (ArrayIndexOutOfBoundsException e)
    86       {
    87         throw new StringIndexOutOfBoundsException();
    88       }
     82    return builder.charAt(index);
    8983  }
    9084
    9185  public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
    9286  {
    93     if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > used)
    94       throw new StringIndexOutOfBoundsException();
    95     System.arraycopy(buffer, srcBegin, dst, dstBegin, srcEnd - srcBegin);
     87    builder.getChars(srcBegin, srcEnd, dst, dstBegin);
    9688  }
    9789
    9890  public void setCharAt(int index, char c)
    9991  {
    100     try
    101       {
    102         buffer[index] = c;
    103       }
    104     catch (ArrayIndexOutOfBoundsException e)
    105       {
    106         throw new StringIndexOutOfBoundsException();
    107       }
     92    builder.setCharAt(index, c);
    10893  }
    10994
    11095  public void ensureCapacity(int minimumCapacity)
    11196  {
    112     if (buffer.length < minimumCapacity)
    113       {
    114         int newCapacity = buffer.length * 2 + 2;
    115         if (newCapacity < minimumCapacity)
    116           newCapacity = minimumCapacity;
    117         char newBuffer[] = new char[newCapacity];
    118         System.arraycopy(buffer, 0, newBuffer, 0, used);
    119         buffer = newBuffer;
    120       }
    121   }
    122 
    123   public void setText(String s)
    124   {
    125     used = 0;
    126     append(s);
     97    builder.ensureCapacity(minimumCapacity);
    12798  }
    12899
    129100  public FastStringBuffer append(String s)
    130101  {
    131     if (s == null)
    132       s = "null";
    133     int addedLength = s.length();
    134     int combinedLength = used + addedLength;
    135     ensureCapacity(combinedLength);
    136     s.getChars(0, addedLength, buffer, used);
    137     used = combinedLength;
     102    builder.append(s);
    138103    return this;
    139104  }
     
    141106  public FastStringBuffer append(char[] chars)
    142107  {
    143     if (used + chars.length > buffer.length)
    144       ensureCapacity(used + chars.length);
    145     System.arraycopy(chars, 0, buffer, used, chars.length);
    146     used += chars.length;
     108    builder.append(chars);
    147109    return this;
    148110  }
     
    150112  public FastStringBuffer append(char[] chars, int offset, int len)
    151113  {
    152     if (offset < 0 || len < 0 || offset + len > chars.length)
    153       throw new StringIndexOutOfBoundsException();
    154     if (used + len > buffer.length)
    155       ensureCapacity(used + len);
    156     System.arraycopy(chars, offset, buffer, used, len);
    157     used += len;
     114    builder.append(chars, offset, len);
    158115    return this;
    159116  }
     
    166123  public FastStringBuffer append(char c)
    167124  {
    168     if (used + 1 > buffer.length)
    169       ensureCapacity(used + 1);
    170     buffer[used++] = c;
     125    builder.append(c);
    171126    return this;
    172127  }
     
    184139  public void setLength(int newLength) throws IndexOutOfBoundsException
    185140  {
    186     if (newLength < 0)
    187       throw new StringIndexOutOfBoundsException(newLength);
    188     ensureCapacity(newLength);
    189     used = newLength;
     141    builder.setLength(newLength);
    190142  }
    191143
    192144  public FastStringBuffer reverse()
    193145  {
    194     final int limit = used / 2;
    195     for (int i = 0; i < limit; ++i)
    196       {
    197         char c = buffer[i];
    198         buffer[i] = buffer[used - i - 1];
    199         buffer[used - i - 1] = c;
    200       }
    201     return this;
     146    builder.reverse();
     147    return this;
    202148  }
    203149
     
    205151  public final String toString()
    206152  {
    207     return new String(buffer, 0, used);
     153    return builder.toString();
    208154  }
    209155
    210156  public final char[] toCharArray()
    211157  {
    212     char[] copy = new char[used];
    213     System.arraycopy(buffer, 0, copy, 0, used);
    214     return copy;
     158    return toString().toCharArray();
    215159  }
     160
     161   public CharSequence subSequence(int start, int end)
     162  {
     163    return builder.subSequence(start, end);
     164   }
     165 
     166   public FastStringBuffer append(CharSequence seq)
     167   {
     168     builder.append(seq);
     169     return this;
     170   }
     171 
     172   public FastStringBuffer append(CharSequence seq, int start, int end)
     173   {
     174     builder.append(seq, start, end);
     175     return this;
     176   }
    216177}
Note: See TracChangeset for help on using the changeset viewer.