Why does Java StringLatin1.regionMatchesCI method perform toUpperCase() and than toLowerCase() when comparing chars?

huangapple go评论48阅读模式
英文:

Why does Java StringLatin1.regionMatchesCI method perform toUpperCase() and than toLowerCase() when comparing chars?

问题

I was looking into String.equalsIgnoreCase method and found that at the end it invokes StringLatin1.regionMatchesCI method.

However, the code of this method seems strange to me, here it is:

public static boolean regionMatchesCI(byte[] value, int toffset,
                                      byte[] other, int ooffset, int len) {
    int last = toffset + len;
    while (toffset < last) {
        char c1 = (char)(value[toffset++] & 0xff);
        char c2 = (char)(other[ooffset++] & 0xff);
        if (c1 == c2) {
            continue;
        }
        char u1 = Character.toUpperCase(c1);
        char u2 = Character.toUpperCase(c2);
        if (u1 == u2) {
            continue;
        }
        if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
            continue;
        }
        return false;
    }
    return true;
}

Why check the upperCase and then lowerCase? Wouldn't the lowercase always fail if the uppercase check doesn't match? Am I missing something?

英文:

I was looking into String.euqalsIgnoreCase method and found that at the end it invokes StringLatin1.regionMatchesCI method.

However, the code of this method seems strange to me, here it is:

public static boolean regionMatchesCI(byte[] value, int toffset,
                                      byte[] other, int ooffset, int len) {
    int last = toffset + len;
    while (toffset &lt; last) {
        char c1 = (char)(value[toffset++] &amp; 0xff);
        char c2 = (char)(other[ooffset++] &amp; 0xff);
        if (c1 == c2) {
            continue;
        }
        char u1 = Character.toUpperCase(c1);
        char u2 = Character.toUpperCase(c2);
        if (u1 == u2) {
            continue;
        }
        if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
            continue;
        }
        return false;
    }
    return true;
}

Why check the upperCase and than lowerCase? Wouldn't the lower cases always fail in case the upper check doesn't match? Am I missing something?

答案1

得分: 4

在我找到的源代码中(在谷歌的某个地方),对于这个函数,我有额外的解释:

// 尝试将两个字符都转换为大写。
// 如果结果匹配,则比较扫描应该继续。
char u1 = Character.toUpperCase(c1);
char u2 = Character.toUpperCase(c2);
if (u1 == u2) {
continue;
}
// 不幸的是,将字符转换为大写不适用于格鲁吉亚字母,因为它有关于大小写转换的奇怪规则。因此,在退出之前,我们需要进行最后一次检查。
if (Character.toLowerCase(u1) == Character.toLowerCase(u2)) {
continue;
}

所以看起来有一些变通方法。在GitHub上,您可能会找到更多不同的此函数实现。

英文:

In the source code I found (somewhere on google) for this function I have additional explanation:

        // 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;
        }

So it looks like some workarounds. On github you might find even more different implementations of this function.

huangapple
  • 本文由 发表于 2020年7月28日 03:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63122464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定