Skip to content

Instantly share code, notes, and snippets.

@jstedfast
Created January 11, 2016 22:39
Show Gist options
  • Save jstedfast/5a752e0adb19bbd3dcac to your computer and use it in GitHub Desktop.
Save jstedfast/5a752e0adb19bbd3dcac to your computer and use it in GitHub Desktop.
class CustomOrdinalIgnoreCaseComparer : IEqualityComparer<string>
{
static int ToLower (int c)
{
if (c >= 0x41 && c <= 0x5A)
return c + 0x20;
return c;
}
public bool Equals (string x, string y)
{
if (x.Length != y.Length)
return false;
for (int i = 0; i < x.Length; i++) {
if (ToLower (x[i]) != ToLower (y[i]))
return false;
}
return true;
}
public int GetHashCode (string obj)
{
unsafe {
fixed (char *src = obj) {
int hash1 = 5381;
int hash2 = hash1;
char *s = src;
int c;
while ((c = s[0]) != 0) {
hash1 = ((hash1 << 5) + hash1) ^ ToLower (c);
c = s[1];
if (c == 0)
break;
hash2 = ((hash2 << 5) + hash2) ^ ToLower (c);
s += 2;
}
return hash1 + (hash2 * 1566083941);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment