APIdia Launch Partner
Top Description Fields Constructors Methods
jdk.internal.util

public final Class DecimalDigits

extends Object
Class Inheritance
Imports
jdk.internal.access.JavaLangAccess, .SharedSecrets, jdk.internal.misc.Unsafe, jdk.internal.vm.annotation.Stable

Digits class for decimal digits.
Since
21

Field Summary

Modifier and TypeField and Description
private static final short[]
DIGITS

Each element of the array represents the packaging of two ascii characters based on little endian:

     00 -> '0' | ('0' << 8) -> 0x3030
     01 -> '0' | ('1' << 8) -> 0x3130
     02 -> '0' | ('2' << 8) -> 0x3230

    ...
private static final JavaLangAccess
private static final Unsafe

Constructor Summary

AccessConstructor and Description
private
DecimalDigits()

Constructor.

Method Summary

Modifier and TypeMethod and Description
public static void
appendPair(StringBuilder
the StringBuilder to append to.
buf
,
int
the int value (should be between 0 and 99 inclusive).
v
)

Appends the two-digit string representation of the int argument to the given StringBuilder.

public static void
appendQuad(StringBuilder
the StringBuilder to append to.
buf
,
int
the int value (should be between 0 and 9999 inclusive).
v
)

Appends the four-digit string representation of the int argument to the given StringBuilder.

public static int

Returns:

index of the most significant digit or minus sign, if present
getChars
(long
value to convert
i
,
int
next index, after the least significant digit
index
,
char[]
target buffer, UTF16-coded.
buf
)

This is a variant of DecimalDigits#uncheckedGetCharsUTF16(long, int, byte[]), but for UTF-16 coder.

public static void
putPair(char[]
byte buffer to copy into
buf
,
int
insert point
charPos
,
int
to convert
v
)

Insert the 2-chars integer into the buf as 2 decimal digit ASCII chars, only least significant 16 bits of v are used.

public static int

Returns:

string size
stringSize
(int
int value
x
)

Returns the string representation size for a given int value.

public static int

Returns:

string size
stringSize
(long
long value
x
)

Returns the string representation size for a given long value.

public static int

Returns:

index of the most significant digit or minus sign, if present
uncheckedGetCharsLatin1
(int
value to convert
i
,
int
next index, after the least significant digit
index
,
byte[]
target buffer, Latin1-encoded
buf
)

Places characters representing the integer i into the character array buf.

public static int

Returns:

index of the most significant digit or minus sign, if present
uncheckedGetCharsLatin1
(long
value to convert
i
,
int
next index, after the least significant digit
index
,
byte[]
target buffer, Latin1-encoded
buf
)

Places characters representing the long i into the character array buf.

public static int

Returns:

index of the most significant digit or minus sign, if present
uncheckedGetCharsUTF16
(int
value to convert
i
,
int
next index, after the least significant digit
index
,
byte[]
target buffer, UTF16-coded.
buf
)

This is a variant of DecimalDigits#uncheckedGetCharsLatin1(int, int, byte[]), but for UTF-16 coder.

public static int

Returns:

index of the most significant digit or minus sign, if present
uncheckedGetCharsUTF16
(long
value to convert
i
,
int
next index, after the least significant digit
index
,
byte[]
target buffer, UTF16-coded.
buf
)

This is a variant of DecimalDigits#uncheckedGetCharsLatin1(long, int, byte[]), but for UTF-16 coder.

private static void
uncheckedPutCharLatin1(byte[] buf, int charPos, int c)

private static void
uncheckedPutCharUTF16(byte[] buf, int charPos, int c)

public static void
uncheckedPutPairLatin1(byte[]
byte buffer to copy into
buf
,
int
insert point
charPos
,
int
to convert
v
)

Insert the 2-bytes integer into the buf as 2 decimal digit ASCII bytes, only least significant 16 bits of v are used.

public static void
uncheckedPutPairUTF16(byte[]
byte buffer to copy into
buf
,
int
insert point
charPos
,
int
to convert
v
)

Insert the 2-chars integer into the buf as 2 decimal digit UTF16 bytes, only least significant 16 bits of v are used.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DIGITSback to summary
private static final short[] DIGITS

Each element of the array represents the packaging of two ascii characters based on little endian:

     00 -> '0' | ('0' << 8) -> 0x3030
     01 -> '0' | ('1' << 8) -> 0x3130
     02 -> '0' | ('2' << 8) -> 0x3230

    ...

     10 -> '1' | ('0' << 8) -> 0x3031
     11 -> '1' | ('1' << 8) -> 0x3131
     12 -> '1' | ('2' << 8) -> 0x3231

    ...

     97 -> '9' | ('7' << 8) -> 0x3739
     98 -> '9' | ('8' << 8) -> 0x3839
     99 -> '9' | ('9' << 8) -> 0x3939
Annotations
@Stable
JLAback to summary
private static final JavaLangAccess JLA
UNSAFEback to summary
private static final Unsafe UNSAFE

Constructor Detail

DecimalDigitsback to summary
private DecimalDigits()

Constructor.

Method Detail

appendPairback to summary
public static void appendPair(StringBuilder buf, int v)

Appends the two-digit string representation of the int argument to the given StringBuilder.

The integer v is formatted as two decimal digits. Values from 0 to 9 are formatted with a leading zero (e.g., 5 becomes "05"), and values from 10 to 99 are formatted as regular two-digit numbers. If the value is outside the range 0-99, the behavior is unspecified.

Parameters
buf:StringBuilder

