mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8325169: Reduce String::indexOf overheads
Reviewed-by: rriggs, rgiulietti, mli
This commit is contained in:
parent
89e6a02e3b
commit
19e92201b4
4 changed files with 23 additions and 19 deletions
|
@ -2444,7 +2444,8 @@ public final class String
|
|||
* {@code -1} if the character does not occur.
|
||||
*/
|
||||
public int indexOf(int ch) {
|
||||
return indexOf(ch, 0);
|
||||
return isLatin1() ? StringLatin1.indexOf(value, ch, 0, value.length)
|
||||
: StringUTF16.indexOf(value, ch, 0, value.length >> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2500,8 +2501,9 @@ public final class String
|
|||
* {@code fromIndex} were larger than the string length, or were negative.
|
||||
*/
|
||||
public int indexOf(int ch, int fromIndex) {
|
||||
return isLatin1() ? StringLatin1.indexOf(value, ch, fromIndex, length())
|
||||
: StringUTF16.indexOf(value, ch, fromIndex, length());
|
||||
fromIndex = Math.max(fromIndex, 0);
|
||||
return isLatin1() ? StringLatin1.indexOf(value, ch, Math.min(fromIndex, value.length), value.length)
|
||||
: StringUTF16.indexOf(value, ch, Math.min(fromIndex, value.length >> 1), value.length >> 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -310,15 +310,11 @@ final class StringLatin1 {
|
|||
};
|
||||
}
|
||||
|
||||
// Caller must ensure that from- and toIndex are within bounds
|
||||
public static int indexOf(byte[] value, int ch, int fromIndex, int toIndex) {
|
||||
if (!canEncode(ch)) {
|
||||
return -1;
|
||||
}
|
||||
fromIndex = Math.max(fromIndex, 0);
|
||||
toIndex = Math.min(toIndex, value.length);
|
||||
if (fromIndex >= toIndex) {
|
||||
return -1;
|
||||
}
|
||||
return indexOfChar(value, ch, fromIndex, toIndex);
|
||||
}
|
||||
|
||||
|
|
|
@ -604,12 +604,8 @@ final class StringUTF16 {
|
|||
};
|
||||
}
|
||||
|
||||
// Caller must ensure that from- and toIndex are within bounds
|
||||
public static int indexOf(byte[] value, int ch, int fromIndex, int toIndex) {
|
||||
fromIndex = Math.max(fromIndex, 0);
|
||||
toIndex = Math.min(toIndex, value.length >> 1);
|
||||
if (fromIndex >= toIndex) {
|
||||
return -1;
|
||||
}
|
||||
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
|
||||
// handle most cases here (ch is a BMP code point or a
|
||||
// negative value (invalid code point))
|
||||
|
@ -716,11 +712,6 @@ final class StringUTF16 {
|
|||
|
||||
@IntrinsicCandidate
|
||||
private static int indexOfChar(byte[] value, int ch, int fromIndex, int max) {
|
||||
checkBoundsBeginEnd(fromIndex, max, value);
|
||||
return indexOfCharUnsafe(value, ch, fromIndex, max);
|
||||
}
|
||||
|
||||
private static int indexOfCharUnsafe(byte[] value, int ch, int fromIndex, int max) {
|
||||
for (int i = fromIndex; i < max; i++) {
|
||||
if (getChar(value, i) == ch) {
|
||||
return i;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue