Last active
November 7, 2017 02:48
-
-
Save gerrited/7542231 to your computer and use it in GitHub Desktop.
Microsoft messed up the 64 bit version of GetHashCode. This is a platform independent port of the 32 bit version of Object.GetHashCode based on the unsafe code of Yoni Toledano on StackOverflow (http://stackoverflow.com/a/835571/2132050)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class StringExt | |
{ | |
public static int GetHashCode32(this string s) | |
{ | |
var chars = s.ToCharArray(); | |
var lastCharInd = chars.Length - 1; | |
var num1 = 0x15051505; | |
var num2 = num1; | |
var ind = 0; | |
while (ind <= lastCharInd) | |
{ | |
var ch = chars[ind]; | |
var nextCh = ++ind > lastCharInd ? '\0' : chars[ind]; | |
num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ (nextCh << 16 | ch); | |
if (++ind > lastCharInd) break; | |
ch = chars[ind]; | |
nextCh = ++ind > lastCharInd ? '\0' : chars[ind++]; | |
num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ (nextCh << 16 | ch); | |
} | |
return num1 + num2 * 0x5d588b65; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment