提交 8685a9ca 编写于 作者: M martin

6934268: Better implementation of Character.isValidCodePoint

Summary: Use the cleverest possible bit-twiddling micro-optimizations
Reviewed-by: sherman
Contributed-by: NUlf Zibis <ulf.zibis@gmx.de>
上级 4fa56e2c
...@@ -115,6 +115,8 @@ import java.util.Locale; ...@@ -115,6 +115,8 @@ import java.util.Locale;
* @author Lee Boynton * @author Lee Boynton
* @author Guy Steele * @author Guy Steele
* @author Akira Tanaka * @author Akira Tanaka
* @author Martin Buchholz
* @author Ulf Zibis
* @since 1.0 * @since 1.0
*/ */
public final public final
...@@ -3914,7 +3916,10 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -3914,7 +3916,10 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
* @since 1.5 * @since 1.5
*/ */
public static boolean isValidCodePoint(int codePoint) { public static boolean isValidCodePoint(int codePoint) {
return codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT; // Optimized form of:
// codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT
int plane = codePoint >>> 16;
return plane < ((MAX_CODE_POINT + 1) >>> 16);
} }
/** /**
...@@ -3930,7 +3935,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -3930,7 +3935,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
*/ */
public static boolean isSupplementaryCodePoint(int codePoint) { public static boolean isSupplementaryCodePoint(int codePoint) {
return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT
&& codePoint <= MAX_CODE_POINT; && codePoint < MAX_CODE_POINT + 1;
} }
/** /**
...@@ -3954,7 +3959,8 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -3954,7 +3959,8 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
* @since 1.5 * @since 1.5
*/ */
public static boolean isHighSurrogate(char ch) { public static boolean isHighSurrogate(char ch) {
return ch >= MIN_HIGH_SURROGATE && ch <= MAX_HIGH_SURROGATE; // Help VM constant-fold; MAX_HIGH_SURROGATE + 1 == MIN_LOW_SURROGATE
return ch >= MIN_HIGH_SURROGATE && ch < (MAX_HIGH_SURROGATE + 1);
} }
/** /**
...@@ -3977,7 +3983,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -3977,7 +3983,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
* @since 1.5 * @since 1.5
*/ */
public static boolean isLowSurrogate(char ch) { public static boolean isLowSurrogate(char ch) {
return ch >= MIN_LOW_SURROGATE && ch <= MAX_LOW_SURROGATE; return ch >= MIN_LOW_SURROGATE && ch < (MAX_LOW_SURROGATE + 1);
} }
/** /**
...@@ -4001,7 +4007,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4001,7 +4007,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
* @since 1.7 * @since 1.7
*/ */
public static boolean isSurrogate(char ch) { public static boolean isSurrogate(char ch) {
return ch >= MIN_SURROGATE && ch <= MAX_SURROGATE; return ch >= MIN_SURROGATE && ch < (MAX_SURROGATE + 1);
} }
/** /**
...@@ -4160,6 +4166,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4160,6 +4166,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
return codePointAtImpl(a, index, limit); return codePointAtImpl(a, index, limit);
} }
// throws ArrayIndexOutofBoundsException if index out of bounds
static int codePointAtImpl(char[] a, int index, int limit) { static int codePointAtImpl(char[] a, int index, int limit) {
char c1 = a[index++]; char c1 = a[index++];
if (isHighSurrogate(c1)) { if (isHighSurrogate(c1)) {
...@@ -4266,6 +4273,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4266,6 +4273,7 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
return codePointBeforeImpl(a, index, start); return codePointBeforeImpl(a, index, start);
} }
// throws ArrayIndexOutofBoundsException if index-1 out of bounds
static int codePointBeforeImpl(char[] a, int index, int start) { static int codePointBeforeImpl(char[] a, int index, int start) {
char c2 = a[--index]; char c2 = a[--index];
if (isLowSurrogate(c2)) { if (isLowSurrogate(c2)) {
...@@ -4385,15 +4393,14 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4385,15 +4393,14 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) { if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
int n = 0; int n = endIndex - beginIndex;
for (int i = beginIndex; i < endIndex; ) { for (int i = beginIndex; i < endIndex; ) {
n++; if (isHighSurrogate(seq.charAt(i++)) && i < endIndex &&
if (isHighSurrogate(seq.charAt(i++))) { isLowSurrogate(seq.charAt(i))) {
if (i < endIndex && isLowSurrogate(seq.charAt(i))) { n--;
i++; i++;
} }
} }
}
return n; return n;
} }
...@@ -4425,15 +4432,14 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4425,15 +4432,14 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
static int codePointCountImpl(char[] a, int offset, int count) { static int codePointCountImpl(char[] a, int offset, int count) {
int endIndex = offset + count; int endIndex = offset + count;
int n = 0; int n = count;
for (int i = offset; i < endIndex; ) { for (int i = offset; i < endIndex; ) {
n++; if (isHighSurrogate(a[i++]) && i < endIndex &&
if (isHighSurrogate(a[i++])) { isLowSurrogate(a[i])) {
if (i < endIndex && isLowSurrogate(a[i])) { n--;
i++; i++;
} }
} }
}
return n; return n;
} }
...@@ -4470,24 +4476,22 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4470,24 +4476,22 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
if (codePointOffset >= 0) { if (codePointOffset >= 0) {
int i; int i;
for (i = 0; x < length && i < codePointOffset; i++) { for (i = 0; x < length && i < codePointOffset; i++) {
if (isHighSurrogate(seq.charAt(x++))) { if (isHighSurrogate(seq.charAt(x++)) && x < length &&
if (x < length && isLowSurrogate(seq.charAt(x))) { isLowSurrogate(seq.charAt(x))) {
x++; x++;
} }
} }
}
if (i < codePointOffset) { if (i < codePointOffset) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
} else { } else {
int i; int i;
for (i = codePointOffset; x > 0 && i < 0; i++) { for (i = codePointOffset; x > 0 && i < 0; i++) {
if (isLowSurrogate(seq.charAt(--x))) { if (isLowSurrogate(seq.charAt(--x)) && x > 0 &&
if (x > 0 && isHighSurrogate(seq.charAt(x-1))) { isHighSurrogate(seq.charAt(x-1))) {
x--; x--;
} }
} }
}
if (i < 0) { if (i < 0) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
...@@ -4544,24 +4548,22 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -4544,24 +4548,22 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
int limit = start + count; int limit = start + count;
int i; int i;
for (i = 0; x < limit && i < codePointOffset; i++) { for (i = 0; x < limit && i < codePointOffset; i++) {
if (isHighSurrogate(a[x++])) { if (isHighSurrogate(a[x++]) && x < limit &&
if (x < limit && isLowSurrogate(a[x])) { isLowSurrogate(a[x])) {
x++; x++;
} }
} }
}
if (i < codePointOffset) { if (i < codePointOffset) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
} else { } else {
int i; int i;
for (i = codePointOffset; x > start && i < 0; i++) { for (i = codePointOffset; x > start && i < 0; i++) {
if (isLowSurrogate(a[--x])) { if (isLowSurrogate(a[--x]) && x > start &&
if (x > start && isHighSurrogate(a[x-1])) { isHighSurrogate(a[x-1])) {
x--; x--;
} }
} }
}
if (i < 0) { if (i < 0) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
} }
...@@ -5934,8 +5936,11 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara ...@@ -5934,8 +5936,11 @@ class Character extends Object implements java.io.Serializable, Comparable<Chara
* @since 1.5 * @since 1.5
*/ */
public static boolean isISOControl(int codePoint) { public static boolean isISOControl(int codePoint) {
return (codePoint >= 0x0000 && codePoint <= 0x001F) || // Optimized form of:
(codePoint >= 0x007F && codePoint <= 0x009F); // (codePoint >= 0x00 && codePoint <= 0x1F) ||
// (codePoint >= 0x7F && codePoint <= 0x9F);
return codePoint <= 0x9F &&
(codePoint >= 0x7F || (codePoint >>> 5 == 0));
} }
/** /**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册