Repository: netty/netty
File: common/src/main/java/io/netty/util/AsciiString.java (Java)

Code before:
     */
    public boolean endsWith(CharSequence suffix) {
        int suffixLen = suffix.length();
        return regionMatches(length() - suffixLen, suffix, 0, suffixLen);
    }

    /**
     * Compares the specified string to this string ignoring the case of the characters and returns true if they are
     * equal.
     *
     * @param string the string to compare.
     * @return {@code true} if the specified string is equal to this string, {@code false} otherwise.
     */
    public boolean contentEqualsIgnoreCase(CharSequence string) {
        if (this == string) {
            return true;
        }

        if (string == null || string.length() != length()) {
            return false;
        }

        if (string instanceof AsciiString) {
            AsciiString rhs = (AsciiString) string;
            for (int i = arrayOffset(), j = rhs.arrayOffset(), end = i + length(); i < end; ++i, ++j) {
                if (!equalsIgnoreCase(value[i], rhs.value[j])) {
                    return false;
                }
            }
            return true;
        }

        for (int i = arrayOffset(), j = 0, end = length(); j < end; ++i, ++j) {
            if (!equalsIgnoreCase(b2c(value[i]), string.charAt(j))) {
                return false;
            }
        }
        return true;
    }

    /**
     * Copies the characters in this string to a character array.
     *
     * @return a character array containing the characters of this string.
     */
    public char[] toCharArray() {
        return toCharArray(0, length());
    }

    /**
     * Copies the characters in this string to a character array.

Review comment (suggestion):
This is very curious to me. My understanding is that the length check is necessary to make the compiler happy to eliminate bounds checks in the loop, not correctness. If so, it probably deserves a comment or I see it being deleted at some point.

Could we make it `length <= value.length`? Does it help to include checking the `rhs` array length as well to eliminate that check? And could that be applied to the inconsistent offset case as well with a check to the tune of `length + offset <= value.length`?

Perhaps I'm getting too far in the weeds. 😂 

Code after:
     */
    public boolean endsWith(CharSequence suffix) {
        int suffixLen = suffix.length();
        return regionMatches(length() - suffixLen, suffix, 0, suffixLen);
    }

    /**
     * Compares the specified string to this string ignoring the case of the characters and returns true if they are
     * equal.
     *
     * @param string the string to compare.
     * @return {@code true} if the specified string is equal to this string, {@code false} otherwise.
     */
    public boolean contentEqualsIgnoreCase(CharSequence string) {
        if (this == string) {
            return true;
        }

        if (string == null || string.length() != length()) {
            return false;
        }

        if (string instanceof AsciiString) {
            AsciiString other = (AsciiString) string;
            byte[] value = this.value;
            if (offset == 0 && other.offset == 0 && length == value.length) {
                byte[] otherValue = other.value;
                for (int i = 0; i < value.length; ++i) {
                    if (!equalsIgnoreCase(value[i], otherValue[i])) {
                        return false;
                    }
                }
                return true;
            }
            return misalignedEqualsIgnoreCase(other);
        }

        byte[] value = this.value;
        for (int i = offset, j = 0; j < string.length(); ++i, ++j) {
            if (!equalsIgnoreCase(b2c(value[i]), string.charAt(j))) {
                return false;
            }
        }
        return true;
    }

    private boolean misalignedEqualsIgnoreCase(AsciiString other) {
        byte[] value = this.value;
        byte[] otherValue = other.value;
        for (int i = offset, j = other.offset, end = offset + length; i < end; ++i, ++j) {
            if (!equalsIgnoreCase(value[i], otherValue[j])) {