Package org.agrona

Class AsciiEncoding


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

      Fields 
      Modifier and Type Field Description
      static byte[] ASCII_DIGITS
      Lookup table used for encoding ints/longs as ASCII characters.
      private static long[] INT_DIGITS  
      static int INT_MAX_DIGITS
      Maximum number of digits in a US-ASCII-encoded int.
      static int[] INT_POW_10
      Power of ten for int values.
      static long INTEGER_ABSOLUTE_MIN_VALUE
      An absolute value of the Integer.MIN_VALUE as long.
      private static long[] LONG_DIGITS  
      static int LONG_MAX_DIGITS
      Maximum number of digits in a US-ASCII-encoded long.
      static int[] LONG_MAX_VALUE_DIGITS
      Long.MAX_VALUE split into components by 8 digits max.
      static int[] LONG_MIN_VALUE_DIGITS
      Long.MIN_VALUE split into components by 8 digits max.
      static long[] LONG_POW_10
      Power of ten for long values.
      static byte[] MAX_INTEGER_VALUE
      US-ASCII-encoded byte representation of the Integer.MAX_VALUE.
      static byte[] MAX_LONG_VALUE
      US-ASCII-encoded byte representation of the Long.MAX_VALUE.
      static byte[] MIN_INTEGER_VALUE
      US-ASCII-encoded byte representation of the Integer.MIN_VALUE.
      static byte[] MIN_LONG_VALUE
      US-ASCII-encoded byte representation of the Long.MIN_VALUE.
      static byte MINUS_SIGN
      Byte value of the minus sign ('-').
      static byte ZERO
      Byte value of zero character ('0').
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private AsciiEncoding()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static int digitCount​(int value)
      Count number of digits in a positive int value.
      static int digitCount​(long value)
      Count number of digits in a positive long value.
      static int endOffset​(int value)
      Deprecated.
      Use digitCount(int) instead.
      static int endOffset​(long value)
      Deprecated.
      Use digitCount(long) instead.
      static int getDigit​(int index, byte value)
      Get the digit value of an ASCII encoded byte.
      static int getDigit​(int index, char value)
      Get the digit value of an ASCII encoded char.
      static boolean isDigit​(byte value)
      Check if the value is an ASCII-encoded digit.
      static boolean isEightDigitAsciiEncodedNumber​(long value)
      Checks if the provided value represents an ASCII-encoded number which contains exactly eight digits.
      static boolean isFourDigitsAsciiEncodedNumber​(int value)
      Checks if the provided value represents an ASCII-encoded number which contains exactly four digits.
      static int parseEightDigitsLittleEndian​(long bytes)
      Parses an eight-digit number out of an ASCII-encoded value assuming little-endian byte order.
      static int parseFourDigitsLittleEndian​(int bytes)
      Parses a four-digit number out of an ASCII-encoded value assuming little-endian byte order.
      static int parseIntAscii​(java.lang.CharSequence cs, int index, int length)
      Parse an ASCII encoded int from a CharSequence.
      static long parseLongAscii​(java.lang.CharSequence cs, int index, int length)
      Parse an ASCII encoded long from a CharSequence.
      private static long parseLongAsciiOverflowCheck​(java.lang.CharSequence cs, int index, int length, int[] maxValue, int startIndex, int end)  
      private static int parsePositiveIntAscii​(java.lang.CharSequence cs, int index, int length, int startIndex, int end)  
      private static long parsePositiveIntAsciiOverflowCheck​(java.lang.CharSequence cs, int index, int length, int startIndex, int end)  
      private static long parsePositiveLongAscii​(java.lang.CharSequence cs, int index, int length, int startIndex, int end)  
      private static long readEightBytesLittleEndian​(java.lang.CharSequence cs, int index)  
      private static int readFourBytesLittleEndian​(java.lang.CharSequence cs, int index)  
      private static void throwParseIntError​(java.lang.CharSequence cs, int index, int length)  
      private static void throwParseIntOverflowError​(java.lang.CharSequence cs, int index, int length)  
      private static void throwParseLongError​(java.lang.CharSequence cs, int index, int length)  
      private static void throwParseLongOverflowError​(java.lang.CharSequence cs, int index, int length)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • INT_MAX_DIGITS

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

        public static final int LONG_MAX_DIGITS
        Maximum number of digits in a US-ASCII-encoded long.
        See Also:
        Constant Field Values
      • 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:
        Constant Field Values
      • ZERO

        public static final byte ZERO
        Byte value of zero character ('0').
        See Also:
        Constant Field Values
      • 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 Detail

      • AsciiEncoding

        private AsciiEncoding()
    • Method Detail

      • 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:
        digitCount(int)
      • 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(long)
      • 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​(java.lang.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​(java.lang.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​(java.lang.CharSequence cs,
                                                 int index,
                                                 int length,
                                                 int startIndex,
                                                 int end)
      • parsePositiveIntAsciiOverflowCheck

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

        private static void throwParseIntError​(java.lang.CharSequence cs,
                                               int index,
                                               int length)
      • throwParseIntOverflowError

        private static void throwParseIntOverflowError​(java.lang.CharSequence cs,
                                                       int index,
                                                       int length)
      • parsePositiveLongAscii

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

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

        private static void throwParseLongError​(java.lang.CharSequence cs,
                                                int index,
                                                int length)
      • throwParseLongOverflowError

        private static void throwParseLongOverflowError​(java.lang.CharSequence cs,
                                                        int index,
                                                        int length)
      • readFourBytesLittleEndian

        private static int readFourBytesLittleEndian​(java.lang.CharSequence cs,
                                                     int index)
      • readEightBytesLittleEndian

        private static long readEightBytesLittleEndian​(java.lang.CharSequence cs,
                                                       int index)