Skip to content

Commit e084516

Browse files
wenshaocl4es
authored andcommitted
8315968: Move java.util.Digits to jdk.internal.util and refactor to reduce duplication
Reviewed-by: rriggs, liach, redestad
1 parent d75d977 commit e084516

File tree

8 files changed

+76
-79
lines changed

8 files changed

+76
-79
lines changed

src/java.base/share/classes/java/lang/StringLatin1.java

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Stream;
3434
import java.util.stream.StreamSupport;
3535
import jdk.internal.util.ArraysSupport;
36+
import jdk.internal.util.DecimalDigits;
3637
import jdk.internal.util.ByteArrayLittleEndian;
3738
import jdk.internal.vm.annotation.IntrinsicCandidate;
3839
import jdk.internal.vm.annotation.Stable;
@@ -43,41 +44,6 @@
4344
import static java.lang.String.checkOffset;
4445

4546
final class StringLatin1 {
46-
47-
/**
48-
* Each element of the array represents the packaging of two ascii characters based on little endian:<p>
49-
* <pre>
50-
* 00 -> '0' | ('0' << 8) -> 0x3030
51-
* 01 -> '1' | ('0' << 8) -> 0x3130
52-
* 02 -> '2' | ('0' << 8) -> 0x3230
53-
*
54-
* ...
55-
*
56-
* 10 -> '0' | ('1' << 8) -> 0x3031
57-
* 11 -> '1' | ('1' << 8) -> 0x3131
58-
* 12 -> '2' | ('1' << 8) -> 0x3231
59-
*
60-
* ...
61-
*
62-
* 97 -> '7' | ('9' << 8) -> 0x3739
63-
* 98 -> '8' | ('9' << 8) -> 0x3839
64-
* 99 -> '9' | ('9' << 8) -> 0x3939
65-
* </pre>
66-
*/
67-
@Stable
68-
static final short[] PACKED_DIGITS = new short[] {
69-
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
70-
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
71-
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
72-
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
73-
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
74-
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
75-
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
76-
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
77-
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
78-
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939
79-
};
80-
8147
public static char charAt(byte[] value, int index) {
8248
checkIndex(index, value.length);
8349
return (char)(value[index] & 0xff);
@@ -148,13 +114,13 @@ static int getChars(int i, int index, byte[] buf) {
148114
r = (q * 100) - i;
149115
i = q;
150116
charPos -= 2;
151-
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[r]);
117+
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair(r));
152118
}
153119

154120
// We know there are at most two digits left at this point.
155121
if (i < -9) {
156122
charPos -= 2;
157-
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[-i]);
123+
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair(-i));
158124
} else {
159125
buf[--charPos] = (byte)('0' - i);
160126
}
@@ -196,7 +162,7 @@ static int getChars(long i, int index, byte[] buf) {
196162
while (i <= Integer.MIN_VALUE) {
197163
q = i / 100;
198164
charPos -= 2;
199-
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[(int)((q * 100) - i)]);
165+
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair((int)((q * 100) - i)));
200166
i = q;
201167
}
202168

@@ -206,14 +172,14 @@ static int getChars(long i, int index, byte[] buf) {
206172
while (i2 <= -100) {
207173
q2 = i2 / 100;
208174
charPos -= 2;
209-
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[(q2 * 100) - i2]);
175+
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair((q2 * 100) - i2));
210176
i2 = q2;
211177
}
212178

213179
// We know there are at most two digits left at this point.
214180
if (i2 < -9) {
215181
charPos -= 2;
216-
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[-i2]);
182+
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair(-i2));
217183
} else {
218184
buf[--charPos] = (byte)('0' - i2);
219185
}

src/java.base/share/classes/java/lang/StringUTF16.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.stream.Stream;
3434
import java.util.stream.StreamSupport;
3535
import jdk.internal.util.ArraysSupport;
36+
import jdk.internal.util.DecimalDigits;
3637
import jdk.internal.vm.annotation.DontInline;
3738
import jdk.internal.vm.annotation.ForceInline;
3839
import jdk.internal.vm.annotation.IntrinsicCandidate;
@@ -1612,7 +1613,7 @@ static int getChars(long i, int index, byte[] buf) {
16121613
}
16131614

16141615
private static void putPair(byte[] buf, int charPos, int v) {
1615-
int packed = (int) StringLatin1.PACKED_DIGITS[v];
1616+
int packed = (int) DecimalDigits.digitPair(v);
16161617
putChar(buf, charPos, packed & 0xFF);
16171618
putChar(buf, charPos + 1, packed >> 8);
16181619
}

