查看 String.EqualsHelper 和 String.CompareOrdinalHelper (.NET 2 & 4) 的实现,在我看来,比较假设 int 字节大小为 2,long 字节大小为 4。我认为实际大小为 4 和.NET 32 位世界中的 8 个。我错过了什么?这里有什么问题吗?查看 .NET 框架中的 EqualsHelper 方法和http://www.netframeworkdevs.com/common-language-runtime/string-comparision-24141.shtml中的源代码示例。
来自 Reflector(框架 2)的逆向工程源代码:
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
private static unsafe bool EqualsHelper(string strA, string strB)
{
int length = strA.Length;
if (length != strB.Length)
{
return false;
}
fixed (char* str = ((char*) strA))
{
char* chPtr = str;
fixed (char* str2 = ((char*) strB))
{
char* chPtr2 = str2;
char* chPtr3 = chPtr;
char* chPtr4 = chPtr2;
while (length >= 10)
{
if ((((*(((int*) chPtr3)) != *(((int*) chPtr4))) || (*(((int*) (chPtr3 + 2))) != *(((int*) (chPtr4 + 2))))) || ((*(((int*) (chPtr3 + 4))) != *(((int*) (chPtr4 + 4)))) || (*(((int*) (chPtr3 + 6))) != *(((int*) (chPtr4 + 6)))))) || (*(((int*) (chPtr3 + 8))) != *(((int*) (chPtr4 + 8)))))
{
break;
}
chPtr3 += 10;
chPtr4 += 10;
length -= 10;
}
while (length > 0)
{
if (*(((int*) chPtr3)) != *(((int*) chPtr4)))
{
break;
}
chPtr3 += 2;
chPtr4 += 2;
length -= 2;
}
return (length <= 0);
}
}
}
private static unsafe int CompareOrdinalHelper(string strA, string strB)
{
int num = Math.Min(strA.Length, strB.Length);
int num2 = -1;
fixed (char* str = ((char*) strA))
{
char* chPtr = str;
fixed (char* str2 = ((char*) strB))
{
char* chPtr2 = str2;
char* chPtr3 = chPtr;
char* chPtr4 = chPtr2;
while (num >= 10)
{
if (*(((int*) chPtr3)) != *(((int*) chPtr4)))
{
num2 = 0;
break;
}
if (*(((int*) (chPtr3 + 2))) != *(((int*) (chPtr4 + 2))))
{
num2 = 2;
break;
}
if (*(((int*) (chPtr3 + 4))) != *(((int*) (chPtr4 + 4))))
{
num2 = 4;
break;
}
if (*(((int*) (chPtr3 + 6))) != *(((int*) (chPtr4 + 6))))
{
num2 = 6;
break;
}
if (*(((int*) (chPtr3 + 8))) != *(((int*) (chPtr4 + 8))))
{
num2 = 8;
break;
}
chPtr3 += 10;
chPtr4 += 10;
num -= 10;
}
if (num2 == -1)
{
goto Label_0101;
}
chPtr3 += num2;
chPtr4 += num2;
int num3 = chPtr3[0] - chPtr4[0];
if (num3 != 0)
{
return num3;
}
return (chPtr3[1] - chPtr4[1]);
Label_00E7:
if (*(((int*) chPtr3)) != *(((int*) chPtr4)))
{
goto Label_0105;
}
chPtr3 += 2;
chPtr4 += 2;
num -= 2;
Label_0101:
if (num > 0)
{
goto Label_00E7;
}
Label_0105:
if (num > 0)
{
int num4 = chPtr3[0] - chPtr4[0];
if (num4 != 0)
{
return num4;
}
return (chPtr3[1] - chPtr4[1]);
}
return (strA.Length - strB.Length);
}
}
}