class WildcardMatcher {

    public static int indexOfIgnoreCase(final CharSequence haystack1, final CharSequence haystack2, final String needle, final boolean ignoreCase, final int start, final int end) {
        if (start < 0) {
            return -1;
        }
        int totalHaystackLength = haystack1.length() + haystack2.length();
        if (needle.isEmpty() || totalHaystackLength == 0) {
            // Fallback to legacy behavior.
            return indexOf(haystack1, needle);
        }

        final int haystack1Length = haystack1.length();
        final int needleLength = needle.length();
        for (int i = start; i < end; i++) {
            // Early out, if possible.
            if (i + needleLength > totalHaystackLength) {
                return -1;
            }

            // Attempt to match substring starting at position i of haystack.
            int j = 0;
            int ii = i;
            while (ii < totalHaystackLength && j < needleLength) {
                char c = ignoreCase ? Character.toLowerCase(charAt(ii, haystack1, haystack2, haystack1Length)) : charAt(ii, haystack1, haystack2, haystack1Length);
                char c2 = ignoreCase ? Character.toLowerCase(needle.charAt(j)) : needle.charAt(j);
                if (c != c2) {
                    break;
                }
                j++;
                ii++;
            }
            // Walked all the way to the end of the needle, return the start
            // position that this was found.
            if (j == needleLength) {
                return i;
            }
        }
        return -1;
    }

}