Package net.sf.saxon.tree.tiny
Class CharSlice
- java.lang.Object
-
- net.sf.saxon.tree.tiny.CharSlice
-
- All Implemented Interfaces:
java.lang.CharSequence
public final class CharSlice extends java.lang.Object implements java.lang.CharSequence
This is an implementation of the CharSequence interface: it implements a CharSequence as a view of an array. The implementation relies on the array being immutable: as a minimum, the caller is required to ensure that the array contents will not change so long as the CharSlice remains in existence.This class should be more efficient than String because it avoids copying the characters unnecessarily.
The methods in the class don't check their arguments. Incorrect arguments will generally result in exceptions from lower-level classes.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description char
charAt(int index)
Returns the character at the specified index.void
copyTo(char[] destination, int destOffset)
Append the contents to another array at a given offset.boolean
equals(java.lang.Object other)
Compare equalityvoid
getChars(int start, int end, char[] destination, int destOffset)
Append the contents to another array at a given offset.int
hashCode()
Generate a hash codeint
indexOf(char c)
Get the index of a specific character in the sequence.int
length()
Returns the length of this character sequence.void
setLength(int length)
Set the length of this character sequence, without changing the array and start offset to which it is boundjava.lang.CharSequence
subSequence(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.java.lang.String
substring(int start, int end)
Returns a new character sequence that is a subsequence of this sequence.char[]
toCharArray()
static char[]
toCharArray(java.lang.CharSequence in)
Utility method to turn a CharSequence into an array of charsjava.lang.String
toString()
Convert to a stringvoid
write(java.io.Writer writer)
Write the value to a writer
-
-
-
Constructor Detail
-
CharSlice
public CharSlice(char[] array)
Create a CharSlice that maps to the whole of a char[] array- Parameters:
array
- the char[] array
-
CharSlice
public CharSlice(char[] array, int start, int length)
Create a CharSlice that maps to a section of a char[] array- Parameters:
array
- the char[] arraystart
- position of the first character to be includedlength
- number of characters to be included
-
-
Method Detail
-
length
public int length()
Returns the length of this character sequence. The length is the number of 16-bit Unicode characters in the sequence.- Specified by:
length
in interfacejava.lang.CharSequence
- Returns:
- the number of characters in this sequence
-
setLength
public void setLength(int length)
Set the length of this character sequence, without changing the array and start offset to which it is bound- Parameters:
length
- the new length of the CharSlice (which must be less than the existing length, though this is not enforced)
-
charAt
public char charAt(int index)
Returns the character at the specified index. An index ranges from zero to length() - 1. The first character of the sequence is at index zero, the next at index one, and so on, as for array indexing.- Specified by:
charAt
in interfacejava.lang.CharSequence
- Parameters:
index
- the index of the character to be returned- Returns:
- the specified character
- 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 new character sequence that is a subsequence of this sequence. The subsequence starts with the character at the specified index and ends with the character at index end - 1. The length of the returned sequence is end - start, so if start == end then an empty sequence is returned.- Specified by:
subSequence
in 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
-
toString
public java.lang.String toString()
Convert to a string- Specified by:
toString
in interfacejava.lang.CharSequence
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object other)
Compare equality- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
Generate a hash code- Overrides:
hashCode
in classjava.lang.Object
-
indexOf
public int indexOf(char c)
Get the index of a specific character in the sequence. Returns -1 if not found. This method mimicsString.indexOf(int)
- Parameters:
c
- the character to be found- Returns:
- the position of the first occurrence of that character, or -1 if not found.
-
substring
public java.lang.String substring(int start, int end)
Returns a new character sequence that is a subsequence of this sequence. Unlike subSequence, this is guaranteed to return a String.- Parameters:
start
- position of the first character to be included (relative to the start of the CharSlice, not the underlying array)end
- position of the first character not to be included (relative to the start of the CharSlice)- Returns:
- the substring, as a String object
-
copyTo
public void copyTo(char[] destination, int destOffset)
Append the contents to another array at a given offset. The caller is responsible for ensuring that sufficient space is available.- Parameters:
destination
- the array to which the characters will be copieddestOffset
- the offset in the target array where the copy will start
-
getChars
public void getChars(int start, int end, char[] destination, int destOffset)
Append the contents to another array at a given offset. The caller is responsible for ensuring that sufficient space is available. Otherwise this behaves like String.getChars()- Parameters:
start
- offset of first character to be copiedend
- offset of the first character that is not copieddestination
- the array to which the characters will be copieddestOffset
- the offset in the target array where the copy will start
-
toCharArray
public char[] toCharArray()
-
toCharArray
public static char[] toCharArray(java.lang.CharSequence in)
Utility method to turn a CharSequence into an array of chars- Parameters:
in
- the CharSequence- Returns:
- the corresponding array of chars
-
write
public void write(java.io.Writer writer) throws java.io.IOException
Write the value to a writer- Parameters:
writer
- the writer to be written to- Throws:
java.io.IOException
- if writing fails
-
-