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.private static final long[]
static final int
Maximum number of digits in a US-ASCII-encoded int.static final int[]
Power of ten for int values.static final long
An absolute value of theInteger.MIN_VALUE
as long.private static final long[]
static final int
Maximum number of digits in a US-ASCII-encoded long.static final int[]
Long.MAX_VALUE
split into components by 8 digits max.static final int[]
Long.MIN_VALUE
split 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 byte
Byte value of the minus sign ('-
').static final byte
Byte value of zero character ('0
'). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
digitCount
(int value) Count number of digits in a positiveint
value.static int
digitCount
(long value) Count number of digits in a positivelong
value.static int
endOffset
(int value) Deprecated.static int
endOffset
(long value) Deprecated.UsedigitCount(long)
instead.static int
getDigit
(int index, byte value) Get the digit value of an ASCII encodedbyte
.static int
getDigit
(int index, char value) Get the digit value of an ASCII encodedchar
.static boolean
isDigit
(byte value) Check if thevalue
is an ASCII-encoded digit.static boolean
isEightDigitAsciiEncodedNumber
(long value) Checks if the providedvalue
represents an ASCII-encoded number which contains exactly eight digits.static boolean
isFourDigitsAsciiEncodedNumber
(int value) Checks if the providedvalue
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
(CharSequence cs, int index, int length) Parse an ASCII encoded int from aCharSequence
.static long
parseLongAscii
(CharSequence cs, int index, int length) Parse an ASCII encoded long from aCharSequence
.private static long
parseLongAsciiOverflowCheck
(CharSequence cs, int index, int length, int[] maxValue, int startIndex, int end) private static int
parsePositiveIntAscii
(CharSequence cs, int index, int length, int startIndex, int end) private static long
parsePositiveIntAsciiOverflowCheck
(CharSequence cs, int index, int length, int startIndex, int end) private static long
parsePositiveLongAscii
(CharSequence cs, int index, int length, int startIndex, int end) private static long
readEightBytesLittleEndian
(CharSequence cs, int index) private static int
readFourBytesLittleEndian
(CharSequence cs, int index) private static void
throwParseIntError
(CharSequence cs, int index, int length) private static void
throwParseIntOverflowError
(CharSequence cs, int index, int length) private static void
throwParseLongError
(CharSequence cs, int index, int length) private static void
throwParseLongOverflowError
(CharSequence cs, int index, int length)
-
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_VALUE
as 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_VALUE
split into components by 8 digits max. -
LONG_MIN_VALUE_DIGITS
public static final int[] LONG_MIN_VALUE_DIGITSLong.MIN_VALUE
split 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. -
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.UsedigitCount(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.UsedigitCount(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 positiveint
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 be3
.
-
digitCount
public static int digitCount(long value) Count number of digits in a positivelong
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 be14
.
-
isDigit
public static boolean isDigit(byte value) Check if thevalue
is an ASCII-encoded digit.- Parameters:
value
- ti be checked.- Returns:
true
if thevalue
is 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 <= 0
orcs
is 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 <= 0
orcs
is not a long value
-
isFourDigitsAsciiEncodedNumber
public static boolean isFourDigitsAsciiEncodedNumber(int value) Checks if the providedvalue
represents an ASCII-encoded number which contains exactly four digits.- Parameters:
value
- four ASCII-encoded bytes to check.- Returns:
true
if thevalue
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 providedvalue
represents an ASCII-encoded number which contains exactly eight digits.- Parameters:
value
- eoght ASCII-encoded bytes to check.- Returns:
true
if thevalue
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
-
throwParseIntOverflowError
-
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
-
throwParseLongOverflowError
-
readFourBytesLittleEndian
-
readEightBytesLittleEndian
-
digitCount(int)
instead.