Created
December 24, 2015 21:32
-
-
Save paiv/b4f76fc236fc6a3d956b to your computer and use it in GitHub Desktop.
String GetHashCode from mscorlib (.NET)
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
#include <iostream> | |
using namespace std; | |
typedef unsigned short u16; | |
typedef unsigned int u32; | |
typedef int s32; | |
typedef wchar_t wc; | |
static s32 | |
getHash32(wc* src, int length) | |
{ | |
int hash1 = (5381<<16) + 5381; | |
int hash2 = hash1; | |
int* pint = (int *)src; | |
int len = length; | |
while (len > 2) | |
{ | |
hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0]; | |
hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ pint[1]; | |
pint += 2; | |
len -= 4; | |
} | |
if (len > 0) | |
{ | |
hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0]; | |
} | |
return hash1 + (hash2 * 1566083941); | |
} | |
static s32 | |
getHash64(wc* src, int length) | |
{ | |
int hash1 = 5381; | |
int hash2 = hash1; | |
int c = 0; | |
wc *s = src; | |
while ((c = s[0]) != 0) { | |
hash1 = ((hash1 << 5) + hash1) ^ c; | |
c = s[1]; | |
if (c == 0) | |
break; | |
hash2 = ((hash2 << 5) + hash2) ^ c; | |
s += 2; | |
} | |
return hash1 + (hash2 * 1566083941); | |
} | |
static s32 | |
getHash11(wc* src, int length) | |
{ | |
int hash = 5381; | |
int c = 0; | |
wc* szStr = src; | |
while ((c = *szStr) != 0) { | |
hash = ((hash << 5) + hash) ^ c; | |
++szStr; | |
} | |
return hash; | |
} | |
static s32 | |
getHashMono(wc* src, int length) | |
{ | |
wc* chPtr1 = src; | |
wc* chPtr2 = chPtr1; | |
wc* chPtr3 = chPtr2 + length - 1; | |
int num1 = 0; | |
while (chPtr2 < chPtr3) | |
{ | |
int num2 = (num1 << 5) - num1 + (int) *chPtr2; | |
num1 = (num2 << 5) - num2 + (int) *(chPtr2 + 1); | |
chPtr2 += 2; | |
} | |
wc* chPtr4 = (chPtr3 + 1); | |
if (chPtr2 < chPtr4) | |
num1 = (num1 << 5) - num1 + (int) *chPtr2; | |
return num1; | |
} | |
static u16 | |
getHash(wc*src, int length) | |
{ | |
return getHashMono(src, length); | |
} | |
int main() { | |
std::cout << "hash 'yuna' = " << getHash(L"yuna", 4) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment