Class Chars
- java.lang.Object
-
- net.sourceforge.pmd.lang.document.Chars
-
- All Implemented Interfaces:
CharSequence
public final class Chars extends Object implements CharSequence
View on a string which doesn't copy the array for subsequence operations. This view is immutable. Since it uses a string internally it benefits from Java 9's compacting feature, it can also be efficiently created from a StringBuilder. When confronted with an instance of this interface, please don't create substrings unnecessarily. BothsubSequence(int, int)
andslice(int, int)
can cut out a subsequence without copying the underlying byte array. ThePattern
API also works perfectly on arbitraryCharSequence
s, not just on strings. Lastly some methods here provided provide mediated access to the underlying string, which for many use cases is much more optimal than using this CharSequence directly, egappendChars(StringBuilder)
,writeFully(Writer)
.- See Also:
Chars::wrap, the factory method
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
appendChars(StringBuilder sb)
Append this character sequence on the given stringbuilder.void
appendChars(StringBuilder sb, int start, int end)
Appends the character range identified by start and end offset into the string builder.char
charAt(int index)
boolean
contentEquals(CharSequence cs)
LikecontentEquals(CharSequence, boolean)
, considering case distinctions.boolean
contentEquals(CharSequence cs, boolean ignoreCase)
Returns true if this char sequence is logically equal to the parameter.boolean
endsWith(String suffix)
boolean
equals(Object o)
ByteBuffer
getBytes(Charset charset)
Returns the characters of this charsequence encoded with the given charset.void
getChars(int srcBegin, char @NonNull [] cbuf, int dstBegin, int count)
Copies 'count' characters from index 'srcBegin' into the given array, starting at 'dstBegin'.int
hashCode()
int
indexOf(int ch, int fromIndex)
int
indexOf(String searched, int fromIndex)
boolean
isEmpty()
Whether this slice is the empty string.int
lastIndexOf(int ch, int fromIndex)
int
length()
Iterable<Chars>
lines()
Returns an iterable over the lines of this char sequence.Stream<Chars>
lineStream()
Returns a stream of lines yielded bylines()
.Reader
newReader()
Returns a new reader for the whole contents of this char sequence.Chars
removePrefix(String charSeq)
Remove the prefix if it is present, otherwise returns this.Chars
removeSuffix(String charSeq)
Remove the suffix if it is present, otherwise returns this.Chars
slice(int off, int len)
LikesubSequence(int, int)
but with offset + length instead of start + end.Chars
slice(TextRegion region)
Slice a region of text.Iterable<Chars>
splits(Pattern regex)
Split this slice into subslices, likeString.split(String)
, except it's iterated lazily.boolean
startsWith(char prefix, int fromIndex)
boolean
startsWith(String prefix)
boolean
startsWith(String prefix, int fromIndex)
Chars
subSequence(int start)
Returns the subsequence that starts at the given offset and ends at the end of this string.Chars
subSequence(int start, int end)
String
substring(int start, int end)
Returns the substring between the given offsets.@NonNull String
toString()
StringBuilder
toStringBuilder()
Returns a new stringbuilder containing the whole contents of this char sequence.Chars
trim()
LikeString.trim()
.Chars
trimBlankLines()
Remove trailing and leading blank lines.Chars
trimEnd()
Returns a subsequence which does not end with control characters (<= 32
).Chars
trimStart()
Returns a subsequence which does not start with control characters (<= 32
).static Chars
wrap(CharSequence chars)
Wraps the given char sequence into aChars
.void
write(@NonNull Writer writer, int start, int count)
Write a range of characters to the given writer.void
writeFully(@NonNull Writer writer)
Write all characters of this buffer into the given writer.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.CharSequence
chars, codePoints
-
-
-
-
Field Detail
-
EMPTY
public static final Chars EMPTY
An empty Chars instance.
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Whether this slice is the empty string.
-
wrap
public static Chars wrap(CharSequence chars)
Wraps the given char sequence into aChars
. This may callCharSequence.toString()
. If the sequence is already aChars
, returns it. This is the main factory method for this class. You can eg pass a StringBuilder if you want.
-
writeFully
public void writeFully(@NonNull Writer writer) throws IOException
Write all characters of this buffer into the given writer.- Parameters:
writer
- A writer- Throws:
NullPointerException
- If the writer is nullIOException
-
write
public void write(@NonNull Writer writer, int start, int count) throws IOException
Write a range of characters to the given writer.- Parameters:
writer
- A writerstart
- Start offset in this CharSequencecount
- Number of characters- Throws:
IOException
- If the writer throwsIndexOutOfBoundsException
- SeeWriter.write(int)
-
getChars
public void getChars(int srcBegin, char @NonNull [] cbuf, int dstBegin, int count)
Copies 'count' characters from index 'srcBegin' into the given array, starting at 'dstBegin'.- Parameters:
srcBegin
- Start offset in this CharSequencecbuf
- Character arraycount
- Number of characters to copydstBegin
- Start index in the array- Throws:
NullPointerException
- If the array is null (may)IndexOutOfBoundsException
- SeeString.getChars(int, int, char[], int)
-
appendChars
public void appendChars(StringBuilder sb, int start, int end)
Appends the character range identified by start and end offset into the string builder. This is much more efficient than callingStringBuilder.append(CharSequence)
with this as the parameter, especially on Java 9+.- Parameters:
start
- Start index (inclusive)end
- End index (exclusive)- Throws:
IndexOutOfBoundsException
- SeeStringBuilder.append(CharSequence, int, int)
-
appendChars
public void appendChars(StringBuilder sb)
Append this character sequence on the given stringbuilder. This is much more efficient than callingStringBuilder.append(CharSequence)
with this as the parameter, especially on Java 9+.- Parameters:
sb
- String builder
-
getBytes
public ByteBuffer getBytes(Charset charset)
Returns the characters of this charsequence encoded with the given charset.
-
indexOf
public int indexOf(String searched, int fromIndex)
-
indexOf
public int indexOf(int ch, int fromIndex)
-
lastIndexOf
public int lastIndexOf(int ch, int fromIndex)
-
startsWith
public boolean startsWith(String prefix, int fromIndex)
-
startsWith
public boolean startsWith(String prefix)
-
startsWith
public boolean startsWith(char prefix, int fromIndex)
-
endsWith
public boolean endsWith(String suffix)
-
trimStart
public Chars trimStart()
Returns a subsequence which does not start with control characters (<= 32
). This is consistent withString.trim()
.
-
trimEnd
public Chars trimEnd()
Returns a subsequence which does not end with control characters (<= 32
). This is consistent withString.trim()
.
-
trim
public Chars trim()
LikeString.trim()
.
-
trimBlankLines
public Chars trimBlankLines()
Remove trailing and leading blank lines. The resulting string does not end with a line terminator.
-
removeSuffix
public Chars removeSuffix(String charSeq)
Remove the suffix if it is present, otherwise returns this.
-
removePrefix
public Chars removePrefix(String charSeq)
Remove the prefix if it is present, otherwise returns this.
-
contentEquals
public boolean contentEquals(CharSequence cs, boolean ignoreCase)
Returns true if this char sequence is logically equal to the parameter. This means they're equal character-by-character. This is more general thanequals(Object)
, which will only answer true if the parameter is aChars
.- Parameters:
cs
- Another char sequenceignoreCase
- Whether to ignore case- Returns:
- True if both sequences are logically equal
-
contentEquals
public boolean contentEquals(CharSequence cs)
LikecontentEquals(CharSequence, boolean)
, considering case distinctions.- Parameters:
cs
- A char sequence- Returns:
- True if both sequences are logically equal, considering case
-
length
public int length()
- Specified by:
length
in interfaceCharSequence
-
charAt
public char charAt(int index)
- Specified by:
charAt
in interfaceCharSequence
-
subSequence
public Chars subSequence(int start, int end)
- Specified by:
subSequence
in interfaceCharSequence
-
subSequence
public Chars subSequence(int start)
Returns the subsequence that starts at the given offset and ends at the end of this string. Similar toString.substring(int)
.
-
slice
public Chars slice(TextRegion region)
Slice a region of text.- Parameters:
region
- A region- Returns:
- A Chars instance
- Throws:
IndexOutOfBoundsException
- If the region is not a valid range
-
slice
public Chars slice(int off, int len)
LikesubSequence(int, int)
but with offset + length instead of start + end.- Parameters:
off
- Start of the slice (0 <= off < this.length()
)len
- Length of the slice (0 <= len <= this.length() - off
)- Returns:
- A Chars instance
- Throws:
IndexOutOfBoundsException
- If the parameters are not a valid range
-
substring
public String substring(int start, int end)
Returns the substring between the given offsets. given length.Note: Unlike slice or subSequence, this method will create a new String which involves copying the backing char array. Don't use it unnecessarily.
- Parameters:
start
- Start offset (0 <= start < this.length()
)end
- End offset (start <= end <= this.length()
)- Returns:
- A substring
- Throws:
IndexOutOfBoundsException
- If the parameters are not a valid range- See Also:
String.substring(int, int)
-
toString
public @NonNull String toString()
- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-
lines
public Iterable<Chars> lines()
Returns an iterable over the lines of this char sequence. The lines are yielded without line separators. LikeBufferedReader.readLine()
, a line delimiter isCR
,LF
orCR+LF
.
-
toStringBuilder
public StringBuilder toStringBuilder()
Returns a new stringbuilder containing the whole contents of this char sequence.
-
splits
public Iterable<Chars> splits(Pattern regex)
Split this slice into subslices, likeString.split(String)
, except it's iterated lazily.
-
newReader
public Reader newReader()
Returns a new reader for the whole contents of this char sequence.
-
-