提交 6f011303 编写于 作者: H henryjen

8015522: CharSequence.codePoints can be faster

Reviewed-by: martin, psandoz, alanb
Contributed-by: henry.jen@oracle.com
上级 802f99a8
...@@ -179,10 +179,25 @@ public interface CharSequence { ...@@ -179,10 +179,25 @@ public interface CharSequence {
@Override @Override
public void forEachRemaining(IntConsumer block) { public void forEachRemaining(IntConsumer block) {
while (cur < length()) { final int length = length();
int cp = Character.codePointAt(CharSequence.this, cur); int i = cur;
cur += Character.charCount(cp); try {
block.accept(cp); while (i < length) {
char c1 = charAt(i++);
if (!Character.isHighSurrogate(c1) || i >= length) {
block.accept(c1);
} else {
char c2 = charAt(i);
if (Character.isLowSurrogate(c2)) {
i++;
block.accept(Character.toCodePoint(c1, c2));
} else {
block.accept(c1);
}
}
}
} finally {
cur = i;
} }
} }
...@@ -191,12 +206,20 @@ public interface CharSequence { ...@@ -191,12 +206,20 @@ public interface CharSequence {
} }
public int nextInt() { public int nextInt() {
if (!hasNext()) { final int length = length();
if (cur >= length) {
throw new NoSuchElementException(); throw new NoSuchElementException();
} }
int cp = Character.codePointAt(CharSequence.this, cur); char c1 = charAt(cur++);
cur += Character.charCount(cp); if (Character.isHighSurrogate(c1) && cur < length) {
return cp; char c2 = charAt(cur);
if (Character.isLowSurrogate(c2)) {
cur++;
return Character.toCodePoint(c1, c2);
}
}
return c1;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册