Package net.sf.saxon.tree.util
Class FastStringBuffer
- java.lang.Object
-
- net.sf.saxon.tree.util.FastStringBuffer
-
- All Implemented Interfaces:
java.lang.CharSequence,AppendableCharSequence,CharSequenceConsumer
public final class FastStringBuffer extends java.lang.Object implements AppendableCharSequence, CharSequenceConsumer
A simple implementation of a class similar to StringBuffer. Unlike StringBuffer it is not synchronized. It also offers the capability to remove unused space. (This class could possibly be replaced by StringBuilder in JDK 1.5, but using our own class gives more control.)
-
-
Constructor Summary
Constructors Constructor Description FastStringBuffer(int initialSize)Create a FastStringBuffer with a given initial capacityFastStringBuffer(java.lang.CharSequence cs)Create a FastStringBuffer with initial content
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidappend(char[] srcArray)Append the entire contents of a character array to the buffervoidappend(char[] srcArray, int start, int length)Append the contents of a character array to the buffervoidappend(java.lang.CharSequence s)Append the contents of a general CharSequence to the buffervoidappend(java.lang.String s)Append the contents of a String to the buffervoidappend(java.lang.StringBuffer s)Append the contents of a StringBuffer to the buffervoidappend(UnicodeString str)Append a UnicodeString to the buffer (using surrogate pairs if necessary)voidappend(CharSlice s)Append the contents of a CharSlice to the buffervoidappend(FastStringBuffer s)Append the contents of a FastStringBuffer to the buffervoidappendWideChar(int ch)Append a wide character to the buffer (as a surrogate pair if necessary)FastStringBuffercat(char ch)Append a character to the bufferFastStringBuffercat(java.lang.CharSequence s)Append the contents of a general CharSequence to the bufferFastStringBuffercat(java.lang.String s)Append the contents of a String to the buffercharcharAt(int index)Returns thecharvalue at the specified index.FastStringBuffercondense()Remove surplus space from the array.static java.lang.StringdiagnosticPrint(java.lang.CharSequence in)Diagnostic print of the contents of a CharSequence.voidensureCapacity(int extra)Expand the character array if necessary to ensure capacity for appended databooleanequals(java.lang.Object other)Compare equalityvoidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Copies characters from this FastStringBuffer into the destination character array.inthashCode()Generate a hash codeintindexOf(char ch)Get the index of the first character equal to a given valuevoidinsert(int index, char ch)Insert a character at a particular offsetvoidinsertWideChar(int index, int ch)Insert wide character at a particular offsetbooleanisEmpty()Ask whether the string buffer is emptyintlength()Returns the length of this character sequence.voidprepend(char ch)Insert a given character at the start of the buffervoidprepend(java.lang.CharSequence str)Insert a given string at the start of the buffervoidprependRepeated(char ch, int repeat)Insert a given character N times at the start of the buffervoidprependWideChar(int ch)Prepend a wide character to the buffer (as a surrogate pair if necessary)voidremoveCharAt(int index)Remove a character at a particular offsetvoidsetCharAt(int index, char ch)Set the character at a particular offsetvoidsetLength(int length)Set the length.java.lang.CharSequencesubSequence(int start, int end)Returns a newCharSequencethat is a subsequence of this sequence.char[]toCharArray()Get a char[] array containing the characters.java.lang.StringtoString()Convert contents of the FastStringBuffer to a stringvoidwrite(java.io.Writer writer)Write the value to a writer-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.saxon.tree.util.CharSequenceConsumer
close, open
-
-
-
-
Field Detail
-
C16
public static final int C16
- See Also:
- Constant Field Values
-
C64
public static final int C64
- See Also:
- Constant Field Values
-
C256
public static final int C256
- See Also:
- Constant Field Values
-
C1024
public static final int C1024
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FastStringBuffer
public FastStringBuffer(int initialSize)
Create a FastStringBuffer with a given initial capacity- Parameters:
initialSize- the initial capacity
-
FastStringBuffer
public FastStringBuffer(java.lang.CharSequence cs)
Create a FastStringBuffer with initial content- Parameters:
cs- the initial content. The buffer is created with just enough capacity for this content (it will be expanded if more content is added later).
-
-
Method Detail
-
cat
public FastStringBuffer cat(java.lang.String s)
Append the contents of a String to the buffer- Parameters:
s- the String to be appended- Returns:
- the FastStringBuffer with the new contents
-
append
public void append(java.lang.String s)
Append the contents of a String to the buffer- Parameters:
s- the String to be appended
-
append
public void append(CharSlice s)
Append the contents of a CharSlice to the buffer- Parameters:
s- the String to be appended
-
append
public void append(FastStringBuffer s)
Append the contents of a FastStringBuffer to the buffer- Parameters:
s- the FastStringBuffer to be appended
-
append
public void append(java.lang.StringBuffer s)
Append the contents of a StringBuffer to the buffer- Parameters:
s- the StringBuffer to be appended
-
cat
public FastStringBuffer cat(java.lang.CharSequence s)
Append the contents of a general CharSequence to the buffer- Specified by:
catin interfaceAppendableCharSequence- Specified by:
catin interfaceCharSequenceConsumer- Parameters:
s- the CharSequence to be appended- Returns:
- the result of the concatenation
-
append
public void append(java.lang.CharSequence s)
Append the contents of a general CharSequence to the buffer- Parameters:
s- the CharSequence to be appended
-
append
public void append(char[] srcArray, int start, int length)Append the contents of a character array to the buffer- Parameters:
srcArray- the array whose contents are to be addedstart- the offset of the first character in the array to be copiedlength- the number of characters to be copied
-
append
public void append(char[] srcArray)
Append the entire contents of a character array to the buffer- Parameters:
srcArray- the array whose contents are to be added
-
cat
public FastStringBuffer cat(char ch)
Append a character to the buffer- Specified by:
catin interfaceAppendableCharSequence- Specified by:
catin interfaceCharSequenceConsumer- Parameters:
ch- the character to be added- Returns:
- this FastStringBuffer
-
appendWideChar
public void appendWideChar(int ch)
Append a wide character to the buffer (as a surrogate pair if necessary)- Parameters:
ch- the character, as a 32-bit Unicode codepoint
-
append
public void append(UnicodeString str)
Append a UnicodeString to the buffer (using surrogate pairs if necessary)- Parameters:
str- the UnicodeString
-
prependWideChar
public void prependWideChar(int ch)
Prepend a wide character to the buffer (as a surrogate pair if necessary)- Parameters:
ch- the character, as a 32-bit Unicode codepoint
-
length
public int length()
Returns the length of this character sequence. The length is the number of 16-bitchars in the sequence.- Specified by:
lengthin interfacejava.lang.CharSequence- Returns:
- the number of
chars in this sequence
-
isEmpty
public boolean isEmpty()
Ask whether the string buffer is empty- Returns:
- true if the buffer is empty (length()==0)
-
charAt
public char charAt(int index)
Returns thecharvalue at the specified index. An index ranges from zero to length() - 1. The firstcharvalue of the sequence is at index zero, the next at index one, and so on, as for array indexing.If the
charvalue specified by the index is a surrogate, the surrogate value is returned.- Specified by:
charAtin interfacejava.lang.CharSequence- Parameters:
index- the index of thecharvalue to be returned- Returns:
- the specified
charvalue - Throws:
java.lang.IndexOutOfBoundsException- if the index argument is negative or not less than length()
-
subSequence
public java.lang.CharSequence subSequence(int start, int end)Returns a newCharSequencethat is a subsequence of this sequence. The subsequence starts with thecharvalue at the specified index and ends with thecharvalue at index end - 1. The length (inchars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.- Specified by:
subSequencein interfacejava.lang.CharSequence- Parameters:
start- the start index, inclusiveend- the end index, exclusive- Returns:
- the specified subsequence
- Throws:
java.lang.IndexOutOfBoundsException- if start or end are negative, if end is greater than length(), or if start is greater than end
-
getChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)Copies characters from this FastStringBuffer into the destination character array.The first character to be copied is at index
srcBegin; the last character to be copied is at indexsrcEnd-1(thus the total number of characters to be copied issrcEnd-srcBegin). The characters are copied into the subarray ofdststarting at indexdstBeginand ending at index:dstbegin + (srcEnd-srcBegin) - 1- Parameters:
srcBegin- index of the first character in the string to copy.srcEnd- index after the last character in the string to copy.dst- the destination array.dstBegin- the start offset in the destination array.- Throws:
java.lang.IndexOutOfBoundsException- If any of the following is true:srcBeginis negative.srcBeginis greater thansrcEndsrcEndis greater than the length of this stringdstBeginis negativedstBegin+(srcEnd-srcBegin)is larger thandst.length
-
indexOf
public int indexOf(char ch)
Get the index of the first character equal to a given value- Parameters:
ch- the character to search for- Returns:
- the position of the first occurrence, or -1 if not found
-
toString
public java.lang.String toString()
Convert contents of the FastStringBuffer to a string- Specified by:
toStringin interfacejava.lang.CharSequence- Overrides:
toStringin classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
Compare equality- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
Generate a hash code- Overrides:
hashCodein classjava.lang.Object
-
toCharArray
public char[] toCharArray()
Get a char[] array containing the characters. The caller should not modify the array.- Returns:
- a char[] array containing the characters.
-
setCharAt
public void setCharAt(int index, char ch)Set the character at a particular offset- Parameters:
index- the index of the character to be setch- the new character to overwrite the existing character at that location- Throws:
java.lang.IndexOutOfBoundsException- if int<0 or int>=length()
-
insert
public void insert(int index, char ch)Insert a character at a particular offset- Parameters:
index- the index of the character to be setch- the new character to insert at that location- Throws:
java.lang.IndexOutOfBoundsException- if int<0 or int>=length()
-
insertWideChar
public void insertWideChar(int index, int ch)Insert wide character at a particular offset- Parameters:
index- the index of the character to be setch- the character, as a 32-bit Unicode codepoint- Throws:
java.lang.IndexOutOfBoundsException- if int<0 or int>=length()
-
removeCharAt
public void removeCharAt(int index)
Remove a character at a particular offset- Parameters:
index- the index of the character to be set- Throws:
java.lang.IndexOutOfBoundsException- if int<0 or int>=length()
-
prepend
public void prepend(char ch)
Insert a given character at the start of the buffer- Parameters:
ch- the character to insert
-
prepend
public void prepend(java.lang.CharSequence str)
Insert a given string at the start of the buffer- Parameters:
str- the string to insert
-
prependRepeated
public void prependRepeated(char ch, int repeat)Insert a given character N times at the start of the buffer- Parameters:
ch- the character to insertrepeat- the number of occurrences required. Supplying 0 or a negative number is OK, and is treated as a no-op.
-
setLength
public void setLength(int length)
Set the length. If this exceeds the current length, this method is a no-op. If this is less than the current length, characters beyond the specified point are deleted.- Specified by:
setLengthin interfaceAppendableCharSequence- Parameters:
length- the new length
-
ensureCapacity
public void ensureCapacity(int extra)
Expand the character array if necessary to ensure capacity for appended data- Parameters:
extra- the amount of additional capacity needed, in characters
-
condense
public FastStringBuffer condense()
Remove surplus space from the array. This doesn't reduce the array to the minimum possible size; it only reclaims space if it seems worth doing. Specifically, it contracts the array if the amount of wasted space is more than 256 characters, or more than half the allocated size and more than 20 chars.- Returns:
- the buffer after removing unused space
-
write
public void write(java.io.Writer writer) throws java.io.IOExceptionWrite the value to a writer- Parameters:
writer- the writer to which the content is to be written- Throws:
java.io.IOException
-
diagnosticPrint
public static java.lang.String diagnosticPrint(java.lang.CharSequence in)
Diagnostic print of the contents of a CharSequence. Ordinary printable ASCII characters are displayed as themselves; anything else is displayed as a \\uNNNN escape sequence- Parameters:
in- the CharSequence whose contents are to be displayed.- Returns:
- the diagnostic output
-
-