0

why equalsIgnoreCase() use toUpperCase() firstly, and toLowerCase() to avoid the mistaken identification of Georgian alphabet, rather than use toLowerCase() for the first time and only use it?

public boolean regionMatches(boolean ignoreCase, int toffset,
        String other, int ooffset, int len) {
    char ta[] = value;
    int to = toffset;
    char pa[] = other.value;
    int po = ooffset;
    // Note: toffset, ooffset, or len might be near -1>>>1.
    if ((ooffset < 0) || (toffset < 0)
            || (toffset > (long)value.length - len)
            || (ooffset > (long)other.value.length - len)) {
        return false;
    }
    while (len-- > 0) {
        char c1 = ta[to++];
        char c2 = pa[po++];
        if (c1 == c2) {
            continue;
        }
        if (ignoreCase) {
            // If characters don't match but case may be ignored,
            // try converting both characters to uppercase.
            // If the results match, then the comparison scan should
            // continue.
            char u1 = Character.toUpperCase(c1);
            char u2 = Character.toUpperCase(c2);
            if (u1 == u2) {
                continue;
            }
            // Unfortunately, conversion to uppercase does not work properly
            // for the Georgian alphabet, which has strange rules about case
            // conversion.  So we need to make one last check before
            // exiting.
            if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
                continue;
            }
        }
        return false;
    }
    return true;
}
xiaoxi
  • 1
  • 2
  • There is no evidence to preclude that other character sets dont work properly with converting to lower case. They seem to be just playing it safe here. – vikingsteve Aug 14 '18 at 09:34
  • thanks, I find the answer in the duplicate question. – xiaoxi Aug 14 '18 at 09:55

1 Answers1

1

Notice that it's using toLowerCase on the result of toUpperCase (u1 and u2), not on the original characters (c1 and c2). What it's checking is that c1 converted to upper case (u1) and then converted to lower case matches c2 converted to upper case (u2) and then converted to lower case. So it can't just skip the initial toUpperCase, it's using the information it got from that (u1 and u2).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875