8315968: Move java.util.Digits to jdk.internal.util and refactor to reduce duplication

Reviewed-by: rriggs, liach, redestad
This commit is contained in:
shaojin.wensj 2023-09-12 16:35:01 +00:00 committed by Claes Redestad
parent d75d9774c8
commit e0845163aa
8 changed files with 76 additions and 79 deletions

View file

@ -33,6 +33,7 @@ import java.util.function.IntConsumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import jdk.internal.util.DecimalDigits;
import jdk.internal.util.ByteArrayLittleEndian;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
@ -43,41 +44,6 @@ import static java.lang.String.checkIndex;
import static java.lang.String.checkOffset;
final class StringLatin1 {
/**
* Each element of the array represents the packaging of two ascii characters based on little endian:<p>
* <pre>
* 00 -> '0' | ('0' << 8) -> 0x3030
* 01 -> '1' | ('0' << 8) -> 0x3130
* 02 -> '2' | ('0' << 8) -> 0x3230
*
* ...
*
* 10 -> '0' | ('1' << 8) -> 0x3031
* 11 -> '1' | ('1' << 8) -> 0x3131
* 12 -> '2' | ('1' << 8) -> 0x3231
*
* ...
*
* 97 -> '7' | ('9' << 8) -> 0x3739
* 98 -> '8' | ('9' << 8) -> 0x3839
* 99 -> '9' | ('9' << 8) -> 0x3939
* </pre>
*/
@Stable
static final short[] PACKED_DIGITS = new short[] {
0x3030, 0x3130, 0x3230, 0x3330, 0x3430, 0x3530, 0x3630, 0x3730, 0x3830, 0x3930,
0x3031, 0x3131, 0x3231, 0x3331, 0x3431, 0x3531, 0x3631, 0x3731, 0x3831, 0x3931,
0x3032, 0x3132, 0x3232, 0x3332, 0x3432, 0x3532, 0x3632, 0x3732, 0x3832, 0x3932,
0x3033, 0x3133, 0x3233, 0x3333, 0x3433, 0x3533, 0x3633, 0x3733, 0x3833, 0x3933,
0x3034, 0x3134, 0x3234, 0x3334, 0x3434, 0x3534, 0x3634, 0x3734, 0x3834, 0x3934,
0x3035, 0x3135, 0x3235, 0x3335, 0x3435, 0x3535, 0x3635, 0x3735, 0x3835, 0x3935,
0x3036, 0x3136, 0x3236, 0x3336, 0x3436, 0x3536, 0x3636, 0x3736, 0x3836, 0x3936,
0x3037, 0x3137, 0x3237, 0x3337, 0x3437, 0x3537, 0x3637, 0x3737, 0x3837, 0x3937,
0x3038, 0x3138, 0x3238, 0x3338, 0x3438, 0x3538, 0x3638, 0x3738, 0x3838, 0x3938,
0x3039, 0x3139, 0x3239, 0x3339, 0x3439, 0x3539, 0x3639, 0x3739, 0x3839, 0x3939
};
public static char charAt(byte[] value, int index) {
checkIndex(index, value.length);
return (char)(value[index] & 0xff);
@ -148,13 +114,13 @@ final class StringLatin1 {
r = (q * 100) - i;
i = q;
charPos -= 2;
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[r]);
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair(r));
}
// We know there are at most two digits left at this point.
if (i < -9) {
charPos -= 2;
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[-i]);
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair(-i));
} else {
buf[--charPos] = (byte)('0' - i);
}
@ -196,7 +162,7 @@ final class StringLatin1 {
while (i <= Integer.MIN_VALUE) {
q = i / 100;
charPos -= 2;
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[(int)((q * 100) - i)]);
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair((int)((q * 100) - i)));
i = q;
}
@ -206,14 +172,14 @@ final class StringLatin1 {
while (i2 <= -100) {
q2 = i2 / 100;
charPos -= 2;
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[(q2 * 100) - i2]);
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair((q2 * 100) - i2));
i2 = q2;
}
// We know there are at most two digits left at this point.
if (i2 < -9) {
charPos -= 2;
ByteArrayLittleEndian.setShort(buf, charPos, PACKED_DIGITS[-i2]);
ByteArrayLittleEndian.setShort(buf, charPos, DecimalDigits.digitPair(-i2));
} else {
buf[--charPos] = (byte)('0' - i2);
}