src/java.base/share/classes/java/util/FormatItem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
import jdk.internal.access.JavaLangAccess;
3737
import jdk.internal.access.SharedSecrets;
3838
import jdk.internal.util.FormatConcatItem;
39+
import jdk.internal.util.DecimalDigits;
40+
import jdk.internal.util.HexDigits;
41+
import jdk.internal.util.OctalDigits;
3942

4043
import static java.lang.invoke.MethodType.methodType;
4144

src/java.base/share/classes/java/util/UUID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import jdk.internal.access.JavaLangAccess;
3333
import jdk.internal.access.SharedSecrets;
3434
import jdk.internal.util.ByteArray;
35+
import jdk.internal.util.HexDigits;
3536

3637
/**
3738
* A class that represents an immutable universally unique identifier (UUID).

src/java.base/share/classes/java/util/DecimalDigits.java renamed to src/java.base/share/classes/jdk/internal/util/DecimalDigits.java

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* questions.
2424
*/
2525

26-
package java.util;
26+
package jdk.internal.util;
2727

2828
import java.lang.invoke.MethodHandle;
2929

@@ -34,29 +34,46 @@
3434
*
3535
* @since 21
3636
*/
37-
final class DecimalDigits implements Digits {
37+
public final class DecimalDigits implements Digits {
38+
39+
/**
40+
* Each element of the array represents the packaging of two ascii characters based on little endian:<p>
41+
* <pre>
42+
* 00 -> '0' | ('0' << 8) -> 0x3030
43+
* 01 -> '1' | ('0' << 8) -> 0x3130
44+
* 02 -> '2' | ('0' << 8) -> 0x3230
45+
*
46+
* ...
47+
*
48+
* 10 -> '0' | ('1' << 8) -> 0x3031
49+
* 11 -> '1' | ('1' << 8) -> 0x3131
50+
* 12 -> '2' | ('1' << 8) -> 0x3231
51+
*
52+
* ...
53+
*
54+
* 97 -> '7' | ('9' << 8) -> 0x3739
55+
* 98 -> '8' | ('9' << 8) -> 0x3839
56+
* 99 -> '9' | ('9' << 8) -> 0x3939
57+
* </pre>
58+
*/
3859
@Stable
39-
private static final short[] DIGITS;
60+
private static final short[] DIGITS = new short[] {
61+
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
62+
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
63+
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
64+
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
65+
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
66+
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
67+
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
68+
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
69+
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
70+
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939
71+
};
4072

4173
/**
4274
* Singleton instance of DecimalDigits.
4375
*/
44-
static final Digits INSTANCE = new DecimalDigits();
45-
46-
static {
47-
short[] digits = new short[10 * 10];
48-
49-
for (int i = 0; i < 10; i++) {
50-
short hi = (short) ((i + '0') << 8);
51-
52-
for (int j = 0; j < 10; j++) {
53-
short lo = (short) (j + '0');
54-
digits[i * 10 + j] = (short) (hi | lo);
55-
}
56-
}
57-
58-
DIGITS = digits;
59-
}
76+
public static final Digits INSTANCE = new DecimalDigits();
6077

6178
/**
6279
* Constructor.
@@ -80,8 +97,8 @@ public int digits(long value, byte[] buffer, int index,
8097
value = q;
8198
int digits = DIGITS[r];
8299

83-
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
84100
putCharMH.invokeExact(buffer, --index, digits >> 8);
101+
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
85102
}
86103

87104
int iq, ivalue = (int)value;
@@ -90,19 +107,19 @@ public int digits(long value, byte[] buffer, int index,
90107
r = (iq * 100) - ivalue;
91108
ivalue = iq;
92109
int digits = DIGITS[r];
93-
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
94110
putCharMH.invokeExact(buffer, --index, digits >> 8);
111+
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
95112
}
96113

97114
if (ivalue < 0) {
98115
ivalue = -ivalue;
99116
}
100117

101118
int digits = DIGITS[ivalue];
102-
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
119+
putCharMH.invokeExact(buffer, --index, digits >> 8);
103120

104121
if (9 < ivalue) {
105-
putCharMH.invokeExact(buffer, --index, digits >> 8);
122+
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
106123
}
107124

108125
if (negative) {
@@ -131,4 +148,13 @@ public int size(long value) {
131148

132149
return 19 + sign;
133150
}
151+
152+
/**
153+
* For values from 0 to 99 return a short encoding a pair of ASCII-encoded digit characters in little-endian
154+
* @param i value to convert
155+
* @return a short encoding a pair of ASCII-encoded digit characters
156+
*/
157+
public static short digitPair(int i) {
158+
return DIGITS[i];
159+
}
134160
}

src/java.base/share/classes/java/util/Digits.java renamed to src/java.base/share/classes/jdk/internal/util/Digits.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* questions.
2424
*/
2525

26-
package java.util;
26+
package jdk.internal.util;
2727

2828
import java.lang.invoke.MethodHandle;
2929

@@ -33,7 +33,7 @@
3333
*
3434
* @since 21
3535
*/
36-
sealed interface Digits permits DecimalDigits, HexDigits, OctalDigits {
36+
public sealed interface Digits permits DecimalDigits, HexDigits, OctalDigits {
3737
/**
3838
* Insert digits for long value in buffer from high index to low index.
3939
*

src/java.base/share/classes/java/util/HexDigits.java renamed to src/java.base/share/classes/jdk/internal/util/HexDigits.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* questions.
2424
*/
2525

26-
package java.util;
26+
package jdk.internal.util;
2727

2828
import java.lang.invoke.MethodHandle;
2929

@@ -34,7 +34,7 @@
3434
*
3535
* @since 21
3636
*/
37-
final class HexDigits implements Digits {
37+
public final class HexDigits implements Digits {
3838
/**
3939
* Each element of the array represents the ascii encoded
4040
* hex relative to its index, for example:<p>
@@ -79,7 +79,7 @@ final class HexDigits implements Digits {
7979
/**
8080
* Singleton instance of HexDigits.
8181
*/
82-
static final Digits INSTANCE = new HexDigits();
82+
public static final Digits INSTANCE = new HexDigits();
8383

8484
static {
8585
short[] digits = new short[16 * 16];
@@ -105,14 +105,14 @@ private HexDigits() {
105105
/**
106106
* Combine two hex shorts into one int based on big endian
107107
*/
108-
static int digit(int b0, int b1) {
108+
public static int digit(int b0, int b1) {
109109
return (DIGITS[b0 & 0xff] << 16) | DIGITS[b1 & 0xff];
110110
}
111111

112112
/**
113113
* Combine four hex shorts into one long based on big endian
114114
*/
115-
static long digit(int b0, int b1, int b2, int b3) {
115+
public static long digit(int b0, int b1, int b2, int b3) {
116116
return (((long) DIGITS[b0 & 0xff]) << 48)
117117
| (((long) DIGITS[b1 & 0xff]) << 32)
118118
| (DIGITS[b2 & 0xff] << 16)

src/java.base/share/classes/java/util/OctalDigits.java renamed to src/java.base/share/classes/jdk/internal/util/OctalDigits.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* questions.
2424
*/
2525

26-
package java.util;
26+
package jdk.internal.util;
2727

2828
import java.lang.invoke.MethodHandle;
2929

@@ -34,23 +34,23 @@
3434
*
3535
* @since 21
3636
*/
37-
final class OctalDigits implements Digits {
37+
public final class OctalDigits implements Digits {
3838
@Stable
3939
private static final short[] DIGITS;
4040

4141
/**
4242
* Singleton instance of OctalDigits.
4343
*/
44-
static final Digits INSTANCE = new OctalDigits();
44+
public static final Digits INSTANCE = new OctalDigits();
4545

4646
static {
4747
short[] digits = new short[8 * 8];
4848

4949
for (int i = 0; i < 8; i++) {
50-
short hi = (short) ((i + '0') << 8);
50+
short lo = (short) (i + '0');
5151

5252
for (int j = 0; j < 8; j++) {
53-
short lo = (short) (j + '0');
53+
short hi = (short) ((j + '0') << 8);
5454
digits[(i << 3) + j] = (short) (hi | lo);
5555
}
5656
}
@@ -70,15 +70,15 @@ public int digits(long value, byte[] buffer, int index,
7070
while ((value & ~0x3F) != 0) {
7171
int digits = DIGITS[(int) (value & 0x3F)];
7272
value >>>= 6;
73-
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
7473
putCharMH.invokeExact(buffer, --index, digits >> 8);
74+
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
7575
}
7676

7777
int digits = DIGITS[(int) (value & 0x3F)];
78-
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
78+
putCharMH.invokeExact(buffer, --index, digits >> 8);
7979

8080
if (7 < value) {
81-
putCharMH.invokeExact(buffer, --index, digits >> 8);
81+
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
8282
}
8383

8484
return index;

0 commit comments

Comments
 (0)