Package org.agrona

Class AsciiEncoding

java.lang.Object
org.agrona.AsciiEncoding

public final class AsciiEncoding extends Object
Helper for dealing with ASCII encoding of numbers.
  • Field Details

    • INT_MAX_DIGITS

      public static final int INT_MAX_DIGITS
      Maximum number of digits in a US-ASCII-encoded int.
      See Also:
    • LONG_MAX_DIGITS

      public static final int LONG_MAX_DIGITS
      Maximum number of digits in a US-ASCII-encoded long.
      See Also:
    • INTEGER_ABSOLUTE_MIN_VALUE

      public static final long INTEGER_ABSOLUTE_MIN_VALUE
      An absolute value of the Integer.MIN_VALUE as long.
    • MIN_INTEGER_VALUE

      public static final byte[] MIN_INTEGER_VALUE
      US-ASCII-encoded byte representation of the Integer.MIN_VALUE.
    • MAX_INTEGER_VALUE

      public static final byte[] MAX_INTEGER_VALUE
      US-ASCII-encoded byte representation of the Integer.MAX_VALUE.
    • MIN_LONG_VALUE

      public static final byte[] MIN_LONG_VALUE
      US-ASCII-encoded byte representation of the Long.MIN_VALUE.
    • MAX_LONG_VALUE

      public static final byte[] MAX_LONG_VALUE
      US-ASCII-encoded byte representation of the Long.MAX_VALUE.
    • MINUS_SIGN

      public static final byte MINUS_SIGN
      Byte value of the minus sign ('-').
      See Also:
    • ZERO

      public static final byte ZERO
      Byte value of zero character ('0').
      See Also:
    • ASCII_DIGITS

      public static final byte[] ASCII_DIGITS
      Lookup table used for encoding ints/longs as ASCII characters.
    • LONG_MAX_VALUE_DIGITS

      public static final int[] LONG_MAX_VALUE_DIGITS
      Long.MAX_VALUE split into components by 8 digits max.
    • LONG_MIN_VALUE_DIGITS

      public static final int[] LONG_MIN_VALUE_DIGITS
      Long.MIN_VALUE split into components by 8 digits max.
    • INT_POW_10

      public static final int[] INT_POW_10
      Power of ten for int values.
    • LONG_POW_10

      public static final long[] LONG_POW_10
      Power of ten for long values.
    • INT_DIGITS

      private static final long[] INT_DIGITS
    • LONG_DIGITS

      private static final long[] LONG_DIGITS
  • Constructor Details

    • AsciiEncoding

      private AsciiEncoding()
  • Method Details

    • endOffset

      @Deprecated public static int endOffset(int value)
      Deprecated.
      Use digitCount(int) instead.
      Calling this method is equivalent of doing:
       digitCount(value) - 1
       
      Parameters:
      value - to find the end encoded character offset.
      Returns:
      the offset at which the encoded value will end.
      See Also:
    • endOffset

      @Deprecated public static int endOffset(long value)
      Deprecated.
      Use digitCount(long) instead.
      Calling this method is equivalent of doing:
       digitCount(value) - 1
       
      Parameters:
      value - to find the end encoded character offset.
      Returns:
      the offset at which the encoded value will end.
      See Also:
    • digitCount

      public static int digitCount(int value)
      Count number of digits in a positive int value.

      Implementation is based on the Kendall Willets' idea as presented in the Computing the number of digits of an integer even faster blog post.

      Use org.agrona.AsciiEncodingTest#printDigitCountIntTable() to regenerate lookup table.

      Parameters:
      value - to count number of digits int.
      Returns:
      number of digits in a number, e.g. if input value is 123 then the result will be 3.
    • digitCount

      public static int digitCount(long value)
      Count number of digits in a positive long value.

      Implementation is based on the Kendall Willets' idea as presented in the Computing the number of digits of an integer even faster blog post.

      Use org.agrona.AsciiEncodingTest#printDigitCountLongTable() to regenerate lookup table.

      Parameters:
      value - to count number of digits int.
      Returns:
      number of digits in a number, e.g. if input value is 12345678909876 then the result will be 14.
    • isDigit

      public static boolean isDigit(byte value)
      Check if the value is an ASCII-encoded digit.
      Parameters:
      value - ti be checked.
      Returns:
      true if the value is an ASCII-encoded digit.
    • getDigit

      public static int getDigit(int index, byte value)
      Get the digit value of an ASCII encoded byte.
      Parameters:
      index - within the string the value is encoded.
      value - of the encoding in ASCII.
      Returns:
      the digit value of the encoded ASCII.
      Throws:
      AsciiNumberFormatException - if the value is not a digit.
    • getDigit

      public static int getDigit(int index, char value)
      Get the digit value of an ASCII encoded char.
      Parameters:
      index - within the string the value is encoded.
      value - of the encoding in ASCII.
      Returns:
      the digit value of the encoded ASCII.
      Throws:
      AsciiNumberFormatException - if the value is not a digit.
    • parseIntAscii

      public static int parseIntAscii(CharSequence cs, int index, int length)
      Parse an ASCII encoded int from a CharSequence.
      Parameters:
      cs - to parse.
      index - at which the number begins.
      length - of the encoded number in characters.
      Returns:
      the parsed value.
      Throws:
      AsciiNumberFormatException - if length <= 0 or cs is not an int value
    • parseLongAscii

      public static long parseLongAscii(CharSequence cs, int index, int length)
      Parse an ASCII encoded long from a CharSequence.
      Parameters:
      cs - to parse.
      index - at which the number begins.
      length - of the encoded number in characters.
      Returns:
      the parsed value.
      Throws:
      AsciiNumberFormatException - if length <= 0 or cs is not a long value
    • isFourDigitsAsciiEncodedNumber

      public static boolean isFourDigitsAsciiEncodedNumber(int value)
      Checks if the provided value represents an ASCII-encoded number which contains exactly four digits.
      Parameters:
      value - four ASCII-encoded bytes to check.
      Returns:
      true if the value is an ASCII-encoded number with four digits in it.
    • parseFourDigitsLittleEndian

      public static int parseFourDigitsLittleEndian(int bytes)
      Parses a four-digit number out of an ASCII-encoded value assuming little-endian byte order.
      Parameters:
      bytes - ASCII-encoded value in little-endian byte order.
      Returns:
      int value with four digits.
    • isEightDigitAsciiEncodedNumber

      public static boolean isEightDigitAsciiEncodedNumber(long value)
      Checks if the provided value represents an ASCII-encoded number which contains exactly eight digits.
      Parameters:
      value - eoght ASCII-encoded bytes to check.
      Returns:
      true if the value is an ASCII-encoded number with eight digits in it.
    • parseEightDigitsLittleEndian

      public static int parseEightDigitsLittleEndian(long bytes)
      Parses an eight-digit number out of an ASCII-encoded value assuming little-endian byte order.
      Parameters:
      bytes - ASCII-encoded value in little-endian byte order.
      Returns:
      int value with eight digits.
    • parsePositiveIntAscii

      private static int parsePositiveIntAscii(CharSequence cs, int index, int length, int startIndex, int end)
    • parsePositiveIntAsciiOverflowCheck

      private static long parsePositiveIntAsciiOverflowCheck(CharSequence cs, int index, int length, int startIndex, int end)
    • throwParseIntError

      private static void throwParseIntError(CharSequence cs, int index, int length)
    • throwParseIntOverflowError

      private static void throwParseIntOverflowError(CharSequence cs, int index, int length)
    • parsePositiveLongAscii

      private static long parsePositiveLongAscii(CharSequence cs, int index, int length, int startIndex, int end)
    • parseLongAsciiOverflowCheck

      private static long parseLongAsciiOverflowCheck(CharSequence cs, int index, int length, int[] maxValue, int startIndex, int end)
    • throwParseLongError

      private static void throwParseLongError(CharSequence cs, int index, int length)
    • throwParseLongOverflowError

      private static void throwParseLongOverflowError(CharSequence cs, int index, int length)
    • readFourBytesLittleEndian

      private static int readFourBytesLittleEndian(CharSequence cs, int index)
    • readEightBytesLittleEndian

      private static long readEightBytesLittleEndian(CharSequence cs, int index)