Package org.agrona
Class AsciiEncoding
java.lang.Object
org.agrona.AsciiEncoding
Helper for dealing with ASCII encoding of numbers.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]Lookup table used for encoding ints/longs as ASCII characters.static final intMaximum number of digits in a US-ASCII-encoded int.static final int[]Power of ten for int values.static final longAn absolute value of theInteger.MIN_VALUEas long.static final intMaximum number of digits in a US-ASCII-encoded long.static final int[]Long.MAX_VALUEsplit into components by 8 digits max.static final int[]Long.MIN_VALUEsplit into components by 8 digits max.static final long[]Power of ten for long values.static final byte[]US-ASCII-encoded byte representation of theInteger.MAX_VALUE.static final byte[]US-ASCII-encoded byte representation of theLong.MAX_VALUE.static final byte[]US-ASCII-encoded byte representation of theInteger.MIN_VALUE.static final byte[]US-ASCII-encoded byte representation of theLong.MIN_VALUE.static final byteByte value of the minus sign ('-').static final byteByte value of zero character ('0'). -
Method Summary
Modifier and TypeMethodDescriptionstatic intdigitCount(int value) Count number of digits in a positiveintvalue.static intdigitCount(long value) Count number of digits in a positivelongvalue.static intendOffset(int value) Deprecated.static intendOffset(long value) Deprecated.UsedigitCount(long)instead.static intgetDigit(int index, byte value) Get the digit value of an ASCII encodedbyte.static intgetDigit(int index, char value) Get the digit value of an ASCII encodedchar.static booleanisDigit(byte value) Check if thevalueis an ASCII-encoded digit.static booleanisEightDigitAsciiEncodedNumber(long value) Checks if the providedvaluerepresents an ASCII-encoded number which contains exactly eight digits.static booleanisFourDigitsAsciiEncodedNumber(int value) Checks if the providedvaluerepresents an ASCII-encoded number which contains exactly four digits.static intparseEightDigitsLittleEndian(long bytes) Parses an eight-digit number out of an ASCII-encoded value assuming little-endian byte order.static intparseFourDigitsLittleEndian(int bytes) Parses a four-digit number out of an ASCII-encoded value assuming little-endian byte order.static intparseIntAscii(CharSequence cs, int index, int length) Parse an ASCII encoded int from aCharSequence.static longparseLongAscii(CharSequence cs, int index, int length) Parse an ASCII encoded long from aCharSequence.
-
Field Details
-
INT_MAX_DIGITS
public static final int INT_MAX_DIGITSMaximum number of digits in a US-ASCII-encoded int.- See Also:
-
LONG_MAX_DIGITS
public static final int LONG_MAX_DIGITSMaximum number of digits in a US-ASCII-encoded long.- See Also:
-
INTEGER_ABSOLUTE_MIN_VALUE
public static final long INTEGER_ABSOLUTE_MIN_VALUEAn absolute value of theInteger.MIN_VALUEas long. -
MIN_INTEGER_VALUE
public static final byte[] MIN_INTEGER_VALUEUS-ASCII-encoded byte representation of theInteger.MIN_VALUE. -
MAX_INTEGER_VALUE
public static final byte[] MAX_INTEGER_VALUEUS-ASCII-encoded byte representation of theInteger.MAX_VALUE. -
MIN_LONG_VALUE
public static final byte[] MIN_LONG_VALUEUS-ASCII-encoded byte representation of theLong.MIN_VALUE. -
MAX_LONG_VALUE
public static final byte[] MAX_LONG_VALUEUS-ASCII-encoded byte representation of theLong.MAX_VALUE. -
MINUS_SIGN
public static final byte MINUS_SIGNByte value of the minus sign ('-').- See Also:
-
ZERO
public static final byte ZEROByte value of zero character ('0').- See Also:
-
ASCII_DIGITS
public static final byte[] ASCII_DIGITSLookup table used for encoding ints/longs as ASCII characters. -
LONG_MAX_VALUE_DIGITS
public static final int[] LONG_MAX_VALUE_DIGITSLong.MAX_VALUEsplit into components by 8 digits max. -
LONG_MIN_VALUE_DIGITS
public static final int[] LONG_MIN_VALUE_DIGITSLong.MIN_VALUEsplit into components by 8 digits max. -
INT_POW_10
public static final int[] INT_POW_10Power of ten for int values. -
LONG_POW_10
public static final long[] LONG_POW_10Power of ten for long values.
-
-
Method Details
-
endOffset
Deprecated.UsedigitCount(int)instead.Calling this method is equivalent of doing the following.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.UsedigitCount(long)instead.Calling this method is equivalent of doing the following.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 positiveintvalue.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
123then the result will be3.
-
digitCount
public static int digitCount(long value) Count number of digits in a positivelongvalue.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
12345678909876then the result will be14.
-
isDigit
public static boolean isDigit(byte value) Check if thevalueis an ASCII-encoded digit.- Parameters:
value- ti be checked.- Returns:
trueif thevalueis an ASCII-encoded digit.
-
getDigit
public static int getDigit(int index, byte value) Get the digit value of an ASCII encodedbyte.- 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 encodedchar.- 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
Parse an ASCII encoded int from aCharSequence.- Parameters:
cs- to parse.index- at which the number begins.length- of the encoded number in characters.- Returns:
- the parsed value.
- Throws:
AsciiNumberFormatException- iflength <= 0orcsis not an int value
-
parseLongAscii
Parse an ASCII encoded long from aCharSequence.- Parameters:
cs- to parse.index- at which the number begins.length- of the encoded number in characters.- Returns:
- the parsed value.
- Throws:
AsciiNumberFormatException- iflength <= 0orcsis not a long value
-
isFourDigitsAsciiEncodedNumber
public static boolean isFourDigitsAsciiEncodedNumber(int value) Checks if the providedvaluerepresents an ASCII-encoded number which contains exactly four digits.- Parameters:
value- four ASCII-encoded bytes to check.- Returns:
trueif thevalueis 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:
intvalue with four digits.
-
isEightDigitAsciiEncodedNumber
public static boolean isEightDigitAsciiEncodedNumber(long value) Checks if the providedvaluerepresents an ASCII-encoded number which contains exactly eight digits.- Parameters:
value- eoght ASCII-encoded bytes to check.- Returns:
trueif thevalueis 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:
intvalue with eight digits.
-
digitCount(int)instead.