Class CharSequences

java.lang.Object
io.servicetalk.buffer.api.CharSequences

public final class CharSequences extends Object
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 Details

    • newAsciiString

      public static CharSequence newAsciiString(CharSequence input)
      Create a new CharSequence from the specified input, supporting only 8-bit ASCII characters, and with a case-insensitive hashCode.
      Parameters:
      input - a character sequence containing only 8-bit ASCII characters.
      Returns:
      a CharSequence
    • newAsciiString

      public static CharSequence newAsciiString(Buffer input)
      Create a new CharSequence from the specified input, supporting only 8-bit ASCII characters, and with a case-insensitive hashCode.
      Parameters:
      input - a Buffer containing
      Returns:
      a CharSequence.
    • emptyAsciiString

      public static CharSequence emptyAsciiString()
      Get a reference to an unmodifiable empty CharSequence with the same properties as newAsciiString(Buffer).
      Returns:
      a reference to an unmodifiable empty CharSequence with the same properties as newAsciiString(Buffer).
    • unwrapBuffer

      @Nullable public static Buffer unwrapBuffer(CharSequence cs)
      Attempt to unwrap a CharSequence and obtain the underlying Buffer if possible.
      Parameters:
      cs - the CharSequence to unwrap.
      Returns:
      the underlying Buffer or null.
    • forEachByte

      public static int forEachByte(CharSequence sequence, ByteProcessor visitor)
      Iterates over the readable bytes of this CharSequence with the specified ByteProcessor in ascending order.
      Parameters:
      sequence - the CharSequence to operate on.
      visitor - the ByteProcessor visitor of each element.
      Returns:
      -1 if the processor iterated to or beyond the end of the readable bytes. The last-visited index If the ByteProcessor.process(byte) returned false.
    • contentEqualsIgnoreCase

      public static boolean contentEqualsIgnoreCase(@Nullable CharSequence a, @Nullable CharSequence b)
      Perform a case-insensitive comparison of two CharSequences.

      NOTE: This only supports 8-bit ASCII.

      Parameters:
      a - first CharSequence to compare.
      b - second CharSequence to compare.
      Returns:
      true if both CharSequence's are equals when ignoring the case.
    • contentEquals

      public static boolean contentEquals(@Nullable CharSequence a, @Nullable CharSequence b)
      Returns true if the content of both CharSequence's are equals. This only supports 8-bit ASCII.
      Parameters:
      a - left hand side of comparison.
      b - right hand side of comparison.
      Returns:
      true if a's content equals b.
    • indexOf

      public static int indexOf(CharSequence sequence, char c, int fromIndex)
      Find the index of c within sequence starting at index fromIndex.
      Parameters:
      sequence - The CharSequence 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 public static int asciiStringIndexOf(CharSequence sequence, char c, int fromIndex)
      Find the index of c within sequence starting at index fromIndex. This version avoids instance type check for special occasions that we can know the type before hand. The input is expected to be an AsciiString value; if the input type is not known use the generic indexOf(CharSequence, char, int) instead.
      Parameters:
      sequence - The CharSequence to search in.
      c - The character to find.
      fromIndex - The index to start searching (inclusive).
      Returns:
      The index of c or -1 otherwise.
    • caseInsensitiveHashCode

      public static int caseInsensitiveHashCode(CharSequence seq)
      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

      public static List<CharSequence> split(CharSequence input, char delimiter, boolean trim)
      Split a given AsciiString to separate ones on the given delimiter. Trimming white-space before and after each token can be controlled by the trim flag This method has no support for regex.
      Parameters:
      input - The initial CharSequence 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 of CharSequence subsequences of the input with the separated values
    • equalsIgnoreCaseLower

      public static boolean equalsIgnoreCaseLower(char a, char lowerCaseChar)
      Compare an unknown ascii character a with a known lowercase character lowerCaseChar in a case insensitive manner.
      Parameters:
      a - an unknown ascii character.
      lowerCaseChar - a known to be lowercase ascii character.
      Returns:
      true if a and lowerCaseChar 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 - the CharSequence to be processed
      ignoreCase - specifies if case should be ignored.
      csStart - the starting offset in the cs CharSequence
      string - the CharSequence to compare.
      start - the starting offset in the specified string.
      length - the number of characters to compare.
      Returns:
      true if the ranges of characters are equal, false otherwise.
    • parseLong

      public static long parseLong(CharSequence cs) throws NumberFormatException
      Parses the CharSequence argument as a signed decimal long.

      This is the equivalent of Long.parseLong(String) that does not require to CharSequence.toString() conversion.

      Parameters:
      cs - a CharSequence containing the long value to be parsed
      Returns:
      {code long} representation of the passed CharSequence
      Throws:
      NumberFormatException - if the passed CharSequence cannot be parsed into long