Last active
February 26, 2016 09:30
-
-
Save paincompiler/e204d180794fd5836579 to your computer and use it in GitHub Desktop.
simulate python 2.7 built-in __hash__ of string object
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
import "math/big" | |
// simulate python 2.7 built-in __hash__ of string object | |
func hash(s string) *big.Int { | |
if len(s) == 0 { | |
return big.NewInt(0) | |
} | |
value := big.NewInt(int64([]byte(s)[0]) << 7) | |
for _, v := range []byte(s) { | |
value = value.Xor(value.Mul(value, big.NewInt(1000003)), big.NewInt(int64(v))) | |
} | |
value = value.Xor(value, big.NewInt(int64(len(s)))) | |
if value.Int64() == -1 { | |
value.SetInt64(-2) | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment