Package io.servicetalk.buffer.api
Class CharSequences
java.lang.Object
io.servicetalk.buffer.api.CharSequences
8-bit ASCII strings factory and basic utilities helper.
This class can work with 8-bit
CharSequence
instances only, any other input will have undefined behavior.-
Method Summary
Modifier and TypeMethodDescriptionstatic int
asciiStringIndexOf
(CharSequence sequence, char c, int fromIndex) Deprecated.static int
Calculate a hash code of a byte array assuming ASCII character encoding.static boolean
Returnstrue
if the content of bothCharSequence
's are equals.static boolean
Perform a case-insensitive comparison of twoCharSequence
s.static CharSequence
Get a reference to an unmodifiable emptyCharSequence
with the same properties asnewAsciiString(Buffer)
.static boolean
equalsIgnoreCaseLower
(char a, char lowerCaseChar) Compare an unknown ascii charactera
with a known lowercase characterlowerCaseChar
in a case insensitive manner.static int
forEachByte
(CharSequence sequence, ByteProcessor visitor) Iterates over the readable bytes of thisCharSequence
with the specifiedByteProcessor
in ascending order.static int
indexOf
(CharSequence sequence, char c, int fromIndex) Find the index ofc
withinsequence
starting at indexfromIndex
.static CharSequence
newAsciiString
(Buffer input) Create a newCharSequence
from the specifiedinput
, supporting only 8-bit ASCII characters, and with a case-insensitivehashCode
.static CharSequence
newAsciiString
(CharSequence input) Create a newCharSequence
from the specifiedinput
, supporting only 8-bit ASCII characters, and with a case-insensitivehashCode
.static long
Parses theCharSequence
argument as a signed decimallong
.static boolean
regionMatches
(CharSequence cs, boolean ignoreCase, int csStart, CharSequence string, int start, int length) This methods make regionMatches operation correctly for any chars in strings.static List<CharSequence>
split
(CharSequence input, char delimiter, boolean trim) Split a givenAsciiString
to separate ones on the givendelimiter
.static Buffer
Attempt to unwrap aCharSequence
and obtain the underlyingBuffer
if possible.
-
Method Details
-
newAsciiString
Create a newCharSequence
from the specifiedinput
, supporting only 8-bit ASCII characters, and with a case-insensitivehashCode
.- Parameters:
input
- a character sequence containing only 8-bit ASCII characters.- Returns:
- a
CharSequence
-
newAsciiString
Create a newCharSequence
from the specifiedinput
, supporting only 8-bit ASCII characters, and with a case-insensitivehashCode
.- Parameters:
input
- aBuffer
containing- Returns:
- a
CharSequence
.
-
emptyAsciiString
Get a reference to an unmodifiable emptyCharSequence
with the same properties asnewAsciiString(Buffer)
.- Returns:
- a reference to an unmodifiable empty
CharSequence
with the same properties asnewAsciiString(Buffer)
.
-
unwrapBuffer
Attempt to unwrap aCharSequence
and obtain the underlyingBuffer
if possible.- Parameters:
cs
- theCharSequence
to unwrap.- Returns:
- the underlying
Buffer
ornull
.
-
forEachByte
Iterates over the readable bytes of thisCharSequence
with the specifiedByteProcessor
in ascending order.- Parameters:
sequence
- theCharSequence
to operate on.visitor
- theByteProcessor
visitor of each element.- Returns:
-1
if the processor iterated to or beyond the end of the readable bytes. The last-visited index If theByteProcessor.process(byte)
returnedfalse
.
-
contentEqualsIgnoreCase
Perform a case-insensitive comparison of twoCharSequence
s.NOTE: This only supports 8-bit ASCII.
- Parameters:
a
- firstCharSequence
to compare.b
- secondCharSequence
to compare.- Returns:
true
if bothCharSequence
's are equals when ignoring the case.
-
contentEquals
Returnstrue
if the content of bothCharSequence
's are equals. This only supports 8-bit ASCII.- Parameters:
a
- left hand side of comparison.b
- right hand side of comparison.- Returns:
true
ifa
's content equalsb
.
-
indexOf
Find the index ofc
withinsequence
starting at indexfromIndex
.- Parameters:
sequence
- TheCharSequence
to search in.c
- The character to find.fromIndex
- The index to start searching (inclusive).- Returns:
- The index of
c
or-1
otherwise.
-
asciiStringIndexOf
Deprecated.Find the index ofc
withinsequence
starting at indexfromIndex
. This version avoids instance type check for special occasions that we can know the type before hand. The input is expected to be anAsciiString
value; if the input type is not known use the genericindexOf(CharSequence, char, int)
instead.- Parameters:
sequence
- TheCharSequence
to search in.c
- The character to find.fromIndex
- The index to start searching (inclusive).- Returns:
- The index of
c
or-1
otherwise.
-
caseInsensitiveHashCode
Calculate a hash code of a byte array assuming ASCII character encoding. The resulting hash code will be case insensitive.- Parameters:
seq
- The ascii string to produce hashcode for.- Returns:
- a hashcode for the given input.
-
split
Split a givenAsciiString
to separate ones on the givendelimiter
. Trimming white-space before and after each token can be controlled by thetrim
flag This method has no support for regex.- Parameters:
input
- The initialCharSequence
to split, this experiences no side effects.delimiter
- The delimiter character.trim
- Flag to control whether the individual items must be trimmed.- Returns:
- a
List
ofCharSequence
subsequences of the input with the separated values
-
equalsIgnoreCaseLower
public static boolean equalsIgnoreCaseLower(char a, char lowerCaseChar) Compare an unknown ascii charactera
with a known lowercase characterlowerCaseChar
in a case insensitive manner.- Parameters:
a
- an unknown ascii character.lowerCaseChar
- a known to be lowercase ascii character.- Returns:
true
ifa
andlowerCaseChar
are case insensitive equal.
-
regionMatches
public static boolean regionMatches(CharSequence cs, boolean ignoreCase, int csStart, CharSequence string, int start, int length) This methods make regionMatches operation correctly for any chars in strings.- Parameters:
cs
- theCharSequence
to be processedignoreCase
- specifies if case should be ignored.csStart
- the starting offset in thecs
CharSequencestring
- theCharSequence
to compare.start
- the starting offset in the specifiedstring
.length
- the number of characters to compare.- Returns:
true
if the ranges of characters are equal,false
otherwise.
-
parseLong
Parses theCharSequence
argument as a signed decimallong
.This is the equivalent of
Long.parseLong(String)
that does not require toCharSequence.toString()
conversion.- Parameters:
cs
- aCharSequence
containing thelong
value to be parsed- Returns:
- {code long} representation of the passed
CharSequence
- Throws:
NumberFormatException
- if the passedCharSequence
cannot be parsed intolong
-
indexOf(CharSequence, char, int)
.