Skip to content

Instantly share code, notes, and snippets.

@MihaZupan
Created May 14, 2020 14:20
Show Gist options
  • Save MihaZupan/cd9104ff383cbc679fd6f38d6babade7 to your computer and use it in GitHub Desktop.
Save MihaZupan/cd9104ff383cbc679fd6f38d6babade7 to your computer and use it in GitHub Desktop.
public bool Equals(string value)
{
return (value != null || this == null) && string.EqualsHelper(this, value);
}
private static unsafe bool EqualsHelper(string strA, string strB)
{
int length = strA.Length;
if (length != strB.Length)
return false;
fixed (char* chPtr1 = strA)
fixed (char* chPtr2 = strB)
{
char* chPtr3 = chPtr1;
char* chPtr4;
for (chPtr4 = chPtr2; length >= 10 && (*(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)); length -= 10)
{
chPtr3 += 10;
chPtr4 += 10;
}
for (; length > 0 && *(int*) chPtr3 == *(int*) chPtr4; length -= 2)
{
chPtr3 += 2;
chPtr4 += 2;
}
return length <= 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment