Skip to content

Instantly share code, notes, and snippets.

@paincompiler
Last active February 26, 2016 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paincompiler/e204d180794fd5836579 to your computer and use it in GitHub Desktop.
Save paincompiler/e204d180794fd5836579 to your computer and use it in GitHub Desktop.
simulate python 2.7 built-in __hash__ of string object
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