- java.lang.Object
-
- net.morimekta.strings.chr.CharSlice
-
- All Implemented Interfaces:
CharSequence
,Comparable<CharSlice>
public class CharSlice extends Object implements Comparable<CharSlice>, CharSequence
A slice of a character array. This class is handy to expand on whenever a very large number of sub-strings are handled, so you are guaranteed that the underlying character array is never copied. Also:- Ordering is in a structured order of the underlying buffer, not of the visible string itself.
- Equality and hash is based on the assumption that you care about the underlying buffer more than the actual characters.
-
The
toString()
method returns the "visible" content of the slice, as it is was a string.
As the characters in the buffer is not considered part of the data of this class, it can be considered fully immutable.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description char
at(int index)
Get char char at the dynamic index.char
charAt(int i)
Get character at slice relative position.int
comparePosition(CharSlice o)
Handy method to compare slices based on the position in the main buffer.int
compareTo(CharSlice o)
Compare slice with other slice as a string.boolean
equals(Object o)
int
hashCode()
int
length()
Get the total length of the slice.static CharSlice
of(CharSequence str)
Get the char slice for the sequence.int
offset()
Get the offset of the buffer.CharSlice
subSequence(int start, int end)
Create a substring slice based on the current slice.CharSlice
subSlice(int start)
Similar to subSequence, but also handles negative offset, where a negative offset means relative to the end.CharSlice
subSlice(int start, int end)
Similar to subSequence, but also handles negative offset, where a negative offset means relative to the end.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.CharSequence
chars, codePoints
-
-
-
-
Constructor Detail
-
CharSlice
public CharSlice(String raw)
Create a slice instance.- Parameters:
raw
- The raw string to wrap.
-
CharSlice
public CharSlice(char[] fb)
Create a slice from a full character buffer.- Parameters:
fb
- The char buffer to wrap in the slice.
-
CharSlice
public CharSlice(char[] fb, int off, int len)
Create a slice instance. The slice is only meant to be internal state immutable, and not representing an immutable byte content.- Parameters:
fb
- The buffer to wrap.off
- The start offset to wrap.len
- The length to represent.
-
-
Method Detail
-
of
public static CharSlice of(CharSequence str)
Get the char slice for the sequence.- Parameters:
str
- Get char slice of the sequence.- Returns:
- The slice or slice wrapping the string.
-
offset
public int offset()
Get the offset of the buffer.- Returns:
- The slice offset.
-
subSlice
public CharSlice subSlice(int start)
Similar to subSequence, but also handles negative offset, where a negative offset means relative to the end. This will return the slice from the start position of the index, until the end. E.g.subSlice(-2)
will give a slice containing the last two chars of the origin.- Parameters:
start
- The start offset, inclusive.- Returns:
- The resulting slice.
-
subSlice
public CharSlice subSlice(int start, int end)
Similar to subSequence, but also handles negative offset, where a negative offset means relative to the end.- Parameters:
start
- The start offset, inclusive.end
- The end offset, exclusive.- Returns:
- The resulting slice.
-
at
public char at(int index)
Get char char at the dynamic index. This handles negative indices as counting from the end of the slice.- Parameters:
index
- The index of the requested char.- Returns:
- The char at given index.
-
length
public int length()
Get the total length of the slice.- Specified by:
length
in interfaceCharSequence
- Returns:
- The slice length.
-
subSequence
public CharSlice subSequence(int start, int end)
Create a substring slice based on the current slice.- Specified by:
subSequence
in interfaceCharSequence
- Parameters:
start
- The internal start position, relative to the slice's offset, inclusive.end
- The internal end position, relative to the slice's offset, exclusive.- Returns:
- The substring slice.
-
charAt
public char charAt(int i)
Get character at slice relative position.- Specified by:
charAt
in interfaceCharSequence
- Parameters:
i
- The position to get. If negative is relative to the slice's end position.- Returns:
- The char at given position.
-
compareTo
public int compareTo(CharSlice o)
Compare slice with other slice as a string. This will always do a simple char-by-char value comparison, ignoring locale collation.- Specified by:
compareTo
in interfaceComparable<CharSlice>
- Parameters:
o
- The other slice.- Returns:
- Compared value.
-
comparePosition
public int comparePosition(CharSlice o)
Handy method to compare slices based on the position in the main buffer.Slice ordering: - Firstly ordered by start (offset) position. - Secondly ordered by reverse length (longest slice first).
Result is undefined of the two slices point to different byte buffers.
- Parameters:
o
- The other slice.- Returns:
- Compared value.
-
toString
public String toString()
- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-
-