package java.lang; import josx.platform.rcx.LCD; /** * An expandable string of characters. Actually not very expandable! * 09/25/2001 added number formatting thanks to Martin E. Nielsen. * You must ensure that the buffer is large enough to take the formatted * number. *
* @author Martin E. Nielsen
*/
public final class StringBuffer
{
char[] characters;
int curPos = 0;
/**
* Conversion between integers from 0 to 9 and their respective
* chars.
**/
private static final char[] numbers = { '0', '1', '2', '3', '4',
'5', '6', '7', '8', '9' };
/**
* The value of log(10) used for converting from base
* e to base 10.
**/
private static final float log10 = 2.30258509f;
public StringBuffer () {
characters = new char[0];
}
public StringBuffer (String aString)
{
characters = aString.toCharArray();
}
public StringBuffer (int length)
{
characters = new char[length];
}
public StringBuffer delete(int start, int end)
{
if (start >= 0 && start < end && start < curPos)
{
if (end >= curPos)
end = curPos;
else
System.arraycopy(characters, end, characters, start, curPos-end);
curPos -= end-start;
}
return this;
}
public StringBuffer append (String s)
{
// Reminder: compact code more important than speed
char[] sc = s.toCharArray();
int cl = characters.length;
int sl = sc.length;
char [] nc = characters;
if (sl + curPos > cl)
{
nc = new char[sl + curPos];
System.arraycopy (characters, 0, nc, 0, curPos);
}
System.arraycopy (sc, 0, nc, curPos, sl);
characters = nc;
curPos += sl;
return this;
}
public StringBuffer append (java.lang.Object aObject)
{
return append (aObject.toString());
}
public StringBuffer append (boolean aBoolean)
{
return append (aBoolean ? "true" : "false");
}
public StringBuffer append (char aChar)
{
return append (new String (new char[] { aChar }, 0, 1));
}
public StringBuffer append (int aInt)
{
if ( aInt < 0 ) {
characters[ curPos++ ] = '-';
aInt = -aInt;
} // if
int pow = ( int )Math.floor( Math.log( aInt ) / log10 );
int div = 0;
while ( pow >= 0 ) {
div = ( int ) ( aInt / (int)Math.pow( 10, pow ) );
characters[ curPos++ ] = numbers[ div ];
aInt -= div * (int)Math.pow( 10, pow );
pow--;
} // while
return this;
}
public StringBuffer append (long aLong)
{
return append("