8148936: Adapt UUID.toString() to Compact Strings
Reviewed-by: igerasim, redestad
This commit is contained in:
parent
f8fa6e063d
commit
24816037ab
@ -401,18 +401,6 @@ public final class Long extends Number implements Comparable<Long> {
|
||||
* @param offset the offset in the destination buffer to start at
|
||||
* @param len the number of characters to write
|
||||
*/
|
||||
static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
|
||||
// assert shift > 0 && shift <=5 : "Illegal shift value";
|
||||
// assert offset >= 0 && offset < buf.length : "illegal offset";
|
||||
// assert len > 0 && (offset + len) <= buf.length : "illegal length";
|
||||
int charPos = offset + len;
|
||||
int radix = 1 << shift;
|
||||
int mask = radix - 1;
|
||||
do {
|
||||
buf[--charPos] = Integer.digits[((int) val) & mask];
|
||||
val >>>= shift;
|
||||
} while (charPos > offset);
|
||||
}
|
||||
|
||||
/** byte[]/LATIN1 version */
|
||||
static void formatUnsignedLong0(long val, int shift, byte[] buf, int offset, int len) {
|
||||
@ -436,6 +424,39 @@ public final class Long extends Number implements Comparable<Long> {
|
||||
} while (charPos > offset);
|
||||
}
|
||||
|
||||
static String fastUUID(long lsb, long msb) {
|
||||
if (COMPACT_STRINGS) {
|
||||
byte[] buf = new byte[36];
|
||||
formatUnsignedLong0(lsb, 4, buf, 24, 12);
|
||||
formatUnsignedLong0(lsb >>> 48, 4, buf, 19, 4);
|
||||
formatUnsignedLong0(msb, 4, buf, 14, 4);
|
||||
formatUnsignedLong0(msb >>> 16, 4, buf, 9, 4);
|
||||
formatUnsignedLong0(msb >>> 32, 4, buf, 0, 8);
|
||||
|
||||
buf[23] = '-';
|
||||
buf[18] = '-';
|
||||
buf[13] = '-';
|
||||
buf[8] = '-';
|
||||
|
||||
return new String(buf, LATIN1);
|
||||
} else {
|
||||
byte[] buf = new byte[72];
|
||||
|
||||
formatUnsignedLong0UTF16(lsb, 4, buf, 24, 12);
|
||||
formatUnsignedLong0UTF16(lsb >>> 48, 4, buf, 19, 4);
|
||||
formatUnsignedLong0UTF16(msb, 4, buf, 14, 4);
|
||||
formatUnsignedLong0UTF16(msb >>> 16, 4, buf, 9, 4);
|
||||
formatUnsignedLong0UTF16(msb >>> 32, 4, buf, 0, 8);
|
||||
|
||||
StringUTF16.putChar(buf, 23, '-');
|
||||
StringUTF16.putChar(buf, 18, '-');
|
||||
StringUTF16.putChar(buf, 13, '-');
|
||||
StringUTF16.putChar(buf, 8, '-');
|
||||
|
||||
return new String(buf, UTF16);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@code String} object representing the specified
|
||||
* {@code long}. The argument is converted to signed decimal
|
||||
|
@ -1911,11 +1911,8 @@ public final class System {
|
||||
public void invokeFinalize(Object o) throws Throwable {
|
||||
o.finalize();
|
||||
}
|
||||
public void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) {
|
||||
Long.formatUnsignedLong(val, shift, buf, offset, len);
|
||||
}
|
||||
public void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
|
||||
Integer.formatUnsignedInt(val, shift, buf, offset, len);
|
||||
public String fastUUID(long lsb, long msb) {
|
||||
return Long.fastUUID(lsb, msb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -392,17 +392,7 @@ public final class UUID implements java.io.Serializable, Comparable<UUID> {
|
||||
* @return A string representation of this {@code UUID}
|
||||
*/
|
||||
public String toString() {
|
||||
char[] chars = new char[36];
|
||||
jla.formatUnsignedLong(mostSigBits >> 32, 4, chars, 0, 8);
|
||||
chars[8] = '-';
|
||||
jla.formatUnsignedLong(mostSigBits >> 16, 4, chars, 9, 4);
|
||||
chars[13] = '-';
|
||||
jla.formatUnsignedLong(mostSigBits, 4, chars, 14, 4);
|
||||
chars[18] = '-';
|
||||
jla.formatUnsignedLong(leastSigBits >> 48, 4, chars, 19, 4);
|
||||
chars[23] = '-';
|
||||
jla.formatUnsignedLong(leastSigBits, 4, chars, 24, 12);
|
||||
return jla.newStringUnsafe(chars);
|
||||
return jla.fastUUID(leastSigBits, mostSigBits);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -124,12 +124,7 @@ public interface JavaLangAccess {
|
||||
void invokeFinalize(Object o) throws Throwable;
|
||||
|
||||
/**
|
||||
* Invokes Long.formatUnsignedLong(long val, int shift, char[] buf, int offset, int len)
|
||||
* Invokes Long.fastUUID
|
||||
*/
|
||||
void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len);
|
||||
|
||||
/**
|
||||
* Invokes Integer.formatUnsignedInt(long val, int shift, char[] buf, int offset, int len)
|
||||
*/
|
||||
void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len);
|
||||
String fastUUID(long lsb, long msb);
|
||||
}
|
||||
|
@ -22,9 +22,11 @@
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4173528 5068772
|
||||
* @bug 4173528 5068772 8148936
|
||||
* @summary Unit tests for java.util.UUID
|
||||
* @key randomness
|
||||
* @run main/othervm -XX:+CompactStrings UUIDTest
|
||||
* @run main/othervm -XX:-CompactStrings UUIDTest
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
Loading…
x
Reference in New Issue
Block a user