1 /*
2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
68 int fromIndex = origFromIndex;
69 if (fromIndex >= sourceCount) {
70 return (targetCount == 0 ? sourceCount : -1);
71 }
72 if (fromIndex < 0) {
73 fromIndex = 0;
74 }
75 if (targetCount == 0) {
76 // The empty string is in every string.
77 return fromIndex;
78 }
79
80 int totalOffset = sourceOffset + fromIndex;
81 if (sourceCount - fromIndex < targetCount) {
82 // The empty string contains nothing except the empty string.
83 return -1;
84 }
85
86 if (targetCount == 1) {
87 return AMD64ArrayIndexOf.indexOf1Char(source, sourceCount, totalOffset, target[targetOffset]);
88 } else if (targetCount == 2) {
89 return AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(source, sourceCount, totalOffset, target[targetOffset], target[targetOffset + 1]);
90 } else {
91 int haystackLength = sourceCount - (targetCount - 2);
92 while (totalOffset < haystackLength) {
93 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(source, haystackLength, totalOffset, target[targetOffset], target[targetOffset + 1]);
94 if (indexOfResult < 0) {
95 return -1;
96 }
97 totalOffset = indexOfResult;
98 Pointer cmpSourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
99 Pointer targetPointer = Word.objectToTrackedPointer(target).add(charArrayBaseOffset(INJECTED)).add(targetOffset * charArrayIndexScale(INJECTED));
100 if (ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Char)) {
101 return totalOffset;
102 }
103 totalOffset++;
104 }
105 return -1;
106 }
107 }
108
109 // Only exists in JDK <= 8
110 @MethodSubstitution(isStatic = false, optional = true)
111 public static int indexOf(String source, int ch, int origFromIndex) {
112 int fromIndex = origFromIndex;
113 final int sourceCount = source.length();
114 if (fromIndex >= sourceCount) {
115 // Note: fromIndex might be near -1>>>1.
116 return -1;
117 }
118 if (fromIndex < 0) {
119 fromIndex = 0;
120 }
121
|
1 /*
2 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
68 int fromIndex = origFromIndex;
69 if (fromIndex >= sourceCount) {
70 return (targetCount == 0 ? sourceCount : -1);
71 }
72 if (fromIndex < 0) {
73 fromIndex = 0;
74 }
75 if (targetCount == 0) {
76 // The empty string is in every string.
77 return fromIndex;
78 }
79
80 int totalOffset = sourceOffset + fromIndex;
81 if (sourceCount - fromIndex < targetCount) {
82 // The empty string contains nothing except the empty string.
83 return -1;
84 }
85
86 if (targetCount == 1) {
87 return AMD64ArrayIndexOf.indexOf1Char(source, sourceCount, totalOffset, target[targetOffset]);
88 } else {
89 int haystackLength = sourceCount - (targetCount - 2);
90 while (totalOffset < haystackLength) {
91 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveChars(source, haystackLength, totalOffset, target[targetOffset], target[targetOffset + 1]);
92 if (indexOfResult < 0) {
93 return -1;
94 }
95 totalOffset = indexOfResult;
96 if (targetCount == 2) {
97 return totalOffset;
98 } else {
99 Pointer cmpSourcePointer = Word.objectToTrackedPointer(source).add(charArrayBaseOffset(INJECTED)).add(totalOffset * charArrayIndexScale(INJECTED));
100 Pointer targetPointer = Word.objectToTrackedPointer(target).add(charArrayBaseOffset(INJECTED)).add(targetOffset * charArrayIndexScale(INJECTED));
101 if (ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Char)) {
102 return totalOffset;
103 }
104 }
105 totalOffset++;
106 }
107 return -1;
108 }
109 }
110
111 // Only exists in JDK <= 8
112 @MethodSubstitution(isStatic = false, optional = true)
113 public static int indexOf(String source, int ch, int origFromIndex) {
114 int fromIndex = origFromIndex;
115 final int sourceCount = source.length();
116 if (fromIndex >= sourceCount) {
117 // Note: fromIndex might be near -1>>>1.
118 return -1;
119 }
120 if (fromIndex < 0) {
121 fromIndex = 0;
122 }
123
|