MCPcopy Create free account
hub / github.com/google/re2j / indexOfFallback

Method indexOfFallback

java/com/google/re2j/MachineInput.java:232–265  ·  view source on GitHub ↗
(CharSequence hayStack, String needle, int fromIndex)

Source from the content-addressed store, hash-verified

230
231 // Modified version of {@link String#indexOf(String) that allows a CharSequence.
232 private int indexOfFallback(CharSequence hayStack, String needle, int fromIndex) {
233 if (fromIndex >= hayStack.length()) {
234 return needle.isEmpty() ? 0 : -1;
235 }
236 if (fromIndex < 0) {
237 fromIndex = 0;
238 }
239 if (needle.isEmpty()) {
240 return fromIndex;
241 }
242
243 char first = needle.charAt(0);
244 int max = hayStack.length() - needle.length();
245
246 for (int i = fromIndex; i <= max; i++) {
247 /* Look for first character. */
248 if (hayStack.charAt(i) != first) {
249 while (++i <= max && hayStack.charAt(i) != first) {}
250 }
251
252 /* Found first character, now look at the rest of v2 */
253 if (i <= max) {
254 int j = i + 1;
255 int end = j + needle.length() - 1;
256 for (int k = 1; j < end && hayStack.charAt(j) == needle.charAt(k); j++, k++) {}
257
258 if (j == end) {
259 /* Found whole string. */
260 return i;
261 }
262 }
263 }
264 return -1;
265 }
266 }
267}

Callers 1

indexOfMethod · 0.95

Calls 2

isEmptyMethod · 0.80
lengthMethod · 0.45

Tested by

no test coverage detected