1 /* |
1 /* |
2 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
110 fromIndex = 0; |
110 fromIndex = 0; |
111 } else if (fromIndex >= length) { |
111 } else if (fromIndex >= length) { |
112 // Note: fromIndex might be near -1>>>1. |
112 // Note: fromIndex might be near -1>>>1. |
113 return -1; |
113 return -1; |
114 } |
114 } |
115 Pointer sourcePointer = byteOffsetPointer(value, fromIndex); |
115 return AMD64ArrayIndexOf.indexOf1Byte(value, length, fromIndex, (byte) ch); |
116 int result = AMD64ArrayIndexOf.indexOf1Byte(sourcePointer, length - fromIndex, (byte) ch); |
|
117 if (result != -1) { |
|
118 return result + fromIndex; |
|
119 } |
|
120 return result; |
|
121 } |
116 } |
122 |
117 |
123 @MethodSubstitution |
118 @MethodSubstitution |
124 public static int indexOf(byte[] source, int sourceCount, byte[] target, int targetCount, int origFromIndex) { |
119 public static int indexOf(byte[] source, int sourceCount, byte[] target, int targetCount, int origFromIndex) { |
125 int fromIndex = origFromIndex; |
120 int fromIndex = origFromIndex; |
135 } |
130 } |
136 if (sourceCount - fromIndex < targetCount) { |
131 if (sourceCount - fromIndex < targetCount) { |
137 // The empty string contains nothing except the empty string. |
132 // The empty string contains nothing except the empty string. |
138 return -1; |
133 return -1; |
139 } |
134 } |
140 int totalOffset = fromIndex; |
|
141 if (targetCount == 1) { |
135 if (targetCount == 1) { |
142 Pointer sourcePointer = byteOffsetPointer(source, totalOffset); |
136 return AMD64ArrayIndexOf.indexOf1Byte(source, sourceCount, fromIndex, target[0]); |
143 int indexOfResult = AMD64ArrayIndexOf.indexOf1Byte(sourcePointer, sourceCount - fromIndex, target[0]); |
|
144 if (indexOfResult >= 0) { |
|
145 return indexOfResult + totalOffset; |
|
146 } |
|
147 return indexOfResult; |
|
148 } else if (targetCount == 2) { |
|
149 Pointer sourcePointer = byteOffsetPointer(source, totalOffset); |
|
150 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(sourcePointer, sourceCount - fromIndex, target[0], target[1]); |
|
151 if (indexOfResult >= 0) { |
|
152 return indexOfResult + totalOffset; |
|
153 } |
|
154 return indexOfResult; |
|
155 } else { |
137 } else { |
156 int haystackLength = sourceCount - (fromIndex + (targetCount - 2)); |
138 int haystackLength = sourceCount - (targetCount - 2); |
157 while (haystackLength > 0) { |
139 int offset = fromIndex; |
158 Pointer sourcePointer = byteOffsetPointer(source, totalOffset); |
140 while (offset < haystackLength) { |
159 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(sourcePointer, haystackLength, target[0], target[1]); |
141 int indexOfResult = AMD64ArrayIndexOf.indexOfTwoConsecutiveBytes(source, haystackLength, offset, target[0], target[1]); |
160 if (indexOfResult < 0) { |
142 if (indexOfResult < 0) { |
161 return -1; |
143 return -1; |
162 } |
144 } |
163 totalOffset += indexOfResult; |
145 offset = indexOfResult; |
164 haystackLength -= (indexOfResult + 1); |
146 Pointer cmpSourcePointer = byteOffsetPointer(source, offset); |
165 Pointer cmpSourcePointer = byteOffsetPointer(source, totalOffset); |
|
166 Pointer targetPointer = pointer(target); |
147 Pointer targetPointer = pointer(target); |
167 if (ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Byte)) { |
148 if (targetCount == 2 || ArrayRegionEqualsNode.regionEquals(cmpSourcePointer, targetPointer, targetCount, JavaKind.Byte)) { |
168 return totalOffset; |
149 return offset; |
169 } |
150 } |
170 totalOffset++; |
151 offset++; |
171 } |
152 } |
172 return -1; |
153 return -1; |
173 } |
154 } |
174 } |
155 } |
175 |
156 |