int (@NotNull CharSequence buffer, @NotNull String pattern, int maxIndex) { char[] chars = pattern.toCharArray(); int end = buffer.length() - chars.length; if (maxIndex > end) { maxIndex = end; } SearchLoop: for (int i = maxIndex; i >= 0; i--) { for (int j = 0; j < chars.length; j++) { if (chars[j] != buffer.charAt(i + j)) continue SearchLoop; } return i; } return -1; }