为什么 Java StringLatin1.regionMatchesCI 方法在比较字符时执行 toUpperCase() 而不是 toLowerCase()?
Why does Java StringLatin1.regionMatchesCI method perform toUpperCase() and than toLowerCase() when comparing chars?
我正在研究 String.euqalsIgnoreCase
方法,发现最后它调用了 StringLatin1.regionMatchesCI
方法。
不过,这个方法的代码我觉得很奇怪,这里是:
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;
}
为什么要检查大写而不是小写?如果上面的检查不匹配,小写字母不会总是失败吗?我错过了什么吗?
在我找到的源代码中(在 google 的某处),我对此函数有额外的解释:
// 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;
}
所以它看起来像是一些解决方法。在 github 上,您可能会发现此函数的更多不同实现。
我正在研究 String.euqalsIgnoreCase
方法,发现最后它调用了 StringLatin1.regionMatchesCI
方法。
不过,这个方法的代码我觉得很奇怪,这里是:
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;
}
为什么要检查大写而不是小写?如果上面的检查不匹配,小写字母不会总是失败吗?我错过了什么吗?
在我找到的源代码中(在 google 的某处),我对此函数有额外的解释:
// 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;
}
所以它看起来像是一些解决方法。在 github 上,您可能会发现此函数的更多不同实现。