View file

@ -33,6 +33,7 @@ import java.util.function.IntConsumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import jdk.internal.util.DecimalDigits;
import jdk.internal.vm.annotation.DontInline;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.annotation.IntrinsicCandidate;
@ -1612,7 +1613,7 @@ final class StringUTF16 {
}
private static void putPair(byte[] buf, int charPos, int v) {
int packed = (int) StringLatin1.PACKED_DIGITS[v];
int packed = (int) DecimalDigits.digitPair(v);
putChar(buf, charPos, packed & 0xFF);
putChar(buf, charPos + 1, packed >> 8);
}

View file

@ -1,134 +0,0 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util;
import java.lang.invoke.MethodHandle;
import jdk.internal.vm.annotation.Stable;
/**
* Digits class for decimal digits.
*
* @since 21
*/
final class DecimalDigits implements Digits {
@Stable
private static final short[] DIGITS;
/**
* Singleton instance of DecimalDigits.
*/
static final Digits INSTANCE = new DecimalDigits();
static {
short[] digits = new short[10 * 10];
for (int i = 0; i < 10; i++) {
short hi = (short) ((i + '0') << 8);
for (int j = 0; j < 10; j++) {
short lo = (short) (j + '0');
digits[i * 10 + j] = (short) (hi | lo);
}
}
DIGITS = digits;
}
/**
* Constructor.
*/
private DecimalDigits() {
}
@Override
public int digits(long value, byte[] buffer, int index,
MethodHandle putCharMH) throws Throwable {
boolean negative = value < 0;
if (!negative) {
value = -value;
}
long q;
int r;
while (value <= Integer.MIN_VALUE) {
q = value / 100;
r = (int)((q * 100) - value);
value = q;
int digits = DIGITS[r];
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
int iq, ivalue = (int)value;
while (ivalue <= -100) {
iq = ivalue / 100;
r = (iq * 100) - ivalue;
ivalue = iq;
int digits = DIGITS[r];
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
if (ivalue < 0) {
ivalue = -ivalue;
}
int digits = DIGITS[ivalue];
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
if (9 < ivalue) {
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
if (negative) {
putCharMH.invokeExact(buffer, --index, (int)'-');
}
return index;
}
@Override
public int size(long value) {
boolean negative = value < 0;
int sign = negative ? 1 : 0;
if (!negative) {
value = -value;
}
long precision = -10;
for (int i = 1; i < 19; i++) {
if (value > precision)
return i + sign;
precision = 10 * precision;
}
return 19 + sign;
}
}

View file

@ -1,61 +0,0 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util;
import java.lang.invoke.MethodHandle;
/**
* Digits provides a fast methodology for converting integers and longs to
* ASCII strings.
*
* @since 21
*/
sealed interface Digits permits DecimalDigits, HexDigits, OctalDigits {
/**
* Insert digits for long value in buffer from high index to low index.
*
* @param value value to convert
* @param buffer byte buffer to copy into
* @param index insert point + 1
* @param putCharMH method to put character
*
* @return the last index used
*
* @throws Throwable if putCharMH fails (unusual).
*/
int digits(long value, byte[] buffer, int index,
MethodHandle putCharMH) throws Throwable;
/**
* Calculate the number of digits required to represent the long.
*
* @param value value to convert
*
* @return number of digits
*/
int size(long value);
}

View file

@ -36,6 +36,9 @@ import java.util.Formatter.FormatSpecifier;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.FormatConcatItem;
import jdk.internal.util.DecimalDigits;
import jdk.internal.util.HexDigits;
import jdk.internal.util.OctalDigits;
import static java.lang.invoke.MethodType.methodType;

View file

@ -1,147 +0,0 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util;
import java.lang.invoke.MethodHandle;
import jdk.internal.vm.annotation.Stable;
/**
* Digits class for hexadecimal digits.
*
* @since 21
*/
final class HexDigits implements Digits {
/**
* Each element of the array represents the ascii encoded
* hex relative to its index, for example:<p>
* <pre>
* 0 -> '00' -> ('0' << 8) | '0' -> 12336
* 1 -> '01' -> ('0' << 8) | '1' -> 12337
* 2 -> '02' -> ('0' << 8) | '2' -> 12338
*
* ...
*
* 10 -> '0a' -> ('0' << 8) | 'a' -> 12385
* 11 -> '0b' -> ('0' << 8) | 'b' -> 12386
* 12 -> '0c' -> ('0' << 8) | 'b' -> 12387
*
* ...
*
* 26 -> '1a' -> ('1' << 8) | 'a' -> 12641
* 27 -> '1b' -> ('1' << 8) | 'b' -> 12642
* 28 -> '1c' -> ('1' << 8) | 'c' -> 12643
*
* ...
*
* 253 -> 'fd' -> ('f' << 8) | 'd' -> 26212
* 254 -> 'fe' -> ('f' << 8) | 'e' -> 26213
* 255 -> 'ff' -> ('f' << 8) | 'f' -> 26214
* </pre>
* <p>use like this:
* <pre>
* int v = 254;
*
* char[] chars = new char[2];
*
* short i = DIGITS[v]; // 26213
*
* chars[0] = (char) (byte) (i >> 8); // 'f'
* chars[1] = (char) (byte) i; // 'e'
* </pre>
*/
@Stable
private static final short[] DIGITS;
/**
* Singleton instance of HexDigits.
*/
static final Digits INSTANCE = new HexDigits();
static {
short[] digits = new short[16 * 16];
for (int i = 0; i < 16; i++) {
short hi = (short) ((i < 10 ? i + '0' : i - 10 + 'a') << 8);
for (int j = 0; j < 16; j++) {
short lo = (short) (j < 10 ? j + '0' : j - 10 + 'a');
digits[(i << 4) + j] = (short) (hi | lo);
}
}
DIGITS = digits;
}
/**
* Constructor.
*/
private HexDigits() {
}
/**
* Combine two hex shorts into one int based on big endian
*/
static int digit(int b0, int b1) {
return (DIGITS[b0 & 0xff] << 16) | DIGITS[b1 & 0xff];
}
/**
* Combine four hex shorts into one long based on big endian
*/
static long digit(int b0, int b1, int b2, int b3) {
return (((long) DIGITS[b0 & 0xff]) << 48)
| (((long) DIGITS[b1 & 0xff]) << 32)
| (DIGITS[b2 & 0xff] << 16)
| DIGITS[b3 & 0xff];
}
@Override
public int digits(long value, byte[] buffer, int index,
MethodHandle putCharMH) throws Throwable {
while ((value & ~0xFF) != 0) {
int digits = DIGITS[(int) (value & 0xFF)];
value >>>= 8;
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
int digits = DIGITS[(int) (value & 0xFF)];
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
if (0xF < value) {
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
return index;
}
@Override
public int size(long value) {
return value == 0 ? 1 :
67 - Long.numberOfLeadingZeros(value) >> 2;
}
}

View file

@ -1,91 +0,0 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util;
import java.lang.invoke.MethodHandle;
import jdk.internal.vm.annotation.Stable;
/**
* Digits class for octal digits.
*
* @since 21
*/
final class OctalDigits implements Digits {
@Stable
private static final short[] DIGITS;
/**
* Singleton instance of OctalDigits.
*/
static final Digits INSTANCE = new OctalDigits();
static {
short[] digits = new short[8 * 8];
for (int i = 0; i < 8; i++) {
short hi = (short) ((i + '0') << 8);
for (int j = 0; j < 8; j++) {
short lo = (short) (j + '0');
digits[(i << 3) + j] = (short) (hi | lo);
}
}
DIGITS = digits;
}
/**
* Constructor.
*/
private OctalDigits() {
}
@Override
public int digits(long value, byte[] buffer, int index,
MethodHandle putCharMH) throws Throwable {
while ((value & ~0x3F) != 0) {
int digits = DIGITS[(int) (value & 0x3F)];
value >>>= 6;
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
int digits = DIGITS[(int) (value & 0x3F)];
putCharMH.invokeExact(buffer, --index, digits & 0xFF);
if (7 < value) {
putCharMH.invokeExact(buffer, --index, digits >> 8);
}
return index;
}
@Override
public int size(long value) {
return (66 - Long.numberOfLeadingZeros(value)) / 3;
}
}

View file

@ -32,6 +32,7 @@ import java.security.*;
import jdk.internal.access.JavaLangAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.util.ByteArray;
import jdk.internal.util.HexDigits;
/**
* A class that represents an immutable universally unique identifier (UUID).