the StringBuilder to append to.

v:int

the int value (should be between 0 and 99 inclusive).

appendQuadback to summary
public static void appendQuad(StringBuilder buf, int v)

Appends the four-digit string representation of the int argument to the given StringBuilder.

The integer v is formatted as four decimal digits. Values from 0 to 9 are formatted with leading zeros (e.g., 5 becomes "0005"), values from 10 to 99 add two leading zeros (e.g., 25 becomes "0025"), values from 100 to 999 add one leading zero (e.g., 123 becomes "0123"), and values from 1000 to 9999 have no leading zeros. If the value is outside the range 0-9999, the behavior is unspecified.

Parameters
buf:StringBuilder

the StringBuilder to append to.

v:int

the int value (should be between 0 and 9999 inclusive).

getCharsback to summary
public static int getChars(long i, int index, char[] buf)

This is a variant of DecimalDigits#uncheckedGetCharsUTF16(long, int, byte[]), but for UTF-16 coder.

Parameters
i:long

value to convert

index:int

next index, after the least significant digit

buf:char[]

target buffer, UTF16-coded.

Returns:int

index of the most significant digit or minus sign, if present

putPairback to summary
public static void putPair(char[] buf, int charPos, int v)

Insert the 2-chars integer into the buf as 2 decimal digit ASCII chars, only least significant 16 bits of v are used.

Parameters
buf:char[]

byte buffer to copy into

charPos:int

insert point

v:int

to convert

stringSizeback to summary
public static int stringSize(int x)

Returns the string representation size for a given int value.

Implementation Note

There are other ways to compute this: e.g. binary search, but values are biased heavily towards zero, and therefore linear search wins. The iteration results are also routinely inlined in the generated code after loop unrolling.

Parameters
x:int

int value

Returns:int

string size

stringSizeback to summary
public static int stringSize(long x)

Returns the string representation size for a given long value.

Implementation Note

There are other ways to compute this: e.g. binary search, but values are biased heavily towards zero, and therefore linear search wins. The iteration results are also routinely inlined in the generated code after loop unrolling.

Parameters
x:long

long value

Returns:int

string size

uncheckedGetCharsLatin1back to summary
public static int uncheckedGetCharsLatin1(int i, int index, byte[] buf)

Places characters representing the integer i into the character array buf. The characters are placed into the buffer backwards starting with the least significant digit at the specified index (exclusive), and working backwards from there.

Implementation Note

This method converts positive inputs into negative values, to cover the Integer.MIN_VALUE case. Converting otherwise (negative to positive) will expose -Integer.MIN_VALUE that overflows integer.

WARNING: This method does not perform any bound checks.

Parameters
i:int

value to convert

index:int

next index, after the least significant digit

buf:byte[]

target buffer, Latin1-encoded

Returns:int

index of the most significant digit or minus sign, if present

uncheckedGetCharsLatin1back to summary
public static int uncheckedGetCharsLatin1(long i, int index, byte[] buf)

Places characters representing the long i into the character array buf. The characters are placed into the buffer backwards starting with the least significant digit at the specified index (exclusive), and working backwards from there.

Implementation Note

This method converts positive inputs into negative values, to cover the Long.MIN_VALUE case. Converting otherwise (negative to positive) will expose -Long.MIN_VALUE that overflows long.

WARNING: This method does not perform any bound checks.

Parameters
i:long

value to convert

index:int

next index, after the least significant digit

buf:byte[]

target buffer, Latin1-encoded

Returns:int

index of the most significant digit or minus sign, if present

uncheckedGetCharsUTF16back to summary
public static int uncheckedGetCharsUTF16(int i, int index, byte[] buf)

This is a variant of DecimalDigits#uncheckedGetCharsLatin1(int, int, byte[]), but for UTF-16 coder.

Warning

This method does not perform any bound checks.

Parameters
i:int

value to convert

index:int

next index, after the least significant digit

buf:byte[]

target buffer, UTF16-coded.

Returns:int

index of the most significant digit or minus sign, if present

uncheckedGetCharsUTF16back to summary
public static int uncheckedGetCharsUTF16(long i, int index, byte[] buf)

This is a variant of DecimalDigits#uncheckedGetCharsLatin1(long, int, byte[]), but for UTF-16 coder.

Warning

This method does not perform any bound checks.

Parameters
i:long

value to convert

index:int

next index, after the least significant digit

buf:byte[]

target buffer, UTF16-coded.

Returns:int

index of the most significant digit or minus sign, if present

uncheckedPutCharLatin1back to summary
private static void uncheckedPutCharLatin1(byte[] buf, int charPos, int c)
uncheckedPutCharUTF16back to summary
private static void uncheckedPutCharUTF16(byte[] buf, int charPos, int c)
uncheckedPutPairLatin1back to summary
public static void uncheckedPutPairLatin1(byte[] buf, int charPos, int v)

Insert the 2-bytes integer into the buf as 2 decimal digit ASCII bytes, only least significant 16 bits of v are used.

Warning

This method does not perform any bound checks.

Parameters
buf:byte[]

byte buffer to copy into

charPos:int

insert point

v:int

to convert

uncheckedPutPairUTF16back to summary
public static void uncheckedPutPairUTF16(byte[] buf, int charPos, int v)

Insert the 2-chars integer into the buf as 2 decimal digit UTF16 bytes, only least significant 16 bits of v are used.

Warning

This method does not perform any bound checks.

Parameters
buf:byte[]

byte buffer to copy into

charPos:int

insert point

v:int

to convert